Weird Cryptography

花品
2023-12-01
                              Weird Cryptography 

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.

Example
Input
3 one two two
7 num go book go hand num num
25 aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa aa
0

Output
Case 1: 12
Case 2: 124
Case 3: 1111111111111111111111111

Note
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.

题目大意:给你n个字符串,判断每个字符串出现的次数,把这些字符串分组,要求每组里的字符串不能相同,将每一组的元素个数从小到大输出;

思路:模拟,先排序,然后累计每个字符串出现的次数,依次放到每一组中,输出时注意倒序输出;

code:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
#define INF 0x3f3f3f3f

int ans[10010],cur[10010];
vector<string >v;
string s;
int n;

bool cmp(int s1,int s2)
{
    if(s1>s2) return true;
    return false;
}

int main()
{
    int tt = 0;
    while(~scanf("%d",&n) && n)
    {
        v.clear();
        int len = 1;
        memset(ans,0,sizeof(ans));
        memset(cur,0,sizeof(cur));
        for(int i=0;i<n;i++)
        {
            cin>>s;
            v.push_back(s);
        }
        sort(v.begin(),v.end());
        for(int i=0;i<n-1;i++)
        {
            if(v[i] == v[i+1])
                ans[len]++;
            else
                ans[len++]++;
        }
        if(v[n-2] == v[n-1])
            ans[len]++;
        else ans[++len]++;
        int maxn = 0;
        for(int i=1;i<=len;i++)
        {
            if(maxn < ans[i])
                maxn = ans[i];
            for(int j=0;j<ans[i];j++)
                cur[j]++;
        }
        printf("Case %d: ",++tt);
        for(int i=maxn-1;i>=0;i--)
            printf("%d",cur[i]);
        printf("\n");
    }
    return 0;
}
 类似资料:

相关阅读

相关文章

相关问答