Gym 100935F A Poet Computer (字典树)

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

Description

standard input/output
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

Input
2
4
cocochannel
chrisschannel
MBCchannel
controlpanel
5
superman
batman
ironman
chrissbrown
MyCrown
Output
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);
	}
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值