G - Seek the Name, Seek the Fame (kmp的数组)

The little cat is so famous, that many couples tramp over hill and dale to Byteland, and asked the little cat to give names to their newly-born babies. They seek the name, and at the same time seek the fame. In order to escape from such boring job, the innovative little cat works out an easy but fantastic algorithm: 

Step1. Connect the father's name and the mother's name, to a new string S. 
Step2. Find a proper prefix-suffix string of S (which is not only the prefix, but also the suffix of S). 

Example: Father='ala', Mother='la', we have S = 'ala'+'la' = 'alala'. Potential prefix-suffix strings of S are {'a', 'ala', 'alala'}. Given the string S, could you help the little cat to write a program to calculate the length of possible prefix-suffix strings of S? (He might thank you by giving your baby a name:) 

Input

The input contains a number of test cases. Each test case occupies a single line that contains the string S described above. 

Restrictions: Only lowercase letters may appear in the input. 1 <= Length of S <= 400000. 

Output

For each test case, output a single line with integer numbers in increasing order, denoting the possible length of the new baby's name.

Sample Input

ababcababababcabab
aaaaa

Sample Output

2 4 9 18
1 2 3 4 5
问题分析:

  读懂题后知道,这个题要算的是,给定一个字符串s,有哪些长度的s的前缀,同时也是s的后缀。

  首先明确一下前缀和后缀的概念。字符串s的前缀是指,从s的开始字符到s的任意字符为止的子串。字符串s的后缀是指,从s的任意字符到s的最后字符为止的子串。s是既是自身的前缀也是自身的后缀。

  这个问题利用KMP算法的next[]数组来解。首先对于输入的字符串s,计算其next[]数组。然后,根据next[]数组的值进行进一步的计算。

  还需要知道的是next[]数组的定义。对于字符串s的第i个字符s[i],next[i]定义为字符s[i]前面最多有多少个连续的字符和字符串s从初始位置开始的字符匹配。

  从后到前匹配前缀和后缀。如果前缀与后缀匹配,则下一个前缀与后缀匹配的字符串只能在前缀中。
代码:


//Full of love and hope for life

#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <queue>
#define inf 0x3f3f3f3f
//https://paste.ubuntu.com/

using namespace std;

char n[400010];
int nex[400010];
int m[400010];

void getnext(char a[],int x){
    int j=-1;
    nex[0]=-1;
    int i=0;
    while(i<x){
        if(j==-1||a[i]==a[j]){
            i++;
            j++;
            nex[i]=j;
        }
        else{
            j=nex[j];
        }
    }
}

/*
int kmp(char a[],char b[],int x,int y){
     getnext(b,y);
     int j=-1;
     for(int i=0;i<x;i++){
        while(j!=-1&&a[i]!=b[j+1]){
             j=nex[j];
        }
        if(a[i]==b[j+1]){
            j++;
        }
        if(j==y-1){
            ans++;
        }
    }
    return ans;
}
*/

int main(){
    //ios::sync_with_stdio(false);
    int k;
    while(cin >> n){
        k=0;
        int len=strlen(n);
        getnext(n,len);
        int t=nex[len-1];
        while(t!=-1){
            if(n[t]==n[len-1]){
                m[++k]=t+1;
            }
            t=nex[t];
        }
        for(int i=k;i>=1;i--){
            cout << m[i] << " ";
        }
        cout << len << endl;
    }
    return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ZZ --瑞 hopeACMer

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值