FZU1901 Period II

题目链接:FZU1901

Q - Period II
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

Description

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: 43 6 9 10
Case #4: 2
9 12

题意:给出一个字符串,问可以看作由长度多少的子串循环得到,最后一周期可以不全。

题目分析:这题可以KMP,next数组求公共的子串,取next[len],之前求循环节的知道,len-next[len]就是最短的循环节,依次递归地求next用子串长度减就是子串的最短循环节,加上原来的子串外的那部分就是新的循环节(子串外部分也是子串的循环节)这个在纸上画一画基本都出来了。两者加在一起就是len-nexts[buf],其中buf为依次递归求nexts的值。

//
//  main.cpp
//  FZU1901
//
//  Created by teddywang on 16/5/19.
//  Copyright © 2016年 teddywang. All rights reserved.
//

#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
char s[2000100];
int nexts[2000100],sum[2000100];
int len,n,num;
void getnexts(char *s)
{
    int i=0,j=-1;
    nexts[0]=-1;
    while(i<len)
    {
        if(s[i]==s[j]||j==-1)
        {
            nexts[++i]=++j;
        }
        else j=nexts[j];
    }
}

int main()
{
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        scanf("%s",s);
        len=strlen(s);
        getnexts(s);
        num=0;
        //int buf=len-nexts[len];
        int buf=len;
        while(buf)
        {
            buf=nexts[buf];
            sum[num++]=len-buf;
        }
        //if(buf>0)
        //{
          //  for(int j=1;j*buf<len;j++)
            //    sum[num++]=j*buf;
        //}
        //sum[num++]=len;
        printf("Case #%d: %d\n",i,num);
        for(int j=0;j<num-1;j++)
            printf("%d ",sum[j]);
        printf("%d\n",sum[num-1]);
    }
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值