地址:http://acm.hdu.edu.cn/showproblem.php?pid=4763

题目:

Theme Section

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3491    Accepted Submission(s): 1623

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
 
Source
 
思路:用所给字符串去匹配字符串EAEBE,AB可以为空
  前缀为E,后缀为E,所以很容易想到kmp的next数组(前缀和后缀的匹配情况),然后查找所有所有前缀匹配后缀的情况然后check一下即可。
  查找所有前缀和后缀匹配的情况时可以利用next进行转移。
  ans=next[ans-1]。
 #include <bits/stdc++.h>
#define PB push_back
#define MP make_pair
using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
#define PI acos((double)-1)
#define E exp(double(1))
const int K=1e6+;
int nt[K];
char a[K]; //参数为模板串和next数组
//字符串均从下标0开始
void kmp_next(char *T,int *nt)
{
nt[]=;
for(int i=,j=,m=strlen(T);i<m;i++)
{
while(j&&T[i]!=T[j])j=nt[j-];
if(T[i]==T[j])j++;
nt[i]=j;
}
} int main(void)
{
int t;cin>>t;
while(t--)
{
scanf("%s",a);
kmp_next(a,nt);
int len=strlen(a);
int ans=nt[len-],ff=;
while(ans)
{
for(int i=ans;i<=len-ans&&ff;i++)
if(nt[i]==ans)
ff=;
if(!ff) break;
ans=nt[ans-];
}
printf("%d\n",ans);
} return ;
}

hdu4763 Theme Section的更多相关文章

  1. HDU4763 Theme Section —— KMP next数组

    题目链接:https://vjudge.net/problem/HDU-4763 Theme Section Time Limit: 2000/1000 MS (Java/Others)    Mem ...

  2. hdu4763 Theme Section【next数组应用】

    Theme Section Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

  3. HDU4763 Theme Section 【KMP】

    Theme Section Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) To ...

  4. HDU4763 - Theme Section(KMP)

    题目描述 给定一个字符串S,要求你找到一个最长的子串,它既是S的前缀,也是S的后缀,并且在S的内部也出现过(非端点) 题解 CF原题不解释....http://codeforces.com/probl ...

  5. HDU-4763 Theme Section KMP

    题意:求最长的子串E,使母串满足EAEBE的形式,A.B可以任意,并且不能重叠. 题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=4763 思 ...

  6. 【kmp算法】hdu4763 Theme Section

    kmp中next数组的含义是:next[i]表示对于s[0]~s[i-1]这个前缀而言,最大相等的前后缀的长度是多少.规定next[0]=-1. 迭代for(int i=next[i];i!=-1;i ...

  7. Theme Section(KMP应用 HDU4763)

    Theme Section Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...

  8. hdu 4763 Theme Section(KMP水题)

    Theme Section Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) To ...

  9. HDU 4763 Theme Section

    题目: It's time for music! A lot of popular musicians are invited to join us in the music festival. Ea ...

随机推荐

  1. Docker 如何把镜像上传到docker hub

    1 首先你得准备一个hub 的帐号, 去 https://hub.docker.com 注册吧! 2 在hub那里新建一个仓库, 这个就类似于github那边的..create ---> cre ...

  2. iOS开发之 -- NSStringFromSelector的使用

    很多时候,我们要触发一个时间,需要设置点击时间,当然了,有很多,比如:按钮,手势,tableview和其他一些空间自带的点击方法, 还有一个就是NSStringFromSelector的使用,废话不多 ...

  3. Docker(1)在CentOS上的安装与卸载

     一. Docker的安装 CentOS7 上安装: 1. 卸载旧版本 $ sudo yum remove docker \ docker-client \ docker-client-latest ...

  4. Linux命令之乐--md5sum

    md5sum命令用于生成和校验文件的md5值.它会逐位对文件的内容进行校验,它常用于检查文件的完整性. 读取文件的MD5值 [root@new ~]# md5sum /usr/local/sbin/* ...

  5. 【黑金原创教程】【Modelsim】【第五章】仿真就是人生

    声明:本文为黑金动力社区(http://www.heijin.org)原创教程,如需转载请注明出处,谢谢! 黑金动力社区2013年原创教程连载计划: http://www.cnblogs.com/al ...

  6. java中计算时间差

    Calendar cale = null; SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss" ...

  7. PHP mysql 扩展库 操作mysql数据库步骤

    一.mysql 扩展库操作mysql数据库步骤如下: 1.获取连接 2.选择数据库 3.设置操作编码 4.发送指令(ddl数据定义/dml数据操作/dql数据查询/dtl数据事务控制) 5.接收返回的 ...

  8. Spring Security OAuth2 源码分析

    Spring Security OAuth2 主要两部分功能:1.生成token,2.验证token,最大概的流程进行了一次梳理 1.Server端生成token (post /oauth/token ...

  9. TCL电视直播软件

    升级你的电视系统我的型号46寸 V7300 3D,具体的升级程序在"技术宅"里有下载 找个格式化过的U盘把你的程序拷贝进去,插在电视上,电视会自动升级 当你成功安装V8-0MT32 ...

  10. 160525、高并发之mysql主从复制(linux)

    第一步:新建两台linux主机(我这里是使用虚拟机,linux版本是CentOS-6.3-x86_64-bin-DVD1.iso,注意:我下载的是dvd1,如果其他版本默认安装可能会自动还原) 第二步 ...