【POJ2752】【KMP】Seek the Name, Seek the Fame
2015-03-12 20:47
369 查看
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
Sample Output
Source
POJ Monthly--2006.01.22,Zeyuan Zhu
【分析】
简单的应用,转个弯就过来了。
从总长度开始一直next就可以了。注意总长度也是符合条件的
View Code
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就可以了。注意总长度也是符合条件的
/* 登科后 唐代 孟郊 昔日龌龊不足夸,今朝放荡思无涯。 春风得意马蹄疾,一日看尽长安花。 */ #include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include <vector> #include <utility> #include <iomanip> #include <string> #include <cmath> #include <queue> #include <assert.h> #include <map> #include <ctime> #include <cstdlib> #include <stack> #define LOCAL const int MAXN = 400000 + 10; const int INF = 100000000; const int SIZE = 450; const int MAXM = 1000000 + 10; const int maxnode = 0x7fffffff + 10; using namespace std; int l1, l2; char a[MAXN]; int next[MAXN];//不用开太大了.. int Ans[MAXN]; void getNext(){ //初始化next数组 next[1] = 0; int j = 0; for (int i = 2; i <= l1; i++){ while (j > 0 && a[j + 1] != a[i]) j = next[j]; if (a[j + 1] == a[i]) j++; next[i] = j; } return; } /*int kmp(){ int j = 0, cnt = 0; for (int i = 1; i <= l2; i++){ while (j > 0 && a[j + 1] != b[i]) j = next[j]; if (a[j + 1] == b[i]) j++; if (j == l1){ cnt++; j = next[j];//回到上一个匹配点 } } return cnt; }*/ /*void init(){ scanf("%s", a + 1); scanf("%s", b + 1); l1 = strlen(a + 1); l2 = strlen(b + 1); }*/ int main(){ int T; while (scanf("%s", a + 1) != EOF){ int tot = 0; l1 = strlen(a + 1); getNext(); int tmp = next[l1]; if (tmp != 0) Ans[tot++] = tmp; while (next[tmp] != 0){ tmp = next[tmp]; Ans[tot++] = tmp; } for (int i = tot - 1; i >= 0; i--) printf("%d ", Ans[i]); printf("%d\n", l1); } /*scanf("%s", a + 1); l1 = strlen(a + 1); getNext(); for (int i = 1; i <= l1; i++) printf("%d" , next[i]);*/ return 0; }
View Code
相关文章推荐
- POJ2752 Seek the Name, Seek the Fame 【KMP】
- [poj2752]Seek the Name, Seek the Fame(KMP)
- 【POJ2752】Seek the Name, Seek the Fame-KMP思想
- 【poj2752】Seek the Name, Seek the Fame kmp
- 【poj2752】Seek the Name, Seek the Fame KMP
- POJ2752 Seek the Name, Seek the Fame(kmp)
- POJ2752 Seek the Name, Seek the Fame 【KMP】
- Seek the Name, Seek the Fame---poj2752(kmp中的Next数组)
- [poj2752]Seek the Name, Seek the Fame_KMP
- 【KMP】POJ2752 Seek the Name, Seek the Fame
- 【POJ2752】Seek the Name, Seek the Fame(KMP)
- poj2752 Seek the Name, Seek the Fame(kmp)
- 【kmp+求所有公共前后缀长度】poj 2752 Seek the Name, Seek the Fame
- Seek the Name, Seek the Fame POJ - 2752 (KMP next数组 的 其他用法)
- poj2752_Seek the Name,Seek the Fame_KMP(next数组定义的应用)
- poj 2752 Seek the Name, Seek the Fame(KMP)
- POJ - 2752 Seek the Name, Seek the Fame(KMP中对next数组的前缀后缀匹配应用)
- poj 2752 Seek the Name, Seek the Fame(KMP应用)
- POJ 2752 Seek the Name, Seek the Fame(KMP)
- |poj 2752|KMP|Seek the Name, Seek the Fame