POJ - 2752 Seek the Name, Seek the Fame (KMP的next[]应用)
来源:互联网 发布:cms网站系统 编辑:程序博客网 时间:2024/05/16 16:56
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:)
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.
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
ababcababababcababaaaaa
Sample Output
2 4 9 181 2 3 4 5题意:求既匹配字符串前缀又匹配后缀的字符串的长度思路:其实next[]数组就表示的是最长的前缀和后缀匹配,那么只要next[]数组的值不为0的话,就代表有前后缀匹配,递归下去,当然整个字符串也符合条件#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int maxn = 400010;char pattern[maxn];int next[maxn], flag;void getNext() {int m = strlen(pattern);next[0] = next[1] = 0;for (int i = 1; i < m; i++) {int j = next[i];while (j && pattern[i] != pattern[j]) j = next[j];next[i+1] = pattern[i] == pattern[j] ? j+1 : 0;}}void dfs(int len) {if (len == 0)return;dfs(next[len]);if (!flag) {printf("%d", len);flag = 1;}else printf(" %d", len);}int main() {while (scanf("%s", pattern) != EOF) {getNext();int len = strlen(pattern);flag = 0;dfs(len);printf("\n");}return 0;}
0 0
- POJ - 2752 Seek the Name, Seek the Fame (KMP的next[]应用)
- POJ 2752 Seek the Name, Seek the Fame(KMP,next数组的应用)
- Seek the Name, Seek the Fame POJ 2752【KMP next数组的应用】
- POJ-2752 Seek the Name, Seek the Fame(kmp中next数组的应用)
- POJ 2752: Seek the Name, Seek the Fame(简单KMP-NEXT数组的应用)
- POJ 2752 Seek the Name, Seek the Fame(KMP next数组的应用)
- POJ 2752 Seek the Name, Seek the Fame KMP Next[]
- POJ 2752 Seek the Name, Seek the Fame(KMP next数组应用)
- POJ 2752 Seek the Name, Seek the Fame kmp失配函数next应用
- POJ 2752 Seek the Name, Seek the Fame(KMP的next数组)
- POJ - 2752 Seek the Name, Seek the Fame(KMP next数组的理解)
- POJ 2752 Seek the Name, Seek the Fame(KMP+next数组的运用)
- POJ 2752-Seek the Name, Seek the Fame(KMP的next数组运用)
- POJ 2752 Seek the Name, Seek the Fame(next数组的应用)
- POJ 2752--Seek the Name, Seek the Fame (next)
- Seek the Name, Seek the Fame next数组的应用
- poj 2752 Seek the Name, Seek the Fame(KMP)
- poj 2752 Seek the Name, Seek the Fame---kmp
- 网狐子游戏二人牛牛机器人不起作用的解决办法
- Fibonacci Again
- extjs 的正则表达式的引用
- java读取EXCEL文件
- linux下,如何查看mysql的版本
- POJ - 2752 Seek the Name, Seek the Fame (KMP的next[]应用)
- 输出学生信息(在Friend类中定义输出和输入)
- 向Windows内核驱动中传递一个函数指针,使其在内核模式下执行
- 在linux下mount windows硬盘
- 用UCenter Home 的《记账本》插件为UCH制作插件
- 常用颜色值表
- arp&icmp
- 设计模式---单例模式
- mysql编码设置