Description
Statements
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
2 4 cocochannel chrisschannel MBCchannel controlpanel 5 superman batman ironman chrissbrown MyCrown
Case 1: 7 3 Case 2: 3 3
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <stack>
using namespace std;
const int MAXN = 1e2 + 5;
int Maxlen, Maxnum;
int T, K;
struct node {
int next[27];
int v, num;
void init() {
v=-1;
num = 0;
memset(next,-1,sizeof(next));
}
};
struct node L[300000];
int tot=0;
void add(char a[],int len) {
int now=0;
for(int i=0; i<len; i++) {
int tmp=a[i]-'a';
int next=L[now].next[tmp];
if(next==-1) {
next=++tot;
L[next].init();
L[now].next[tmp]=next;
}
now=next;
L[now].num ++;
if(L[now].num >= 3) {
if(Maxlen < i + 1) {
Maxlen = i + 1;
Maxnum = L[now].num;
} else if(Maxnum < L[now].num && Maxlen <= i + 1) {
Maxnum = L[now].num;
}
}
}
L[now].v=0;
}
char A[MAXN];
int main() {
int cas = 1;
scanf("%d", &T);
while(T --) {
tot = 0;
L[0].init();
scanf("%d", &K);
Maxlen = 0, Maxnum = 0;
for(int i = 0; i < K; i ++) {
scanf("%s", A);
int len = strlen(A);
for(int j = 0; j < len; j ++) {
if(A[j] < 'a') {
A[j] += 32;
}
}
for(int j = 0; j < len / 2; j ++) {
swap(A[j], A[len - j - 1]);
}
add(A, len);
}
printf("Case %d:\n", cas ++);
printf("%d %d\n", Maxlen, Maxnum);
}
}