poj2752 Seek the Name, Seek the Fame
Description
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
Restrictions: Only lowercase letters may appear in the input. 1 <= Length of S <= 400000.
Output
Sample Input
ababcababababcabab
aaaaa
Sample Output
2 4 9 18
1 2 3 4 5 给一个串s,使它的长度为k的前缀和后缀相同,输出所有k
还是KMP……不过有点坑……原来想倒着做结果发现不对……最后灵机一动才想出做法
先求出s的next数组,然后j=n while (j){ans[++len]=j j=next[j]}这样就行了
这个还是要会到next的定义上去。
next[i]是当前这个匹配不成功的时候可以往前跳到的最长的状态。一开始j=n,然后每次求出一个j,那么j都是n往前跳到的某一个合法状态。
#include<cstdio>
#include<cstring>
char s[1000010];
int next[1000010];
int l,j;
int ans[1000010],len;
int main()
{
while (~scanf("%s",s+1))
{
l=strlen(s+1);j=0;
memset(next,0,sizeof(next));
for (int i=2;i<=l;i++)
{
while (j>0 && s[j+1]!=s[i])j=next[j];
if (s[j+1]==s[i])j++;
next[i]=j;
}
j=l;len=0;
while (j!=0)
{
ans[++len]=j;
j=next[j];
}
for (int i=len;i;i--)printf("%d ",ans[i]);
printf("\n");
}
return 0;
}
poj2752 Seek the Name, Seek the Fame的更多相关文章
- POJ2752 Seek the Name, Seek the Fame —— KMP next数组
题目链接:https://vjudge.net/problem/POJ-2752 Seek the Name, Seek the Fame Time Limit: 2000MS Memory Li ...
- poj-------------(2752)Seek the Name, Seek the Fame(kmp)
Seek the Name, Seek the Fame Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 11831 Ac ...
- poj2752 Seek the Name, Seek the Fame(next数组的运用)
题目链接:id=2752" style="color:rgb(202,0,0); text-decoration:none; font-family:Arial; font-siz ...
- POJ2752 Seek the Name, Seek the Fame 【KMP】
Seek the Name, Seek the Fame Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 11602 Ac ...
- Seek the Name, Seek the Fame (poj2752
Seek the Name, Seek the Fame Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 14561 Ac ...
- [poj2752]Seek the Name, Seek the Fame_KMP
Seek the Name, Seek the Fame poj-2752 题目大意:给出一个字符串p,求所有既是p的前缀又是p的后缀的所有字符串长度,由小到大输出. 注释:$1\le strlen( ...
- 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: 14106 Ac ...
- Seek the Name, Seek the Fame(Kmp)
Seek the Name, Seek the Fame Time Limit : 4000/2000ms (Java/Other) Memory Limit : 131072/65536K (J ...
- 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 ...
随机推荐
- 小结OC中Retain cycle(循环引用)
retain cycle 的产生 说到retain cycle,首先要提一下Objective-C的内存管理机制. 作为C语言的超集,Objective-C延续了C语言中手动管理内存的方式,但是区别于 ...
- 安装nodejs 后运行 npm 命令无响应处理方法
安装和卸载过nodejs, 也编辑过 C:\Users\{账户}\下的.npmrc文件. 再全新安装nodejs ,运行npm 命令,无响应. 处理方法,删除C:\Users\{账户}\下的.npmr ...
- Jsp中response对象的所有属性
所属接口:javax.servlet.http.HttpServletResponse,其父接口是ServletResponse,而且ServletResponse也现在只有唯一一个HttpServl ...
- 差别client、offset、scroll系列以及event的几个距离属性
element元素结点属性 一. offset系列 1.offsetWidth 和offsetHeight element.offsetWidth是一个仅仅读属性,它包含了: css width + ...
- sun.misc.BASE64Encoder是内部专用 API, 可能会在未来发行版中删除
简介 MEVAN打包遇到问题“sun.misc.BASE64Encoder是内部专用 API, 可能会在未来发行版中删除”,属于警告!项目虽然能正常运行,但是有警告就是一种隐患,要将隐患消灭在萌芽中. ...
- iPhone手机的屏幕尺寸、分辨率及适配
1.iPhone尺寸规格 设备 iPhone 宽 Width 高 Height 对角线 Diagonal 逻辑分辨率(point) Scale Factor 设备分辨率(pixel) PPI 3GS ...
- unity中数据的持久化存储
unity 提供了PlayerPrefs这个类用于存储游戏数据到电脑硬盘中. 这个类有10个函数可以使用 Class Functions类函数 SetInt Sets the value of the ...
- C#Graphics画图
public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { G ...
- 【转】【C/C++】内存分配函数:malloc,calloc,realloc,_alloca
转自:http://www.cnblogs.com/particle/archive/2012/09/01/2667034.html#commentform malloc: 原型:extern voi ...
- Floyed算法 最短路径
#include<iostream>#include<cstdio>int v,e,n; //v是顶点数,e是条数int v1[101][101],path[101][101] ...