HDU 4763 Theme Section(KMP灵活应用)
Theme Section
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4876 Accepted Submission(s): 2439
Problem Description
It's time for music! A lot of popular musicians are invited to join us in the music festival. Each of them will play one of their representative songs. To make the programs more interesting and challenging, the hosts are going to add some constraints to the rhythm of the songs, i.e., each song is required to have a 'theme section'. The theme section shall be played at the beginning, the middle, and the end of each song. More specifically, given a theme section E, the song will be in the format of 'EAEBE', where section A and section B could have arbitrary number of notes. Note that there are 26 types of notes, denoted by lower case letters 'a' - 'z'.
To get well prepared for the festival, the hosts want to know the maximum possible length of the theme section of each song. Can you help us?
Input
The integer N in the first line denotes the total number of songs in the festival. Each of the following N lines consists of one string, indicating the notes of the i-th (1 <= i <= N) song. The length of the string will not exceed 10^6.
Output
There will be N lines in the output, where the i-th line denotes the maximum possible length of the theme section of the i-th song.
Sample Input
5
xy
abc
aaa
aaaaba
aaxoaaaaa
Sample Output
0
0
1
1
2
题意:求出在字符串前、中、后均出现的最长字符的长度
分析:利用next数组求出相同前后缀,再用kmp验证中间是否存在该前缀
#include<iostream>
#include<string.h>
using namespace std;
char p[1000001];
int nex[1000001];
int len;
void getnex(int pp)
{
int i=0,k=-1;
nex[0]=-1;
while(i<pp)
{
if(k==-1||p[i]==p[k])
nex[++i]=++k;
else
k=nex[k];
}
}
bool kmp(int st)//验证中间是否存在与前后缀匹配的字符串
{
int i=st,j=0;
int n=strlen(p);
while(i<n-st)//st~n-st区间
{
if(j==-1||p[i]==p[j])
i++,j++;
else
j=nex[j];
if(j==st)//有一段与前缀相等的
return 1;
}
return 0;
}
int main()
{
int n;
scanf("%d",&n);
while(n--)
{
scanf("%s",p);
len=strlen(p);
if(len<3)
{
printf("0\n");continue;
}
getnex(len);
int ans=0;
while(nex[len]!=-1)
{
if(nex[len]>ans&&kmp(nex[len]))
{
ans=nex[len];
break;
}
len=nex[len];
}
printf("%d\n",ans);
}
return 0;
}
HDU 4763 Theme Section(KMP灵活应用)的更多相关文章
- hdu 4763 Theme Section(KMP水题)
Theme Section Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) To ...
- HDU 4763 Theme Section(KMP+枚举公共前后缀)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4763 题目大意: 给你一个字符串s,存在一个子串E同时出现在前缀.中间.后缀,即EAEBE这种模式,A ...
- HDU 4763 Theme Section ( KMP next函数应用 )
设串为str, 串长为len. 对整个串求一遍next函数,从串结尾开始顺着next函数往前找<=len/3的最长串,假设串长为ans,由于next的性质,所以找到的串肯定满足E……E这种形式, ...
- HDU 4763 Theme Section (2013长春网络赛1005,KMP)
Theme Section Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...
- HDU 4763 Theme Section
题目: It's time for music! A lot of popular musicians are invited to join us in the music festival. Ea ...
- 2013长春网赛1005 hdu 4763 Theme Section(kmp应用)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4763 题意:给出一个字符串,问能不能在该串的前中后部找到相同的子串,输出最长的字串的长度. 分析:km ...
- HDU - 4763 Theme Section (KMP的next数组的应用)
给定一个字符串,求出一个前缀A,使得字符串的构成可以表示成ABABA的形式(B可以为空串). 输出这个前缀的最大长度. KMP算法Next数组的使用. 枚举中间的每个位置,可以根据Next数组求出这个 ...
- CF126B password&&HDU 4763 Theme Section
http://acm.hdu.edu.cn/showproblem.php?pid=4763 http://codeforces.com/problemset/problem/126/B 这两个题都是 ...
- hdu 4763 Theme Section(next数组找串中三段相等)
题意:在一个串中找 EAEBE 的形式的最长的E,其中E为一个字符串,也就是说找到前缀与后缀相同,并且串中还存在相同的一段,它们不能重复. 思路:利用next数组,next[len]代表的即是最大的相 ...
随机推荐
- 20165221 JAVA第二周学习心得及体会
基本数据类型与数组理论学习 根据第二章的网课链接,归纳出以下板块: 知识框架 标识符与关键字 1.标识符 其本质是文件名字 标识符的第一个字符不能为数字,标识符不能为关键字(如inter) 标识符不能 ...
- 10分钟轻松学会 Python turtle 绘图
python2.6版本中后引入的一个简单的绘图工具,叫做海龟绘图(Turtle Graphics),turtle库是python的内部库,使用导入即可 import turtle 先说明一下turtl ...
- js学习笔记--dom部分(一)
js 学习整理之Dom部分 前面我总结了我最近学习js基础部分,当时提到过js分了三大部分,第一部分ECMA基础也就是第一次写的基础部分, 第二部分也就是DOM部分,也就是这里要写的内容的,然后第三部 ...
- 关于CactiEZ自定义气象图的配置
作者:邓聪聪 主要目录: Weathermap主目录:/var/www/html/plugins/weathermap 图片目录(包含背景图标文件):/var/www/html/plugins/wea ...
- 【转】HashMap实现原理及源码分析
哈希表(hash table)也叫散列表,是一种非常重要的数据结构,应用场景极其丰富,许多缓存技术(比如memcached)的核心其实就是在内存中维护一张大的哈希表,而HashMap的实现原理也常常出 ...
- Alpha 冲刺 (7/10)
目录 摘要 团队部分 个人部分 摘要 队名:小白吃 组长博客:hjj 作业博客:冲刺7 团队部分 后敬甲(组长) 过去两天完成了哪些任务 界面设计.图标设计 写博客 接下来的计划 准备下周答辩 跟进进 ...
- 利用表格分页显示数据的js组件datatable的使用
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- hue的历史查询记录querys乱码问题解决
hue的历史查询记录querys乱码问题解决: master1中配置文件/home/hadoop/.versions/hue-3.10.0/desktop/conf/hue.ini 可以看到连接的是m ...
- MySQL配置说明
以下内容,来源于http://www.jb51.net/article/48082.htm [client] port = 3306 socket = /tmp/mysql.sock [mysqld] ...
- Linux VPS基础命令 - cp复制文件命令
cp命令在Linux VPS操作和应用过程中还是比较常用的,我们可以用来复制文件或者文件夹,重命名一个新的文件以及复制到其他路径中用于文件的转移. 举例用法: 1.复制root目录下的itbulu.c ...