【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 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

Source

POJ Monthly--2006.01.22,Zeyuan Zhu

【分析】

简单的应用,转个弯就过来了。

从总长度开始一直next就可以了。注意总长度也是符合条件的

 1 /*
 2 登科后
 3 唐代
 4 孟郊
 5
 6 昔日龌龊不足夸,今朝放荡思无涯。
 7 春风得意马蹄疾,一日看尽长安花。
 8 */
 9 #include <iostream>
10 #include <cstdio>
11 #include <algorithm>
12 #include <cstring>
13 #include <vector>
14 #include <utility>
15 #include <iomanip>
16 #include <string>
17 #include <cmath>
18 #include <queue>
19 #include <assert.h>
20 #include <map>
21 #include <ctime>
22 #include <cstdlib>
23 #include <stack>
24 #define LOCAL
25 const int MAXN = 400000 + 10;
26 const int INF = 100000000;
27 const int SIZE = 450;
28 const int MAXM = 1000000 + 10;
29 const int maxnode =  0x7fffffff + 10;
30 using namespace std;
31 int l1, l2;
32 char a[MAXN];
33 int next[MAXN];//不用开太大了..
34 int Ans[MAXN];
35 void getNext(){
36      //初始化next数组
37      next[1] = 0;
38      int j = 0;
39      for (int i = 2; i <= l1; i++){
40          while (j > 0 && a[j + 1] != a[i]) j = next[j];
41          if (a[j + 1] == a[i]) j++;
42          next[i] = j;
43      }
44      return;
45 }
46 /*int kmp(){
47     int j = 0, cnt = 0;
48     for (int i = 1; i <= l2; i++){
49         while (j > 0 && a[j + 1] != b[i]) j = next[j];
50         if (a[j + 1] == b[i]) j++;
51         if (j == l1){
52            cnt++;
53            j = next[j];//回到上一个匹配点
54         }
55     }
56     return cnt;
57 }*/
58
59 /*void init(){
60      scanf("%s", a + 1);
61      scanf("%s", b + 1);
62      l1 = strlen(a + 1);
63      l2 = strlen(b + 1);
64 }*/
65
66 int main(){
67     int T;
68
69     while (scanf("%s", a + 1) != EOF){
70           int tot = 0;
71           l1 = strlen(a + 1);
72           getNext();
73           int tmp = next[l1];
74           if (tmp != 0) Ans[tot++] = tmp;
75           while (next[tmp] != 0){
76                 tmp = next[tmp];
77                 Ans[tot++] = tmp;
78           }
79           for (int i = tot - 1; i >= 0; i--) printf("%d ", Ans[i]);
80           printf("%d\n", l1);
81     }
82     /*scanf("%s", a + 1);
83     l1 = strlen(a + 1);
84     getNext();
85     for (int i = 1; i <= l1; i++) printf("%d" , next[i]);*/
86     return 0;
87 }

时间: 2024-09-29 19:43:11

【POJ2752】【KMP】Seek the Name, Seek the Fame的相关文章

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

【kmp+求所有公共前后缀长度】poj 2752 Seek the Name, Seek the Fame

http://poj.org/problem?id=2752 [题意] 给定一个字符串,求这个字符串的所有公共前后缀的长度,按从小到达输出 [思路] 利用kmp的next数组,最后加上这个字符串本身 [AC] 1 #include<iostream> 2 #include<cstring> 3 #include<string> 4 #include<cstdio> 5 #include<algorithm> 6 using namespace s

poj 2752Seek the Name, Seek the Fame 【kmp】

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

poj 2752 Seek the Name, Seek the Fame【KMP算法分析记录】

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

【洛谷】3375 KMP字符串匹配

[算法]KMP [题解][算法]字符串 #include<cstdio> #include<algorithm> #include<cstring> using namespace std; const int maxn=1000010,maxm=1010; char A[maxn],B[maxm]; int p[maxm],n,m; int main() { scanf("%s%s",A+1,B+1); n=strlen(A+1);m=strlen

POJ2406 Power Strings 【KMP】

Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 31388   Accepted: 13074 Description Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc" and b = "def" then a*b = "

POJ3461 Oulipo 【KMP】

Oulipo Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 22295   Accepted: 8905 Description The French author Georges Perec (1936–1982) once wrote a book, La disparition, without the letter 'e'. He was a member of the Oulipo group. A quote

【KMP】【最小表示法】NCPC 2014 H clock pictures

题目链接: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1794 题目大意: 两个无刻度的钟面,每个上面有N根针(N<=200000),每个针都是相同的,分别指向Ai,Bi(360°被分成360000小份),问能否将其中一个旋转和另一个重合. 题目思路: [KMP][最小表示法] 循环同构问题.可以写KMP,我懒得写KMP了就写了循环同构的最小表示法. 首先将Ai排序,然后求差(记得取模360000,WA了一次),接下来复制一遍开始匹配. A

HDOJ3336 Count the string 【KMP前缀数组】+【动态规划】

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

【动态规划】【KMP】HDU 5763 Another Meaning

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5763 题目大意: T组数据,给两个字符串s1,s2(len<=100000),s2可以被解读成2种意思,问s1可以解读成几种意思(mod 1000000007). 题目思路: [动态规划][KMP] 题目有点绕,看看样例就懂了.其实不用KMP直接用substr就能做. 首先不解读成另一个意思的话,f[i]=f[i-1],接着如果当前位置能够与s2匹配,那么f[i]+=f[i-strlen(s2)]