1 条题解

  • 0
    @ 2025-10-8 17:04:04
    #include <bits/stdc++.h>
    using namespace std;
    const int N=1e6+10;
    char s[N];
    int pre[N];
    int main() 
    {
        int n;scanf("%d%s"&,n,s+1);
        memset(pre,0,sizeof(pre));
        for(int i=1,j=pre[i]; i<n; i++,j=pre[i]) 
        {
            while( j>0 && s[i+1]!=s[j+1]) j=pre[j];
            if(s[i+1]==s[j+1])pre[i+1]=j+1;
        }
        long long ans=0;
        for(int i=1,j; i<=n; i++) 
        {
            j=i;while(pre[j])j=pre[j];
            if(pre[i])pre[i]=j;
            ans+=i-j;
        }
        printf("%lld\n",ans);
        return 0;
    }
    
    • 1

    「POI2006 R1」串的周期 Periods of Words

    信息

    ID
    3166
    时间
    200ms
    内存
    32MiB
    难度
    6
    标签
    递交数
    54
    已通过
    16
    上传者