1 条题解

  • 0
    @ 2025-10-8 16:52:19
    #include <bits/stdc++.h>
    using namespace std;
    const int N=410000;
    char s[N];
    int pre[N], a[N];
    int main() 
    {
        while(scanf("%s", s+1)!=EOF) 
    	{
            int len= strlen(s+1);
            memset(pre, 0, sizeof(pre));
            for(int i=1, j=pre[i]; i<len; 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;
            }
            
            int x=len, an=0;
            while(pre[x]>0) 
    		{
                a[++an]=pre[x];
                x=pre[x];
            }
            for(int i=an; i>=1; i--) printf("%d ", a[i]);
            printf("%d\n", len);
        }
        return 0;
    }
    
    • 1

    *【KMP】所有"前缀等于后缀"的情况[POJ2752]

    信息

    ID
    575
    时间
    2000ms
    内存
    128MiB
    难度
    4
    标签
    递交数
    94
    已通过
    42
    上传者