Theme Section

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

Total Submission(s): 4876    Accepted Submission(s): 2439

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

题意:求出在字符串前、中、后均出现的最长字符的长度

分析:利用next数组求出相同前后缀,再用kmp验证中间是否存在该前缀

#include<iostream>
#include<string.h>
using namespace std;
char p[1000001];
int nex[1000001];
int len;
void getnex(int pp)
{
int i=0,k=-1;
nex[0]=-1;
while(i<pp)
{
if(k==-1||p[i]==p[k])
nex[++i]=++k;
else
k=nex[k];
}
}
bool kmp(int st)//验证中间是否存在与前后缀匹配的字符串
{
int i=st,j=0;
int n=strlen(p);
while(i<n-st)//st~n-st区间
{
if(j==-1||p[i]==p[j])
i++,j++;
else
j=nex[j];
if(j==st)//有一段与前缀相等的
return 1;
}
return 0;
}
int main()
{
int n;
scanf("%d",&n);
while(n--)
{
scanf("%s",p);
len=strlen(p);
if(len<3)
{
printf("0\n");continue;
}
getnex(len);
int ans=0;
while(nex[len]!=-1)
{
if(nex[len]>ans&&kmp(nex[len]))
{
ans=nex[len];
break;
}
len=nex[len];
}
printf("%d\n",ans);
}
return 0;
}

HDU 4763 Theme Section(KMP灵活应用)的更多相关文章

  1. hdu 4763 Theme Section(KMP水题)

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

  2. HDU 4763 Theme Section(KMP+枚举公共前后缀)

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

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

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

  4. HDU 4763 Theme Section (2013长春网络赛1005,KMP)

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

  5. HDU 4763 Theme Section

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

  6. 2013长春网赛1005 hdu 4763 Theme Section(kmp应用)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4763 题意:给出一个字符串,问能不能在该串的前中后部找到相同的子串,输出最长的字串的长度. 分析:km ...

  7. HDU - 4763 Theme Section (KMP的next数组的应用)

    给定一个字符串,求出一个前缀A,使得字符串的构成可以表示成ABABA的形式(B可以为空串). 输出这个前缀的最大长度. KMP算法Next数组的使用. 枚举中间的每个位置,可以根据Next数组求出这个 ...

  8. CF126B password&&HDU 4763 Theme Section

    http://acm.hdu.edu.cn/showproblem.php?pid=4763 http://codeforces.com/problemset/problem/126/B 这两个题都是 ...

  9. hdu 4763 Theme Section(next数组找串中三段相等)

    题意:在一个串中找 EAEBE 的形式的最长的E,其中E为一个字符串,也就是说找到前缀与后缀相同,并且串中还存在相同的一段,它们不能重复. 思路:利用next数组,next[len]代表的即是最大的相 ...

随机推荐

  1. openstack Q版部署-----nova服务配置-控制节点(5)

    一.创建数据库(控制节点) 创建数据库以及用户: CREATE DATABASE nova_api; CREATE DATABASE nova; CREATE DATABASE nova_cell0; ...

  2. SpringSecurityOAuth使用JWT Token

    ⒈JWT? JWT(Json Web Token),是Json的一个开放的Token标准. 1,自包含,SpringSecurityOAuth的默认Token是UUID的一个随机的无意义的字符串,并不 ...

  3. liunx 安装Zabbix的心酸历程

    作者:邓聪聪 为了工作之需要,在系统服务方面不断的摸爬打滚,有了这样点点滴滴的经验,留后在使用!  插件网盘取. 链接: https://pan.baidu.com/s/1i5u0ed3 密码: rx ...

  4. boost 随机数发生器

    Random     随机数 在很多应用中都需要使用随机数.本库力求提供一个高效的,通用的随机数库.boost库有多种随机数生成方式.先熟悉一下各种随机数生成器的概念. 数字生成器(Number Ge ...

  5. 第八章 让Bootstrap轮播插件carousel支持左右滑动手势的三种方法

    因为最近开发的项目涉及到移动设备上的 HTML5 开发,其中需要实现轮播效果.然后最快捷的方式,你知道的(Bootstrap),然后原生的 Bootstrap 的 carousel.js 插件并没有支 ...

  6. 专题1:记忆化搜索/DAG问题/基础动态规划

      A OpenJ_Bailian 1088 滑雪     B OpenJ_Bailian 1579 Function Run Fun     C HDU 1078 FatMouse and Chee ...

  7. oracle 会话 解锁

    背景 这是当年第一次记录博客,当初记录的原因是感觉有些问题很少碰到,碰到有网上寻找一遍,文章很少是正好对症的,折腾半天终于解决了,但是没有记录过程,结果下次碰到又要从来一次.有的问题还极其不好找,or ...

  8. ubuntu 远程登录错误

    利用  ubuntu 16.04 自带功能远程登录到同事的电脑时,提示如下错误: This file server type is not recognized 百度一下,得知,缺少必要的文件,安装后 ...

  9. ModelSerializer序列化(Apiview)

    url部分: url(r'^book/$',views.book.as_view()),url(r'^books/(\d+)/$', views.bookdetail.as_view(),name=' ...

  10. CSS 媒体查询@media

    1. 概述 1.1 定义 @media可以针对不同的屏幕尺寸(媒体类型)设置不同的样式,在响应式页面中,@media非常有用.重置浏览器大小的过程中,页面也会根据浏览器的宽度和高度重新渲染页面. 1. ...