D_FANG @ 2024-08-13 11:37:17
#include<bits/stdc++.h>
using namespace std;
int n;
long long max(long long a,long long b){
return a>b?a:b;
}
const int maxn=2e6+1e5;
char s[maxn];
long long nex[maxn];
long long ans;
int main(){
scanf("%d",&n);
scanf("%s",s);
int j=0;
for (int i=1;i<n;i++){
j=nex[i-1];
while (j>0&&s[i]!=s[j]) j=nex[j-1];
if (s[i]==s[j]) ++j;
nex[i]=j;
}
for (long long i=0;i<n;i++){
if (s[i]==s[0]){
ans=ans+i;
// printf("#%d ans=%d\n",i,ans);
continue;
}
if (2*nex[i]>=i+1){
if (nex[i]==i){
ans=ans+i;
}
else{
ans=ans+1ll*max(i+1-nex[i],nex[i]);
}
}
// printf("#%d ans=%d\n",i,ans);
}
printf("%lld",ans);
return 0;
}
没过第三个点:
输入: 10 bbcabbabbc
我的输出:25 正解:32
问是我理解题意错了吗?怎么我手推这个样例都觉得答案是25,求大佬教我语文(%%%