kmp一道唬人题

P - Period II
描述
For each prefix with length P of a given string S,if
S[i]=S[i+P] for i in [0…SIZE(S)-p-1],

then the prefix is a “period” of S. We want to all the periodic prefixs.

Input
Input contains multiple cases.
The first line contains an integer T representing the number of cases. Then following T cases.

Each test case contains a string S (1 <= SIZE(S) <= 1000000),represents the title.S consists of lowercase ,uppercase letter.

Output
For each test case, first output one line containing “Case #x: y”, where x is the case number (starting from 1) and y is the number of periodic prefixs.Then output the lengths of the periodic prefixs in ascending order.
Sample Input
4
ooo
acmacmacmacmacma
fzufzufzuf
stostootssto
Sample Output
Case #1: 3
1 2 3
Case #2: 6
3 6 9 12 15 16
Case #3: 4
3 6 9 10
Case #4: 2
9 12
水题但是有一点不太好理解 P的意思 特别是最后一个长度p原长(例4)a[0]=?a[p] p=12的时候显然a[12]就超出了 所以无需较劲 这是出的题的问题其实就考察kmp。。。。。其实应该把这种情况阐述一下 不说了,要么就是我理解的错。。。。。最后这种原长 P 应该可以理解成整个作为前缀虽然没有与之相等的或者直接与自己相等但不会对照题中的a[0]==a[p] 。。害 , 我太菜了才会出现这理解,,,,,。。·····
表示的是字符串前缀和后缀相同时相差的长度 例如acm那个串 第一个字符a与最后一个字符a相等 然后相差15,,,,也就是w-f[len] :字符串长度-前缀后缀相等的长度即可然后递归 len=f[len]; 深度表示(前缀和后缀相等)的(前缀的).((前缀和后缀))相等的部分和(后缀的).((前缀和后缀))相等的部分相等 也就是前缀的前缀····与后缀的后缀·····相等,,,,,哇 我感觉真的很清晰了 图都出来了 画面有了 感谢 自己 呜呜呜。。。

#include <map>
#include <set>
#include <list>
#include <queue>
#include <deque>
#include <cmath>
#include <stack>
#include <vector>
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
char a[2000005];
int  f[2000005],exf[2000005];
int pp[2000000];
void fc(int l)
{
    int j=-1;
    int i=0;
    f[0]=-1;
    while(i<l)
    {
        if(j==-1||a[j]==a[i])
        {
            f[++i]=++j;
        }
        else
        {
            j=f[j];
        }

    }

}
int main()
{
    int n;
    scanf("%d",&n);
     int ans=1;
    while(n--)
    {
        memset(pp,0,sizeof(pp));

        int sum=0;
        scanf("%s",a);

        int w=strlen(a);
        int len=w;
        fc(w);
        while(f[len]>=0)
        {
            pp[sum++]=w-f[len];
            len=f[len];
        }
        printf("Case #%d: %d\n",ans,sum);
        ans++;
        for(int i=0;i<sum;i++)
        {
            if(i==0)
            {
                printf("%d",pp[i]);
            }
            else  printf(" %d",pp[i]);
        }
        printf("\n");
        memset(f,0,sizeof(f));
        memset(a,0,sizeof(a));

    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值