比赛 4043级NOIP2022欢乐赛7th 评测结果 AAAAATTTTTTATTA
题目名称 Prefixuffix 最终得分 46
用户昵称 op_组撒头屯 运行时间 8.274 s
代码语言 C++ 内存使用 17.56 MiB
提交时间 2022-11-20 10:38:57
显示代码纯文本
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1000000+5;
const ll p=131;
const ll mod1=1000000007;
const ll mod2=1145141;
struct Int{
	ll val1,val2;
	Int operator+(const ll&x){
		return {(val1+x)%mod1,(val2+x)%mod2};
	}
	Int operator-(const Int&x){
		return {((val1-x.val1)%mod1+mod1)%mod1,((val2-x.val2)%mod2+mod2)%mod2};
	}
	Int operator*(const ll&x){
		return {val1*x%mod1,val2*x%mod2};
	}
	bool operator==(const Int&x){
		return (val1==x.val1)&&(val2==x.val2);
	}
}pw[N];
int n;
char s[N];
bool check(int x){
	Int h1={0,0},h2={0,0};
	for (int i=1;i<=x;i++)h1=h1*p+s[i];
	for (int i=n-x+1;i<=n;i++)h2=h2*p+s[i];
	if (h1==h2)return true;
	for (int i=1;i<x;i++){
		h1=(h1-pw[x-1]*s[i])*p+s[i];
		if (h1==h2)return true;
	}
	return false;
}
int main(){
	freopen ("prefixuffix.in","r",stdin);
	freopen ("prefixuffix.out","w",stdout);
	scanf("%d",&n);
	pw[0]={1,1};
	for (int i=1;i<=n;i++)pw[i]=pw[i-1]*p;
	scanf("%s",s+1);
	for (int i=n/2;i>=1;i--){
		if (check(i)){
			printf("%d\n",i);return 0;
		}
	}
	printf("0\n");
	return 0;
}