就差第一个点,实在找不出错
查看原帖
就差第一个点,实在找不出错
289436
LZX_ssfd楼主2021/9/11 14:55
#include <cstdio>
using namespace std;
const int MAXL=1000005;
int l;
long long ans;
char ch[MAXL];
int next[MAXL];
inline int read() {
	int res=0,f=1;
	char ch=getchar();
	while(ch<'0'||ch>'9') {
		if(ch=='-') f=-1;
		ch=getchar();
	}
	while(ch>='0'&&ch<='9') res=res*10+ch-'0',ch=getchar();
	return res*f;
}
signed main() {
	l=read();
	for(register int i=1; i<=l; ++i)
		ch[i]=getchar();
	next[1]=0;
	for(register int i=2,j=0; i<=l; ++i) {
		while(j&&ch[i]!=ch[j+1]) j=next[j];
		if(ch[i]==ch[j+1]) j++;
		next[i]=j;
	}
	for(register int i=1; i<=l; ++i) {
		int t=i;
		while(next[t]) t=next[t];
		if(next[i]) next[i]=t;
		ans+=i-t;
	}
	printf("%lld\n",ans);
	return 0;
}
2021/9/11 14:55
加载中...