ACM: Gym 100935F A Poet Computer - 字典樹

來源:互聯網
上載者:User

標籤:

Gym 100935F A Poet Computer Time Limit:2000MS      Memory Limit:65536KB      64bit IO Format:%I64d & %I64u

Description

standard input/output

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

/*/題意:找最長常見尾碼;單純的字典樹,找尾碼數大於等於3,長度儘可能大的尾碼。AC代碼:/*/
#include"algorithm"#include"iostream"#include"cstring"#include"cstdlib"#include"cstdio"#include"string"#include"vector"#include"queue"#include"cmath"using namespace std;typedef long long LL ;#define memset(x,y) memset(x,y,sizeof(x))#define memcpy(x,y) memcpy(x,y,sizeof(x))#define FK(x) cout<<"["<<x<<"]\n"struct Trie {int v;int len;Trie *next[26];} root;struct Ans {int len,num;} ans,t;void init() {ans.len=ans.num=0;t.len=t.num=0;}void BuildTree(char *s) {//FK("NO");int len=strlen(s);Trie *p=&root,*q;for(int i=len-1; i>=0; i--) {int num;//if(!((s[i]<=‘Z‘&&s[i]>=‘A‘)||(s[i]<=‘z‘&&s[i]>=‘a‘)))continue;if(s[i]<=‘z‘&&s[i]>=‘a‘)num=s[i]-‘a‘;else num=s[i]-‘A‘;if(p->next[num]==NULL) {q=(Trie *)malloc(sizeof(root));q->v=1;for(int j=0; j<26; j++) {q->next[j]=NULL;}q->len=p->len+1;p->next[num]=q;p=p->next[num];} else {p=p->next[num];p->v++;}if(p->v >= 3&&p->len >= t.len) {t.len=p->len;t.num=p->v;}}}void DeleteTrie(Trie *T,int k) {if (T==NULL) return ;for(int i=0; i<26; i++) {if(T->next[i]!=NULL) {DeleteTrie(T->next[i],k+1);}}if(k==0) {for(int i=0; i<26; i++) {T->next[i]=NULL;}} else free(T);return ;}int main() {int T,n;char s[205];scanf("%d",&T);for(int qq=1; qq<=T; qq++) {scanf("%d",&n);init();for(int i=0; i<n; i++) { cin>>s; BuildTree(s);if(t.num>=3&&t.len>=ans.len) {ans=t;}}printf("Case %d:\n", qq);printf("%d %d\n",ans.len,ans.num);Trie *p=&root;DeleteTrie(p,0);}return 0;}

  

ACM: Gym 100935F A Poet Computer - 字典樹

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.