poj 2752 Seek the Name, Seek the Fame(KMP需转换下思想)
|
Seek the Name, Seek the Fame
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. 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 Sample Output 2 4 9 18 Source POJ Monthly--2006.01.22,Zeyuan Zhu
|
题意:
给你一个字符串。要你找出既是前缀又是后缀的子串长度的可能值。按字典序输出。
思路:
转化下思路其实很简单。既然既是前缀又是后缀。直接用文本串构造一个失配数组。然后直接匹配文本串的文本末。即txt[n].n=strlen(n)。而长度就为f[j]。想一想就明白了。感觉思想还是蛮好的。
详细见代码:
#include <iostream>
#include<string.h>
#include<stdio.h>
using namespace std;
char txt[400100];
int f[400100],ans[400100],cnt;
void getf(char *p)//得到失配数组
{
int i,j,m=strlen(p);
f[0]=f[1]=0;
for(i=1;i<m;i++)
{
j=f[i];
while(j&&p[j]!=p[i])
j=f[j];
f[i+1]=p[j]==p[i]?j+1:0;
}
}
int main()
{
int len,i,j; while(~scanf("%s",txt))
{
getf(txt);
len=strlen(txt);
cnt=0;
j=len;
while(f[j])
{
ans[cnt++]=f[j];
j=f[j];
}
for(i=cnt-1;i>=0;i--)
printf("%d ",ans[i]);
printf("%d\n",len);//注意自己是自己的前后缀
}
return 0;
}
poj 2752 Seek the Name, Seek the Fame(KMP需转换下思想)的更多相关文章
- (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 ...
- Seek the Name, Seek the Fame POJ - 2752
Seek the Name, Seek the Fame POJ - 2752 http://972169909-qq-com.iteye.com/blog/1071548 (kmp的next的简单应 ...
- 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: 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 ...
- 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 过了个年,缓了这么多天终于开始刷题了,好颓废~(-.-)~ 我发现在家真的很难去学习,因为你还要陪父母,干活,做家务等等 但是还是不能浪费时间啊 ...
- poj 2752 Seek the Name, Seek the Fame (KMP纯模版)
Seek the Name, Seek the Fame Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13840 Ac ...
- POJ 2752:Seek the Name, Seek the Fame
Seek the Name, Seek the Fame Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13619 Accept ...
随机推荐
- 用ahk脚本自己主动删除flashcookies
手动方法(请戳点击打开链接): 点击桌面左下脚的"開始"键 打开"控制面板" 并点击 "flash player" 项 进入 并点击&quo ...
- zoj 2256 Mincost
#include<stdio.h> int main(void) { int kil; ; double sum; ) { sum=; flag=; while(kil) { ) { su ...
- NSLog 输出文件名、方法名、行号
项目中经常会需要根据日志输出来寻找源代码,通过以下方法可以让它自动输出文件名.方法.行号,非常方便. 找到项目的pch文件,添加以下内容即可: ...为三个英文句号(复制粘贴后可能会变化). /** ...
- Problem A: 走迷宫问题
Problem A: 走迷宫问题Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 9 Solved: 3[Submit][Status][Web Board] ...
- BZOJ 1296: [SCOI2009]粉刷匠( dp )
dp[ i ][ j ] = max( dp[ i - 1 ][ k ] + w[ i ][ j - k ] ) ( 0 <= k <= j ) 表示前 i 行用了 j 次粉刷的机会能正 ...
- MVC数据提交
关于请求方式(form表单) .form的几个属性 <form name="input" action="http://www.baidu.com" me ...
- 大数据情报分析公司Palantir
最近在学习图数据计算方面技术,在寻找现实应用时发现美国Palantir公司已将所谓的多源异构数据融合分析技术运用的炉火纯青.Palantir创立于2004年,最早是因PayPal公司为保障支付安全而逐 ...
- Python正则表达式一: 基本使用方法
学习python的正则表达式,主要有两个方面学习: 第一,学习如何写正则表达式,主要是掌握其语法规范.正则表达式的语法规范是通用的,对各种开发语言都是一致的. 第二,学习如何使用正则表达式,也就是掌握 ...
- 打包mysql、tomcat、jdk为一个软件
打包mysql.tomcat.jdk为一个软件 博客分类: 成长中的点滴 . 我们在本地开发web应用的时候,直接在IDE里面就可以完成jdk.容器.数据库的配置和集成. 但是如果当我们把应用程序交 ...
- php 实现简单的登录
//登录页面: V层前端模板: Administrator@QCEE61NZ66FEX2D /cygdrive/c/wamp/www/thinkphp/Home/Tpl/Login $ ls inde ...