(KMP)Seek the Name, Seek the Fame -- poj --2752

http://poj.org/problem?id=2752

Seek the Name, Seek the Fame

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 14611   Accepted: 7320

Description

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

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<stack>
using namespace std;

#define N 1000007
#define max(a,b) (a>b?a:b)

int Next[N];
char S[N];

void FindNext(int Slen)
{
    int i=0, j=-1;
    Next[0] = -1;

    while(i<Slen)
    {
        if(j==-1 || S[i]==S[j])
            Next[++i] = ++j;
        else
            j = Next[j];
    }
}

int main()
{

    while(scanf("%s", S)!=EOF)
    {
        int Slen, n;

        Slen = strlen(S);
        n = Slen;
        FindNext(Slen);

        stack<int>Q;

        while(n)
        {
          Q.push(Next[n]);
          n = Next[n];
        }

        Q.pop();
        while(Q.size())
        {
            printf("%d ", Q.top());
            Q.pop();
        }

        printf("%d\n", Slen);
    }
    return 0;
}

时间: 2024-10-23 18:23:39

(KMP)Seek the Name, Seek the Fame -- poj --2752的相关文章

[kuangbin带你飞]专题十六 KMP &amp; 扩展KMP &amp; Manacher H - Seek the Name, Seek the Fame POJ - 2752(kmp的next数组应用)

H - Seek the Name, Seek the Fame POJ - 2752 题目链接:https://vjudge.net/contest/70325#problem/H 题目: 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. Th

HDU3336-Count the string(KMP)

Count the string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4449    Accepted Submission(s): 2094 Problem Description It is well known that AekdyCoin is good at string problems as well as n

CQU 单词替换(KMP)

单词替换(KMP) Time Limit: 500 MS Memory Limit: 64000 K Description 给出一个仅包含小写字母的字符串s,和单词A,B.把s中所有的出现过的A替换为B. Input 第一行一个数T(1<=T<=10),表示数据组数 每组数据三行,第一行为s,第二行为A,第三行为B.所有字符串仅包含小写字母 且长度小于5,000,000. Output 每组数据输出一行,替换后的字符串. Sample Input 3 aaa a b aaa aa b aba

串的模式匹配算法(KMP)

算法: #include<IOSTREAM> using namespace std; #define MAXSIZE 100 void calNext(const char *T,int *next);//T为模式串,next为预判数组 int kmp_match(const char *S,const char *T);//在主串S中寻找模式串T,如果找到返回其位置,否则返回-1.位置从0开始 void calNext(const char *T,int *next) { int n =

HDU 2594 Simpsons’ Hidden Talents (KMP)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2594 这题直接用KMP算法就可以做出来,不过我还尝试了用扩展的kmp,这题用扩展的KMP效率没那么高. KMP算法: #include<stdio.h> #include<iostream> #include<string.h> using namespace std; int next[50001]; char p[50000],s[50000]; void getnex

hdu 3336 Count the string(KMP)

一道应用kmp算法中next数组的题目 这其中vis[i]从1加到n vis[i]=[next[i]]+1; #include<string.h> #include<stdlib.h> #include<stdio.h> #include<iostream> #include<algorithm> using namespace std; char s[200005]; int b; int next[200005]; int vis[20000

poj1961 &amp; hdu 1358 Period(KMP)

poj 题目链接:http://poj.org/problem?id=1961 hdu题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1358 Description For each prefix of a given string S with N characters (each character has an ASCII code between 97 and 126, inclusive), we want to know whether

hdu 1686 Oulipo (kmp)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1686 题目大意:寻找子链在母链中出现的次数. 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 using namespace std; 5 int next[10010],sum,lens,lenc; 6 char str[10010],ch[1000010]; 7 8 int get_nex

codeforces round#259 div2 B题(KMP)

先上链接:http://codeforces.com/contest/454/problem/B B. Little Pony and Sort by Shift time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output One day, Twilight Sparkle is interested in how to sort a se

Seek the Name, Seek the Fame poj 2752

Seek the Name, Seek the Fame Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) Problem Description The little cat is so famous, that many couples tramp over hill and dale to Byteland, and asked the little cat to give n