CodeForces Gym 100935B Weird Cryptography

Weird Cryptography
Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u
 

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

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 <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <vector>
#include <iomanip>
#include <math.h>
#include <map>
using namespace std;
#define FIN     freopen("input.txt","r",stdin);
#define FOUT    freopen("output.txt","w",stdout);
#define INF     0x3f3f3f3f
#define lson    l,m,rt<<1
#define rson    m+1,r,rt<<1|1
typedef long long LL;

int num[10005];
int vis[10005];
int real[10005];
int ans[10005];

struct node{
    char s[105];

} a[10005];

int cmp(node a,node b){
    if(a.s[0]==b.s[0])
        return a.s[1]<b.s[1];
    else
        return a.s[0]<b.s[0];

}

int main()
{
    //FIN
    //FOUT
    int n;
    int u=1;
    while(~scanf("%d",&n))
    {
        if(n==0)  break;
        for(int i=1; i<=n; i++){
            scanf("%s",a[i].s);
        }

        sort(a+1,a+1+n,cmp);

        int cas=0;

        memset(vis,0,sizeof(vis));
        memset(num,0,sizeof(num));
        memset(real,0,sizeof(real));
        memset(ans,0,sizeof(ans));

        for(int i=1; i<=n; i++){
            for(int j=1; j<=n; j++){
                if(i==j)  continue;
                if(strcmp(a[i].s,a[j].s)==0&&vis[j]==0){
                    num[i]++;
                    vis[j]=1;
                }

            }
            vis[i]=1;
        }
        int sum=n;
        for(int i=1; i<=n; i++){
            if(num[i]!=0){
               num[i]++;
               real[cas++]=num[i];
               sum-=num[i];
               //cout<<num[i]<<endl;
            }
        }
        sort(real,real+cas);
        /*for(int i=0;i<cas;i++)
            cout<<real[i]<<endl;
        cout<<"cas="<<cas<<endl;
        cout<<"sum="<<sum<<endl;
        cout<<"-----------------"<<endl;*/
        for(int i=0;i<cas;i++){
            for(int j=1;j<=real[i];j++){
                ans[j]++;
            }
        }
        printf("Case %d: ",u++);
        ans[1]+=sum;
        //cout<<"----------"<<real[cas-1]<<endl;
        if(sum!=n){
            for(int i=real[cas-1];i>=1;i--){
                printf("%d",ans[i]);
            }
            printf("
");
        }else{
            printf("%d
",sum);
        }


    }

}

  

原文地址:https://www.cnblogs.com/Hyouka/p/5754807.html