Theme Section

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 1995 Accepted Submission(s): 943

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

2013 ACM/ICPC Asia Regional Changchun Online

#include <map>
#include <set>
#include <queue>
#include <cstring>
#include <string>
#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std; const int INF = 0x3f3f3f3f;
const int MAX = 1e6+100;
char str[MAX];
int Next[MAX]; void GetNext()//KMP得到Next数组
{
int len=strlen(str);
Next[0]=-1;
int i=1,j=0;
while(i<len)
{
if(j==-1||str[i]==str[j])
{
i++;
j++;
Next[i]=j;
}
else
{
j=Next[j];
}
}
} int Judge(int len)
{
int Len=strlen(str);
int i=len,j=0;
while(i<Len-len)
{
if(j==-1||str[i]==str[j])
{
i++;
j++;
}
else
{
j=Next[j];
}
if(j==len)
{
return len;
}
}
return -1;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%s",str);
int len=strlen(str);
if(len<3)
{
printf("0\n");
continue;
}
GetNext();
int R=strlen(str);
int Max=-1;
while(Next[R]>=0)
{
if(Next[R]>Max)
{
int ans=Judge(Next[R]);
if(ans>Max)
{
Max=ans ;
break;
}
}
R=Next[R];
}
if(Max==-1)
{
printf("%d\n",Max+1);
}
else
printf("%d\n",Max); }
return 0;
}

Theme Section(KMP应用 HDU4763)的更多相关文章

  1. HDU4763 Theme Section —— KMP next数组

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

  2. hdu 4763 Theme Section(KMP水题)

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

  3. HDU 4763 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. HDU 4763 Theme Section(KMP+枚举公共前后缀)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4763 题目大意: 给你一个字符串s,存在一个子串E同时出现在前缀.中间.后缀,即EAEBE这种模式,A ...

  7. HDU 4763 Theme Section ( KMP next函数应用 )

    设串为str, 串长为len. 对整个串求一遍next函数,从串结尾开始顺着next函数往前找<=len/3的最长串,假设串长为ans,由于next的性质,所以找到的串肯定满足E……E这种形式, ...

  8. HDU4763 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 (2013长春网络赛1005,KMP)

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

随机推荐

  1. 定位position

    position : absolute | relative | fixed | static 定位:子集可以超出父级的范围,如父级蛇overflow:hidden则看不到. absolute : 绝 ...

  2. 对Oracle10g rac ons服务的一些理解

    1.什么是ONS ONS(Oracle Notification Service)是Oracle Clusterware 实现FAN Event Push模型的基础.     在传统模型中,客户端需要 ...

  3. Neusoft(3)增加自己的内核模块

    Linux版本 Ubuntu12.04LTS,内核版本为3.2.0-26-generic-ape 具体方法如下: 1.建立源文件,假设文件目录为path,文件名为hello.c源代码如下: #incl ...

  4. C#构造Http 破解学校教务系统学生账号密码

    背景介绍 我们学校的教务系统的是以学生学号作为登陆账号,初始密码是自己的生日. 一点点想法 每次期末查成绩的时候,我都会有一个想法,要是我能跑到系统后台,把自己的成绩修改一下,那该时间多么舒坦的事情啊 ...

  5. LaTex 文本排版

    一.对齐段落 \\ or \newline 另起一行,而不是另起一段 \\* 在强制断行后,还禁止分页 \newpage 另起一页 \linebreak[n] \nolinebreak[n] \pag ...

  6. javaWeb中servlet开发(4)——servlet跳转

    servlet跳转 1.跳转类型 客户端跳转:跳转后地址栏改变,无法传递request范围内属性,是在所有的操作都执行完毕之后才发生跳转的操作,跳转语法是,response.sendRedict() ...

  7. rabbitmq之消息生命周期

    参考:http://jzhihui.iteye.com/blog/1567232

  8. mysql重点--索引

    1.关于索引 # 什么是索引 索引是表的目录,在查找内容之前可以先在目录中查找索引位置,以此快速定位查询数据. #索引的作用 加速查询和约束. # 为什么索引查询会变快 没创建一个索引会相应的创建一个 ...

  9. 判断日期是否符合yyyy-mm格式

    !Regex.IsMatch(dr["DMAKEDATE"].ToString(),@"^(?<year>\\d{2,4})-(?<month>\ ...

  10. Servlet 生命周期、工作原理

    按照单例的编码规则,Servlet本身只是一个Java,结构并不是单例结构. 只是Web容器在维护这些Servlet的时候只给创建一个实例存在JVM中,用户请求服务时,服务器只调用它已经实例化好的Se ...