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. HTTP协议 (七) Cookie

    HTTP协议 (七) Cookie Cookie是HTTP协议中非常重要的东西, 之前拜读了Fish Li 写的[细说Cookie], 让我学到了很多东西.Fish的这篇文章写得太经典了. 所以我这篇 ...

  2. php有效的过滤html标签,js代码,css样式标签

    过滤html标签�php中太简单了,我们可以直接使用strip_tags函数来实现了,下面给各位整理了一些关于 strip_tags函数的例子. php过滤html的函数:strip_tags(str ...

  3. iOS9 HTTP传输安全

    1.在Info.plist中添加 NSAppTransportSecurity 类型  Dictionary 2.在 NSAppTransportSecurity 下添加 NSAllowsArbitr ...

  4. vim的常用命令

    平常最多是用vim来编辑单个文件,看看源码.就是写几k行代码时也没有用一些其他的插件,只是设置了高亮等一些自带的属性.这样的好处是,换到任何一台新机上都能立马使用. 网上流传了大量的“vim命令合集” ...

  5. document获取节点byId&byName

    <script type="text/javascript"> /* *需要:获取页面中的DIV节点: *思路: *通过docment对象完成.因为div节点有ID属性 ...

  6. html5:地理信息 LBS基于地理的服务和百度地图API的使用

    地理位置请求 单次定位请求getCurrentPosition(请求成功函数,请求失败函数,数据收集方式) 多次定位请求watchPosition(请求成功函数,请求失败函数,数据收集方式) 关闭更新 ...

  7. 动态给drawable上色

    只加载一个资源,然后在运行的时候通过ColorFilter进行上色 public Drawable colorDrawable(Resources res, @DrawableRes int draw ...

  8. C#winform调用外部程序,等待外部程序执行完毕才执行下面代码

    1.简单调用外部程序文件(exe文件,批处理等),只需下面一行代码即可 System.Diagnostics.Process.Start(“应用程序文件全路径”); 2.如果要等待调用外部程序执行完毕 ...

  9. VC 菜单前的勾的切换

    if (pMenu->GetSubMenu(2)->GetMenuState(ID_STOP_SPOT_OP_MOSUE,MF_BYCOMMAND) == MF_UNCHECKED) { ...

  10. butterknife异常提示:attribute value must be constant

    就是因为你的android工程是lib类型的 如: apply plugin: 'com.android.library' android { compileSdkVersion 23 buildTo ...