Gym 100935B Weird Cryptography (模拟)

来源:互联网 发布:中学生编程竞赛 编辑:程序博客网 时间:2024/04/27 06:25
B - Weird Cryptography
Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u
Submit Status Practice Gym 100935B

Description

standard input/output
Statements

Khaled was sitting in the garden under an apple tree, suddenly! , well... you should guess what happened, an apple fell on his head! , so he came up with a new Cryptography method!! The method deals only with numbers, so... If you want to encode a number, you must represent each of its digits with a set of strings, then the size of the set is the digit itself, No set should contain the same string more than once. For example: the number 42, can be represented with the following two sets: 1) "dog"   "load"   "under"   "nice". 2) "stack"   "dog". The first set contain four strings so it represent the digit 4. The second set contain two strings so it represent the digit 2. Given N strings, what is the smallest number you can get from dividing these strings into non-empty sets, and then decode the result by Khaled's Cryptography method? , You must use all the given strings, and no set should contain the same string more than once.

Input

The input consists of several test cases, each test case starts with 0 < N  ≤  10000, the number of the given strings, then follows N space-separated string, each string will contain only lower-case English letters, and the length of each string will not exceeded 100. You can assume that there are no more than nine distinct strings among the given strings. A line containing the number 0 defines the end of the input you should not process this line.

Output

For each test case print a single line in the following format: "Case c: x"   where c is the test case number starting from 1 and x is the solution to the described problem above.

Sample Input

Input
3 one two two7 num go book go hand num num25 aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa0
Output
Case 1: 12Case 2: 124Case 3: 1111111111111111111111111

Hint

In the first sample, we divided the given strings into two sets, the first set contains two word: "one"   and "two"   so it represents the digit 2, the second set contains only one word: "two"   so it represent the digit 1.


题意:求解给出的字符串可以组成几个非空集合,这些非空集合中不能包含相同的字符串,最终输出的数字是每个非空集合包含元素的个数,从小到大输出
#include <cstdio>#include <cstring>#include <algorithm>#include <map>#include <stack>#include <iostream>#include <string>using namespace std;const int MAXN = 1e4 + 5;const int MAXM =1e3 + 5;map<string, int>F;string s;int N, A[MAXN];int main(){    int cas = 1;    while(~scanf("%d", &N), N){        int Max = 0;        F.clear();        memset(A, 0, sizeof(A));        for(int i = 0;i < N;i ++){            cin >> s;            F[s] ++;            A[F[s]] ++;            Max = max(Max, F[s]);        }        printf("Case %d: ", cas ++);        for(int i = Max;i > 0;i --){            if(A[i] != 0) printf("%d", A[i]);        }        printf("\n");    }    return 0;}

直接map搞定
#include <cstdio>#include <cstring>#include <algorithm>#include <map>#include <stack>#include <iostream>#include <string>using namespace std;const int MAXN = 1e2 + 5;const int MAXM =1e3 + 5;typedef long long LL;map<string, int>A;stack<int>F;string s;int B[MAXN];int main() {int n;int cas = 1;while(~scanf("%d", &n), n) {A.clear();while(!F.empty()) F.pop();memset(B, 0, sizeof(B));for(int i = 0; i < n; i ++) {cin >> s;A[s] ++;}while(true) {int a = 0, b = 0;for(map<string,int>::iterator it = A.begin(); it != A.end(); it ++) {it -> second --;if(it -> second >= 0) {b ++;}if(it -> second > 0) a++;}F.push(b);if(a == 0) break;}printf("Case %d: ", cas ++);while(!F.empty()) {int a = F.top();F.pop();printf("%d",a);}printf("\n");}return 0;}


1 0
原创粉丝点击