比赛 cmath生日赛 评测结果 AAAAAAAWAA
题目名称 鱼的感恩 最终得分 90
用户昵称 ONCE AGAIN 运行时间 2.455 s
代码语言 C++ 内存使用 16.80 MiB
提交时间 2017-06-13 21:50:46
显示代码纯文本
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
const int SIZEN = 1000100;
typedef unsigned int unt;
const unt mod = 1e9+7;
const unt bit = 2619;
int fail[SIZEN] = {0},n;
unt hash[SIZEN] = {0};
unt mi[SIZEN] = {0};
char str[SIZEN] = {0};
void KMP_INIT(){
	int ans = 0;
	int j = 0;
	for(int i = 2;i <= n;i++){
		while(j && str[j+1]!=str[i])j = fail[j];
		if(str[j+1] == str[i])j++;
		fail[i] = j;
	}
	int tmp = fail[n];
	for(int i = 2;i <= n;i++){
		while(j && str[j+1]!=str[i])j = fail[j];
		if(str[j+1] == str[i])j++;
		fail[i] = j;
		if(i!=n && str[i] == str[n]) ans = max(ans,min(fail[i],tmp));
	}
	for(ans;ans;ans--){
		unt s1 = hash[ans];
		unt s2 = (hash[n]-hash[n-ans]*mi[ans]);
		if(s1 == s2)break;
	}
	if(ans == 0)puts("---");
	else {for(int i = 1;i <= ans;i++)putchar(str[i]);puts("");}
}
int main(){
    freopen("fool.in","r",stdin);
	freopen("fool.out","w",stdout);
	int Q;scanf("%d",&Q);
	mi[0] = 1;
	for(int i = 1;i <= 100000;i++)mi[i] = mi[i-1]*bit;
	while(Q--){
		scanf("%s",str+1);
		n = strlen(str+1);
		memset(hash,0,sizeof hash);
		for(int i = 1;i <= n;i++)hash[i] = (hash[i-1]*bit+str[i]);
		KMP_INIT();
	}
	return 0;
}