POJ2752(KMP)

Seek the Name, Seek the Fame

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 17175   Accepted: 8776

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
 1 //2016.8.17
 2 #include<iostream>
 3 #include<cstdio>
 4 #include<cstring>
 5
 6 using namespace std;
 7
 8 const int N = 400005;
 9 char s[N];
10 int tot, ans[N], nex[N];
11
12 void getNext(int n)
13 {
14     nex[0] = -1;
15     for(int i = 0, fail = -1; i < n;)
16     {
17         if(fail==-1||s[i]==s[fail])
18         {
19             i++, fail++;
20             nex[i] = fail;
21         }else fail = nex[fail];
22     }
23 }
24
25 int main()
26 {
27     while(scanf("%s", s)!=EOF)
28     {
29         int n = strlen(s);
30         getNext(n);
31         for(int i = 0; i < n; i++)
32               cout<<nex[i]<<" ";
33         cout<<endl;
34         tot = 0;
35         int p = nex[n-1];
36         while(p!=-1)
37         {
38             if(s[p] == s[n-1])
39               ans[tot++] = p+1;
40             p = nex[p];
41         }
42         for(int i = tot-1; i >= 0; i--)
43               printf("%d ", ans[i]);
44         printf("%d\n", n);
45     }
46
47     return 0;
48 }
时间: 2024-10-26 01:52:58

POJ2752(KMP)的相关文章

POJ-2752(KMP算法+前缀数组的应用)

Seek the Name, Seek the Fame POJ-2752 本题使用的算法还是KMP 最主要的片段就是前缀数组pi的理解,这里要求解的纸盒pi[n-1]有关,但是还是需要使用一个循环来依次找到前面符合的前缀(所谓符合就是可以保持既是前缀也是s的后缀的子串长度). #include<iostream> #include<cstdio> #include<string> #include<algorithm> #include<set>

poj2752:KMP的简单应用

Seek the Name, Seek the Fame Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 16388 Accepted: 8330 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 th

【POJ2752】【KMP】Seek the Name, Seek the Fame

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 b

POJ2752 Seek the Name, Seek the Fame【KMP】

题目链接: http://poj.org/problem?id=2752 题目大意: 给定一个字符串S,计算出所有可能的前缀-后缀字符串的长度.前缀-后缀字符串指的是S的 子串不仅是S的前缀,还是S的后缀.比如S = "alala",前缀-后缀字符有{"a","ala","alala"}. 思路: KMP算法的应用.在KMP算法中,当字符串匹配失败时,模式串的指针并没有指向0从头比 较,而是指向了一个特定的位置,因为这个Nex

POJ2752题解——KMP入门

题目链接:http://poj.org/problem?id=2752 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 ord

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

大致题意: 给出一个字符串str,求出str中存在多少子串,使得这些子串既是str的前缀,又是str的后缀.从小到大依次输出这些子串的长度. next的简单运用,递归打印next的值就好 //Memory: 3656 KB Time: 141 MS #include<cstdio> #include<iostream> #include<cstring> #define maxn 400100 using namespace std; char str[maxn]; i

POJ2752 (Seek the Name, Seek the Fame,kmp)

传送门 Seek the Name, Seek the Fame Time Limit: 2000MS   Memory Limit: 65536K       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

POJ2752 Seek the Name, Seek the Fame (kmp) 题解

Seek the Name, Seek the Fame Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 17094   Accepted: 8720 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 t

POJ2752 Seek the Name, Seek the Fame 【KMP】

Seek the Name, Seek the Fame Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 11602   Accepted: 5680 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 t