[POJ2752]Seek the Name, Seek the Fame
2015-08-25 20:01
274 查看
题目链接:http://poj.org/problem?id=2752
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
本题求的是整个字符串中前后缀相同的子串,输出的是他们的长度。
通过这个题又加深了对KMP的pre数组的认识:
pre可以认为是(如pre
= k)字符串0-n中前k个字符和后k个字符相同。即前缀和后缀相同。
n != 0,我们可以认为只要pre
!= 0,那么就存在前后缀相同的子串,长度恰好是k。
(PS:“遍历”的过程有点像并查集)
PPS:注意输出要按照"increasing number"
代码如下:
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
本题求的是整个字符串中前后缀相同的子串,输出的是他们的长度。
通过这个题又加深了对KMP的pre数组的认识:
pre可以认为是(如pre
= k)字符串0-n中前k个字符和后k个字符相同。即前缀和后缀相同。
n != 0,我们可以认为只要pre
!= 0,那么就存在前后缀相同的子串,长度恰好是k。
(PS:“遍历”的过程有点像并查集)
PPS:注意输出要按照"increasing number"
代码如下:
#include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> #include <iostream> #include <cmath> #include <queue> #include <map> #include <stack> #include <list> #include <vector> using namespace std; const int maxn = 400010; int nb; char b[maxn]; int pre[maxn]; void getpre(char *b, int *pre) { int j, k; pre[0] = -1; j = 0; k = -1; while(j < nb) { if(k == -1 || b[j] == b[k]) { j++; k++; pre[j] = k; } else { k = pre[k]; } } } int main() { // freopen("in", "r", stdin); while(gets(b) && strlen(b)) { int ans[maxn]; int cnt = 0; nb = strlen(b); getpre(b, pre); while(pre[nb] != -1) { // printf("%d ", nb); ans[cnt++] = nb; nb = pre[nb]; } for(int i = cnt-1; i >= 0; i--) { printf("%d ", ans[i]); } printf("\n"); } }
相关文章推荐
- Java中的HashSet和TreeSet
- Android进程间通信(IPC)之实现细节篇(一) Binder驱动
- 超大型 LED 显示屏
- table 通过 append 动态追加的 tr,点击事件与修改样式
- findContours函数
- BZOJ1052
- Linux系统信号管理相关操作函数
- 安装Xcode主题
- c# 面相对象2-之封装性
- Matlab搜索文件夹(包含子文件夹)下文本,提取文本并存储
- 为什么会有作弊,会有为自己的家人安排工作?
- Sumdiv(数论综合模板题:快速分解因式+快速幂取模+约数和公式+递归二分求等比数列和)
- LeetCode-Decode Ways
- Android与SQLite——实现简单登录
- 磁盘管理的几个常用命令
- KVO 和 UIAlertViewController
- MySQL 幻读
- Linux服务管理/rpm的独立服务管理
- UICollectionView___新手遇到的问题:
- C#datagridview、窗体添加图标问题