字典树 - A Poet Computer

字典树 - A Poet Computer

The ACM team is working on an AI project called (Eih Eye Three) that allows computers to write poems. One of the problems they stumbled upon is finding words with the same suffix. The ACM team constructed a dictionary of words, They are interested only in the longest common suffix, That is, a suffix common to three or more words in the dictionary… A suffix is any substring that starts from some arbitrary position in the string and reaches the end of the string. As the ACM team was also preparing for the ACM-TCPC2015 contest, they figured that the contestants can help in solving this problem. Your task is to write a program that finds a longest common suffix in a dictionary of words. An entry in the dictionary is a word of English letters only. Small letters are the same as capital letters. You can assume that there is exactly one unique solution for every test case.

Input

The first line of the input contains an integer T, the number of test cases. Each test case starts with a line containing one integer K, then K lines follow, each containing one string “Si” that represents an entry in the dictionary. 0 < T ≤ 50 |Si| ≤ 100 0 < K ≤ 1000

Output

For each test case, print on the first line “Case c:” where ‘c’ is the test case number. On the second line you should print an integer denoting the length of the longest common suffix and another integer denoting how many words have the suffix appeared in.

Sample Input

Input
2
4
cocochannel
chrisschannel
MBCchannel
controlpanel
5
superman
batman
ironman
chrissbrown
MyCrown
Output
Case 1:
7 3
Case 2:
3 3


--------------------------------------------------------------我是分割线^_^----------------------------------------------------------


一道字典树的题目,当时还是想用指针做出来的,可惜没有时间了,不过赛后看了一下别人的代码,觉得直接
开结构体数组模拟指针也蛮好的,虽然浪费很多内存,不过速度很快,值得一学,之前写过指针的字典树,觉
得指来指去真是会让人头晕= =,所以学习一下这个结构体数组版本的字典树,其实和指针版的差不多,稍微
改改就行了,主体基本一样的,以后慢慢理解.......记住这个套路就行

#include<iostream>
#include<algorithm>
#include<cstdio> #include<cstring> #include<string> #include<cmath> #include<vector> #include<queue> #include<cctype> using namespace std; #define Int __int64 #define INF 0x3f3f3f3f const int MAXN = 111111; int triecnt; int root; struct Node { int End;//代表以此结为的字符串有多少个 int son[26]; int deep;//字符串的第几个字符 }trie[MAXN]; int new_tire() { triecnt++; trie[triecnt].End = 0; for (int i = 0; i < 26; i++) { trie[triecnt].son[i] = 0; } return triecnt; } void init_trie() { triecnt = 0; root = new_tire(); } void insert_str(char str[]) { int len = strlen(str); int rt = root; for (int i = len - 1; i >= 0; i--) { int id = str[i] - 'a'; if (trie[rt].son[id] == 0) { trie[rt].son[id] = new_tire(); rt = trie[rt].son[id]; trie[rt].End++; trie[rt].deep = len - i; } else { rt = trie[rt].son[id]; trie[rt].End++; trie[rt].deep = len - i; } } } int main() { //freopen("input.txt", "r", stdin); int cas; int sign; while (scanf("%d", &cas) != EOF) { sign = 1; while (cas--) { init_trie(); int k; scanf("%d", &k); while (k--) { char str[105]; scanf("%s", str); int len = strlen(str); for (int i = 0; i < len; i++) str
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值