您的位置:首页 > 其它

B - Weird Cryptography Gym - 100935B(集合,思维题)

2018-02-27 11:50 471 查看
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.


题意:就是说一样的单词不能放在一个集合里,一个集合最多有九个单词,每个集合里面的单词数代表这个集合是几,然后用集合代表的数组成一个多位数,要求这个多位数最小。要求多位数最小那么可得到思路:让一个集合放最多的单词,也就是只要不重复的单词都放到这个集合里,然后这个集合当个位数,这样组合出来的一定是最小的。

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<algorithm>
using namespace std;
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)
{
i
4000
f(a.s[0]==b.s[0])
return a.s[1]<b.s[1];
else
return a.s[0]<b.s[0];

}

int main()
{

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];//除了重复的单词,不重复的有多少个
}
}
sort(real,real+cas);
for(int i=0; i<cas; i++)
{
for(int j=1; j<=real[i]; j++)
{
ans[j]++;//j代表了总共要分几个集合ans[j]代表集合里放了几个数
}
}
printf("Case %d: ",u++);
ans[1]+=sum;//不重复的全放在一个集合里,这样这个集合最大,当做个位数保证组合出来的数最小
if(sum!=n)
{
for(int i=real[cas-1]; i>=1; i--)//倒着输出,最小的数做最高位
{
printf("%d",ans[i]);
}
printf("\n");
}
else
{
printf("%d\n",sum);
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: