就差第一个点,实在找不出错

P3435 [POI2006] OKR-Periods of Words

LZX_ssfd @ 2021-09-11 14:55:21

#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;
}

by Aoi_Erika @ 2021-09-11 17:00:19

换成scanf("%s",ch+1)读入就行了


by LZX_ssfd @ 2021-09-11 17:55:24

@Sz_zUwu 谢谢大佬


by Aoi_Erika @ 2021-09-11 17:56:13

@LZX_ssfd φ(゜▽゜*)♪


|