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. C语言中用于计算数组长度的函数 “strlen() ”。

    de>#include<stdio.h>#include<stdlib.h>#define MAX_LEN 255int my_strlen1(const char* s ...

  2. 手写代码注意点--java.util.Stack相关

    1-Stack的基本函数为: 注意: 取栈顶的函数为peek(),不是top()... 测试stack是否为空的函数为empty(),不是isEmpty()...

  3. 【Tomcat】CentOS7.0下安装多个Tomcat及其配置

    安装前所需环境 在开始安装Tomcat之前,需要安装环境JDK,并配置JAVA环境.如果不知道如何配置,可参考这篇博客:[Linux]CentOS7.0下安装JDK环境 Tomcat安装 Tomcat ...

  4. [转]python3字符串与文本处理

    转自:python3字符串与文本处理 阅读目录 1.针对任意多的分隔符拆分字符串 2.在字符串的开头或结尾处做文本匹配 3.利用shell通配符做字符串匹配 4.文本模式的匹配和查找 5.查找和替换文 ...

  5. shell监控自动备份是否成功(判断文件是否存在)

    作者:邓聪聪 在自动打包生成备份sql文件后,对运行情况做一个监控!  命令前加time,查看命令执行完成所用时间! FILE_DIR=/root/ DATE=$(date +%F) FILE_NAM ...

  6. HDU 5288 OO’s Sequence

    题意: 给你一个序列, 有一个函数 F(L,R) 其中 ai 均不能 被 aL - aR整除的  函数值是这个ai个数 思路 : 反过来求 满足这样的条件的 ai 的区间,然后求和 #include& ...

  7. python学习第11天 迭代器

    函数的名称 闭包 迭代器 递归

  8. vue——router

    1.不同界面传参 <router-link :to="{path:'地址'},query:{name:val}">, 其它界面获取: this.$route.query ...

  9. swift 学习- 14 -- 继承

    // 一个类可以继承另一个 类的方法, 属性和其他特征, 当一个类继承其他类时, 继承类叫子类, 被继承类叫父类 或 (超类), 在 Swift 中, 继承是区分 [类] 和其他类型的 一个基本特征 ...

  10. MVVM 简介

    转:https://objccn.io/issue-13-1/ 所以,MVVM 到底是什么?与其专注于说明 MVVM 的来历,不如让我们看一个典型的 iOS 是如何构建的,并从那里了解 MVVM: 我 ...