题目网址: http://acm.hust.edu.cn/vjudge/contest/view.action?cid=110060#problem/B

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
 
题意: 输入一个字符串,求这个字符串中的一个子串,该子串为此字符串的前缀和后缀,并在字符串中间也有这个子串,但但这三个子串不能重叠,求是否存在这样的子串,输出子串长度的最大值,否则输出0;
 
思路: 使用扩展KMP算法,扩展KMP的next[i]表示从s[i]开始的与s前缀最大的匹配长度。在一个位置i中,如果要满足要求,子串的最大长度不能超过
min(i,  next[i], (len - i) / 2) ,所有的最大长度的最大值就是可能存在的最大长度。先找利用next[]找中间与前缀匹配的子串,然后判断后缀与子串匹配的最大长度,并要注意子串不能重叠。
 
代码:
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
using namespace std;
char s[];
int nex[];
int pre[]; void ex_next(int length)
{
///nex[i]: 以第i位置开始的子串与T的前缀的最大长度;
int i,j;
nex[]=length;
for(i=; i<length-&&s[i]==s[i+];i++);///前缀都是同一个字母的时候;
nex[]=i;
int a=;///a为使匹配到最远的地方时的起始匹配地点;
for(int k=;k<length;k++)
{
int p=a+nex[a]-,L=nex[k-a];
if( (k-)+L>=p )
{
int j=(p-k+)>?(p-k+):;
while(k+j<length&&s[k+j]==s[j]) j++;
/// 枚举(p+1,length) 与(p-k+1,length) 区间比较;
nex[k]=j,a=k;
}
else nex[k]=L;
}
} int main()
{
int T,M,n;
scanf("%d",&T);
while(T--)
{
M=;
scanf("%s",s);
int len=strlen(s);
ex_next(len);
for(int i=;i<len;i++)
{
if(nex[i])
{
///此时nex[i]为中间以s[i]开始的子串与前缀匹配的最大长度;
///接下来判断后缀与前缀及中间子串的最大匹配长度;
n=min(min (i,nex[i]),(len-i)/);
for(int j=len-n;j<len;j++)
if(nex[j]==len-j)
{
if(M<nex[j]) M=nex[j];
else break;
}
}
}
printf("%d\n",M);
}
return ;
}
 

最新文章

  1. 黄聪:如何给wordpress的编辑器添加一个自定义按钮,并且实现插入功能
  2. VMware下利用ubuntu13.04建立嵌入式开发环境之二
  3. RabbitMQ集群、镜像部署配置
  4. for while (list each)的用法
  5. 在cmd窗口中运行php命令
  6. Codeforces Round #263 (Div. 1)
  7. android 开源项目一览
  8. hdu 1047 Integer Inquiry(高精度数)
  9. Linux 中查看网口流量的利器 -- sar
  10. Lambda表达式 简介 语法 示例
  11. XML DOM 遍历Xml文档
  12. 解决linux下oracle进入sqlplus环境中后退键显示^H、上下键无效与ctrl+l无法清屏等问题【weber出品必属精品】
  13. [Xcode]使用target进行协同开发
  14. iOS调试-LLDB学习总结
  15. 使用requireJS
  16. [Luogu2991][USACO10OPEN]水滑梯Water Slides
  17. 解析Json字符串的三种方法
  18. Python爬虫技术(从网页获取图片)+HierarchicalClustering层次聚类算法,实现自动从网页获取图片然后根据图片色调自动分类—Jason niu
  19. c# IO操作
  20. python的一些内置模块

热门文章

  1. 关于BigDecimal的使用
  2. win7搭建ios开发环境
  3. 8个经典炫酷的HTML5 Canvas动画欣赏
  4. 数轴上从左到右有n个点a[0],a[1]…,a[n-1],给定一根长度为L的绳子,求绳子最多能覆盖其中的几个点。要求算法复杂度为o(n)。
  5. Go语言实现HashSet
  6. easyui 键盘控制tree 上下
  7. Code the Tree(图论,树)
  8. 无插件纯web 3D机房 (第四季:大型园区、地球仪效果和其他扩展应用)
  9. 进阶学习js中的执行上下文
  10. c++中继承和java中继承的对比