您的位置:首页 > 其它

gym 100935B (字典树)

2016-03-24 23:32 344 查看
B. Weird Cryptography

time limit per test
2 seconds

memory limit per test
64 megabytes

input
standard input

output
standard output

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.

Examples

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.

题意:一个集合的size表示一个数字,把n个字符串放到不同的集合,一个集合中不能出现相同的字符串,求用

每个集合的size表示的数字组成的数的最小值.

开一个字典树节点统计从根到这个节点为末尾的字符串个数,然后最后统计所有的节点数字,每次把数组

贪心减一.

#include <bits/stdc++.h>
using namespace std;
#define maxn 11111

int n;
struct node {
int num;
int next[26];
}tree[maxn<<4];
char a[maxn];
int root, cnt;

void build_node (int u) {
for (int i = 0; i < 26; i++)
tree[u].next[i] = -1;
tree[u].num = 0;
}

void add () {
int l = strlen (a);
int p = root;
for (int i = 0; i < l; i++) {
int id = a[i]-'a';
if (tree[p].next[id] == -1) {
build_node (cnt);
tree[p].next[id] = cnt++;
}
if (i == l-1) {
tree[tree[p].next[id]].num++;
}
p = tree[p].next[id];
}
}

int ans[maxn], ans_cnt;

void dfs (int u) {
if (tree[u].num > 0) {
ans[ans_cnt++] = tree[u].num;
}
for (int i = 0; i < 26; i++) {
if (tree[u].next[i] != -1) {
dfs (tree[u].next[i]);
}
}
return ;
}

void solve () {
ans_cnt = 0;
dfs (root);
sort (ans, ans+ans_cnt);
char gg[maxn];
int gg_cnt = 0;
int pos = 0;
while (pos < ans_cnt) {
gg[gg_cnt++] = ans_cnt-pos+'0';
for (int i = pos; i < ans_cnt; i++) {
ans[i]--;
if (ans[i] == 0)
pos++;
}
}
for (int i = gg_cnt-1; i >= 0; i--) {
printf ("%c", gg[i]);
}
printf ("\n");
}

int main () {
//freopen ("in.txt", "r", stdin);
int kase = 0;
while (scanf ("%d", &n) == 1 && n) {
cnt = 1;
root = 0;
build_node (root);
for (int i = 0; i < n; i++) {
scanf ("%s", a);
add ();
}
printf ("Case %d: ", ++kase);
solve ();
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: