標籤:
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 - 字典樹