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. ArcGIS AddIN开发之COM对象写入注册表

    做一个交互式绘制文字的工具,希望这次设置的Symbol,下次打开ArcMap时自动调用这个Symbol,并支持对其进行修改. 解决方法是将这个Symbol写入注册表中,每次自动读取上一次设置的Symb ...

  2. map的用法

    Map是c++的一个标准容器,她提供了很好一对一的关系,在一些程序中建立一个map可以起到事半功倍的效果,总结了一些map基本简单实用的操作!1. map最基本的构造函数:   map<stri ...

  3. ngrok访问外网

    1. 外网映射工具介绍 windows用户: 1,下载windows版本的客户端,解压到你喜欢的目录2,在命令行下进入到path/to/windows_386/下3,执行 ngrok -config= ...

  4. 20145337《Java程序设计》第三周学习总结

    20145337 <Java程序设计>第三周学习总结 教材学习内容总结 类与对象 类与对象的关系:要产生对象必须先定义类,类是对象的设计图,对象是类的实例.我觉得在视频中对类与对象关系的描 ...

  5. INSTRUCTION CYCLE

    COMPUTER ORGANIZATION AND ARCHITECTURE DESIGNING FOR PERFORMANCE NINTH EDITION • Fetch: Read the nex ...

  6. win commands

    wmic process where name="explorer.exe" delete 我想你问得应该是运行命令吧!我查了,没有,你自己参考一下:Windows常用命令集 wi ...

  7. UIBezierPath类 笔记

    使用UIBezierPath类可以创建基于矢量的路径.此类是Core Graphics框架关于path的一个封装.使用此类可以定义简单的形状,如椭圆或者矩形,或者有多个直线和曲线段组成的形状.     ...

  8. dom4j微信接口开发

    新建一个web项目,我用的是eclipse和tomcat7.0 ,外网环境用的nat123 先建立一个实体bean:TextMessage /** * xml基本对象 * @author xiaohu ...

  9. windows2003安装TestDirector8.0 安装时输入用户名密码 提示错误

    1.TestDirector8.0的安装过程 2.修改错误 右键“我的电脑”->“属性”->"高级"->性能对应的"设置"->" ...

  10. Qt字符串类——1.字符串常用的几种操作

    字符串有如下几个操作符: (1)QString提供了一个二元的"+"操作符用于组合两个字符串,并提供了一个"+="操作符用于将一个字符串追加到另一个字符串的末尾 ...