POJ2752 Seek the Name, Seek the Fame 【KMP】
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 11602 | Accepted: 5680 |
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
题意:给定一个串。求它的子串的长度,子串满足既是主串的前缀。又是主串的后缀,将全部满足要求的子串长度依照升序输出。
题解:next数组的简单运用,(可是读懂题意用了好久啊。
。
)求出next数组后递归输出对应的后缀长度,len要单独输出。
#include <stdio.h>
#define maxn 400002 char str[maxn];
int next[maxn], len; void getNext()
{
int i = 0, j = -1;
next[0] = -1;
while(str[i]){
if(j == -1 || str[i] == str[j]){
++i; ++j;
next[i] = j; //mode 1
}else j = next[j];
}
len = i;
} void getVal(int n)
{
if(next[n] == 0) return;
getVal(next[n]);
printf("%d ", next[n]);
} int main()
{
//freopen("stdin.txt", "r", stdin);
while(scanf("%s", str) == 1){
getNext();
getVal(len);
printf("%d\n", len);
}
return 0;
}
POJ2752 Seek the Name, Seek the Fame 【KMP】的更多相关文章
- poj2752seek the name, seek the fame【kmp】
The little cat is so famous, that many couples tramp over hill and dale to Byteland, and asked the l ...
- POJ_2752 Seek the Name, Seek the Fame 【KMP】
一.题目 POJ2752 二.分析 比较明显的KMP运用. 但是这题不是只找一个,仔细看题后可以发现相当于是在找到最大的满足条件的后缀后,再在这个后缀里面找满足条件的后缀. 可以不断的运用KMP得出答 ...
- POJ--2752--Seek the Name, Seek the Fame【KMP】
链接:http://poj.org/problem? id=2752 题意:对于一个字符串S,可能存在前n个字符等于后n个字符,从小到大输出这些n值. 思路:这道题加深了对next数组的理解.next ...
- 【KMP】【最小表示法】NCPC 2014 H clock pictures
题目链接: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1794 题目大意: 两个无刻度的钟面,每个上面有N根针(N<=200000),每个 ...
- 【动态规划】【KMP】HDU 5763 Another Meaning
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5763 题目大意: T组数据,给两个字符串s1,s2(len<=100000),s2可以被解读成 ...
- HDOJ 2203 亲和串 【KMP】
HDOJ 2203 亲和串 [KMP] Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- 【KMP】Censoring
[KMP]Censoring 题目描述 Farmer John has purchased a subscription to Good Hooveskeeping magazine for his ...
- 【KMP】OKR-Periods of Words
[KMP]OKR-Periods of Words 题目描述 串是有限个小写字符的序列,特别的,一个空序列也可以是一个串.一个串P是串A的前缀,当且仅当存在串B,使得A=PB.如果P≠A并且P不是一个 ...
- 【KMP】Radio Transmission
问题 L: [KMP]Radio Transmission 题目描述 给你一个字符串,它是由某个字符串不断自我连接形成的.但是这个字符串是不确定的,现在只想知道它的最短长度是多少. 输入 第一行给出字 ...
随机推荐
- 数组排序 sort
数组排序 this.dataShow = this.data.sort((a, b) => { return parseInt(a[this.innerOrderBy]) - parseInt( ...
- SQL Server数据库的除法默认向下取整,要返回小数的解决方法
num1; / 1000.0 num2; * 1.0 num3; num4; 结果:
- Java开发者写SQL时常犯的10个错误
首页 所有文章 资讯 Web 架构 基础技术 书籍 教程 我要投稿 更多频道 » - 导航条 - 首页 所有文章 资讯 Web 架构 基础技术 书籍 教程 我要投稿 更多频道 » - iOS ...
- POJ-1002-487-3279(字符串)
487-3279 Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 309685 Accepted: 55292 Descripti ...
- “打开ftp服务器上的文件夹时发生错误,请检查是否有权限访问该文件夹"
阿里云虚拟主机上传网站程序 问题场景:网页制作完成后,程序需上传至虚拟主机 注意事项: 1.Windows系统的主机请将全部网页文件直接上传到FTP根目录,即 / . 2. 如果网页文件较多,上传较慢 ...
- 深入Linux内核架构——进程管理和调度(上)
如果系统只有一个处理器,那么给定时刻只有一个程序可以运行.在多处理器系统中,真正并行运行的进程数目取决于物理CPU的数目.内核和处理器建立了多任务的错觉,是通过以很短的间隔在系统运行的应用程序之间不停 ...
- soc desgin 目前需要做的事情
1.熟练的画时序图 达到一旦有想法可以立即通过时序图表达出来. 2.下面是项目中经常用到的典型设计 2.1串并互相转换 2.2cdc 2.3握手协议 2.4cgc(门控时钟) 2.5AHB2reg文件 ...
- VMware搭建内网并通过iptables端口转发联网
整体流程图 配置Server1 新建两块网卡 一块网卡设置为桥接模式,另外一块设置为仅主机模式 查看两块网卡配置 root@ubuntu:~# ifconfig ens33 Link encap:Et ...
- Rust所有权语义模型
编程语言的内存管理,大概可以分为自动和手动两种. 自动管理就是用 GC(垃圾回收)来自动管理内存,像 Java.Ruby.Golang.Elixir 等语言都依赖于 GC.而 C/C++ 却是依赖于手 ...
- MySQL存储过程中一直困扰的 の 变量中的@
在声明变量中CREATE function Get_StrArrayLength ( @str varchar(1024), --要分割的字符串@split varchar(10) --分隔符号)re ...