poj2752 Seek the Name, Seek the Fame(next数组的运用)
| Time Limit: 2000MS | Memory Limit: 65536K | |
| Total Submissions: 12018 | Accepted: 5906 |
Description
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
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
Source
题意:寻找前子串与后子串相等的子串。
代码例如以下:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
#define MAXN 1000017
int next[MAXN];
int ans[MAXN];
int len;
void getnext( char T[])
{
int i = 0, j = -1;
next[0] = -1;
while(i < len)
{
if(j == -1 || T[i] == T[j])
{
i++,j++;
next[i] = j;
}
else
j = next[j];
}
} int main()
{
char ss[MAXN];
int length;
while(~scanf("%s",ss))
{
len = strlen(ss);
getnext(ss);
int n = 0 ;int i = len;
ans[0]=len;
while(next[i] > 0 )//倒着扫描next数组
{//递归查找前子串和后子串相等的子串
i = next[i];
ans[++n] = i ;
}
for( i = n ; i >= 0 ; i--)
printf("%d ",ans[i]);
printf("\n");
}
return 0;
}
poj2752 Seek the Name, Seek the Fame(next数组的运用)的更多相关文章
- 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 【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 ...
随机推荐
- Photoshop图象切片保存为网页HTML(DIV+CSS布局)的方法
首先,制作图象切片(以一张图片为例子) 一.选择“切片”工具,在图像上拖动以分割图像(例如:一张图像切割2次就形成3个切片)切片后如下图 二.设置切片选项(如大小.目标链接.图片说明等等):选择“切片 ...
- 获取sina,baidu,google财经历史和实时股票数据接口
实时股票数据接口 股票数据的获取目前有如下两种方法可以获取:1. http/javascript接口取数据2. web-service接口1.http/javascript接口取数据1.1Sina股票 ...
- If-Modified-Since页面是否更新
第一次先请求某个网页,抓取到本地,假设文件名为 a.html.这时文件系统有个文件的修改时间. 第二次访问网页,如果发现本地已经有了 a.html,则向服务器发送一个 If-Modified-Sinc ...
- Nginx之http_image_filter_module模块使用
一.安装 #yum install gd-devel # #./configure --prefix=/usr/local/nginx \ # --with-debug \ # --with-http ...
- Apache+Django+Mysql环境配置
环境要求:Apache:2.2 Mysql:5.5 Django:1.5 python:2.7 首先下载mod_wsgi-win32-ap22py27-3.3.so 下载下来后,改名成mod_wsg ...
- [Cocos2d-x]博客推荐
推荐一下大神们的博客: JackyStudio: http://blog.csdn.net/jackyvincefu/article/category/1591201/3 老G的小屋: http:// ...
- django-admin.py失效的问题合集!
今早在命令行运行django-admin.py突然失效了.联想到昨天把Python的版本号由3.4降为2.7,Django由1.65降为1.5,能够是由于当中的修改造成的问题.网上搜了一下解决方式五花 ...
- EA强大功能之代码凝视
前面讲了EA怎样方便我们生成代码,这次讲一下,怎样生成具体的凝视. 1.文件表头凝视 (1)点击工具----选项 在常规项里改动作者: 在代码project中改动代码project的默认语言. (2) ...
- oracle 12c 中asm元数据是否有所变化
详见原文博客链接地址: oracle 12c 中asm元数据是否有所变化
- 杭州电ACM1098——Ignatius's puzzle
这个话题.简单的数学. 对于函数,f(x)=5*x^13+13*x^5+k*a*x,输入k,对于休闲x,一个数字的存在a,使f(x)是65可分. 对于休闲x. 因此,当x = 1时间,f(x) = 1 ...