POJ:2753-Seek the Name, Seek the Fame
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 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
题意就是问字符串s中有多少个长度不同的相同的前缀和后缀子串。
就是考察的一个KMP中的next数组,考察相同的前缀和后缀就是next数组,然后next数组跳转就可以了。
next[i]的意义就是:前面长度为i的字串的【前缀和后缀的最大匹配长度】
#include<stdio.h>
#include<cstring>
#include<vector>
using namespace std;
const int maxn = 4e5+100;
char s[maxn];
int next[maxn];
void cal_next()
{
int k = -1;
next[0] = -1;
int n = strlen(s);
for(int i=1;i<n;i++)
{
while(k>-1 && s[i] != s[k+1])
k = next[k];
if(s[i] == s[k+1])
k++;
next[i] = k;
}
}
int main()
{
while(scanf("%s",s) != EOF)
{
cal_next();
int len = strlen(s);
vector<int>ve;
int k = len-1;
ve.push_back(len);
while(next[k] != -1)
{
ve.push_back(next[k]+1);
k = next[k];
}
int n = ve.size();
for(int i=n-1;i>=0;i--)
{
printf("%d",ve[i]);
if(i != 0)
printf(" ");
}
printf("\n");
}
return 0;
}
POJ:2753-Seek the Name, Seek the Fame的更多相关文章
- POJ 2752:Seek the Name, Seek the Fame
Seek the Name, Seek the Fame Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13619 Accept ...
- POJ 2751:Seek the Name, Seek the Fame(Hash)
Seek the Name, Seek the Fame Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 24077 Ac ...
- POJ 2752 Seek the Name, Seek the Fame [kmp]
Seek the Name, Seek the Fame Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 17898 Ac ...
- poj 2752 Seek the Name, Seek the Fame(KMP需转换下思想)
Seek the Name, Seek the Fame Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10204 Ac ...
- KMP POJ 2752 Seek the Name, Seek the Fame
题目传送门 /* 题意:求出一个串的前缀与后缀相同的字串的长度 KMP:nex[]就有这样的性质,倒过来输出就行了 */ /************************************** ...
- poj 2752 Seek the Name, Seek the Fame【KMP算法分析记录】【求前后缀相同的子串的长度】
Seek the Name, Seek the Fame Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 14106 Ac ...
- POJ 2752 Seek the Name, Seek the Fame(next数组运用)
Seek the Name, Seek the Fame Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 24000 ...
- POJ 2752 Seek the Name,Seek the Fame(KMP,前缀与后缀相等)
Seek the Name,Seek the Fame 过了个年,缓了这么多天终于开始刷题了,好颓废~(-.-)~ 我发现在家真的很难去学习,因为你还要陪父母,干活,做家务等等 但是还是不能浪费时间啊 ...
- (KMP)Seek the Name, Seek the Fame -- poj --2752
http://poj.org/problem?id=2752 Seek the Name, Seek the Fame Time Limit: 2000MS Memory Limit: 65536 ...
随机推荐
- 在 Java 的多线程中,如何去判断给定的一个类是否是线程安全的(另外:synchronized 同步是否就一定能保证该类是线程安全的。)
同步代码块和同步方法的区别:同步代码块可以传入任意对象,同步方法中 如果多个线程检查的都是一个新的对象,不同的同步锁对不同的线程不具有排他性,不能实现线程同步的效果,这时候线程同步就失效了. 两者的区 ...
- 部署iis服务器与c#程序遇到的问题小结
记得上次部署IIS服务器比较顺利,半天搞定的?有点忘了. 但,服务器版本各有不同,这次装的是server2008 R2 Ennterprice版.虽然忘了上次装的是哪个版本,但进去后发现有些东西明显不 ...
- axios 访问和返回拦截,token处理,返回异常统一处理
在axios文件夹中,index.js添加拦截 访问拦截: import store from '../store' axios.interceptors.request.use( config =& ...
- SSAS中处理时经常出现的几种错误
1.[重复属性键的错误]"处理时找到重复的属性键" 当在[属性关系]里指定属性关系后, 就出出现此错误,因为不知道Month值“1”,并不知道是属于哪一年的, 所以需要 ...
- APPCLOUD 判断网络
if(api.connectionType == 'none'){ api.toast({ msg : '当前网络不可用,请检查网络设置', duration : , location : 'midd ...
- Linux系统下安装rz/sz命令及使用说明(文件上传下载)
1.安装软件 yum -y install lrzsz 2.本地文件上传命令 rz -bye 执行该命令后,在弹出框中选择要上传的文件即可 3.下载文件到本地 sz filename
- Linux 环境下安装 Nginx+php+mysql 开发环境
一直以来都没有养成记录学习的好习惯,我想从这么一天开始,把自己学习和工作中的经验和坑都记录下来.等到以后老的时候还有可以回忆的地方. 最近在学习linux,虽然已经玩linux很久了,但是没有怎么用心 ...
- I/O————缓存流
为什么要使用缓存流? 当对磁盘访问次数多的时候,字节流和字符流就会感觉性能不佳,速度较慢. 缓存流,一次会读取很多的数据到缓存中,以后每一次读取都是从缓存中读取,直到缓存中数据读取完,这样就减少了io ...
- 【algorithm】二叉树的遍历
二叉树的遍历 二叉树用例 代码解析: public class BinaryTree { static class TreeNode { Integer val; TreeNode left; Tre ...
- 正则表达说明—Pattern API
字符类 [abc] 匹配a.b.c任意一个字符 [^abc] 匹配除了a.b.c外的任意一个字符 [a-zA-Z] 匹配a-z或A ...