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
    评论
自2020年以来,自动驾驶汽车(Autonomous Vehicle)在这一领域取得了显著进展。 自动驾驶汽车通过使用先进的传感技术,如雷达、激光雷达和摄像头,以及深度学习和人工智能算法,实现了车辆的自主驾驶。它们能够感知周围环境,了解道路状况,并做出相应的决策和行驶动作,从而实现无需人类操控的车辆行驶。 自动驾驶汽车在2020年迅速崭露头角。它们的技术日益成熟,不断的实验和测试表明其在安全性和性能方面已经取得了显著的突破。虽然仍然存在一些挑战,比如在复杂城市环境中导航和处理紧急情况,但这些问题正经过不断的研究和改进来得以解决。 在未来,自动驾驶汽车有望在各个领域发挥重要作用。首先,它们将可以提高道路交通的安全性。由于自动驾驶车辆不受人类司机的疲劳、分心和驾驶误差的限制,它们的驾驶能力更为稳定和准确。其次,自动驾驶汽车还能够提高交通效率。通过与其他车辆实时通信和协同,它们可以避免交通堵塞和减少事故发生,从而减少交通拥堵和行车时间成本。 此外,自动驾驶汽车也将为交通出行带来便利和舒适性。乘客可以更轻松地进行其他活动,如工作、休息或娱乐,而不必担心驾驶问题。老年人和残障人士也将能够自由独立地出行,提高他们的生活质量。 综上所述,作为2020年的重要趋势,自动驾驶汽车具有广阔的应用前景。通过不断的创新和发展,它们将在道路交通安全、交通效率和出行体验方面取得进一步的提升。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值