Theme Section

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5091    Accepted Submission(s): 2550

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
 
Recommend
liuyiding

题意:

有$n$个字符串,找到每个字符串中最长的一段子串。要求这段子串在字符串的开头,中间和结尾都出现过。输出子串长度。

思路:

果然题目做多了一看就能有思路。

要在开头和结尾都出现过,显然这个串的长度不可能大于$next[len]$。

因为$next[len]$表示的就是$S[1,len]$中,前缀和后缀匹配的最长的长度。

然后我们就要去找中间的。

现在我们已经知道$S[1,nxt[len]$和$S[len - nxt[len]+1,len]$是匹配的了,答案只能比他们更小

对于$S[nxt[len],len-nxt[len]+1]$的部分,我们去找每一个位置对应的$next$。

最大的那一个$next$就表示,可以找到的最长的一段既在开头出现又在中间出现的子串。

这个最大值和$nxt[len]$的较小值就是答案。

$len-nxt[len]+1$之后的部分为什么不找呢,因为如果找到了也是重叠了,画个图会发现这样是不可行的。

 #include<iostream>
#include<bits/stdc++.h>
#include<cstdio>
#include<cmath>
//#include<cstdlib>
#include<cstring>
#include<algorithm>
//#include<queue>
#include<vector>
//#include<set>
//#include<climits>
//#include<map>
using namespace std;
typedef long long LL;
typedef unsigned long long ull;
#define N 100010
#define pi 3.1415926535
#define inf 0x3f3f3f3f const int maxn = 1e6 + ;
int n;
char str[maxn];
int nxt[maxn]; void get_nxt(char *s)
{
int len = strlen(s + );
nxt[] = ;
for(int i = , j = ; i <= len; i++){
while(j > && s[i] != s[j + ])j = nxt[j];
if(s[i] == s[j + ])j++;
nxt[i] = j;
}
} int main()
{
while(scanf("%d", &n) != EOF){
for(int i = ; i < n; i++){
scanf("%s", str + );
get_nxt(str);
int len = strlen(str + );
int mx = -;
//cout<<nxt[len]<<endl;
for(int i = nxt[len]; i <= len - nxt[len] + ; i++){
//cout<<nxt[i]<<endl;
mx = max(mx, nxt[i]);
}
printf("%d\n", min(mx, nxt[len]));
}
}
return ;
}

hdu4763 Theme Section【next数组应用】的更多相关文章

  1. HDU4763 Theme Section —— KMP next数组

    题目链接:https://vjudge.net/problem/HDU-4763 Theme Section Time Limit: 2000/1000 MS (Java/Others)    Mem ...

  2. hdu4763 Theme Section

    地址:http://acm.hdu.edu.cn/showproblem.php?pid=4763 题目: Theme Section Time Limit: 2000/1000 MS (Java/O ...

  3. HDU4763 Theme Section 【KMP】

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

  4. HDU-4763 Theme Section KMP

    题意:求最长的子串E,使母串满足EAEBE的形式,A.B可以任意,并且不能重叠. 题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=4763 思 ...

  5. 【kmp算法】hdu4763 Theme Section

    kmp中next数组的含义是:next[i]表示对于s[0]~s[i-1]这个前缀而言,最大相等的前后缀的长度是多少.规定next[0]=-1. 迭代for(int i=next[i];i!=-1;i ...

  6. HDU4763 - Theme Section(KMP)

    题目描述 给定一个字符串S,要求你找到一个最长的子串,它既是S的前缀,也是S的后缀,并且在S的内部也出现过(非端点) 题解 CF原题不解释....http://codeforces.com/probl ...

  7. Theme Section(KMP应用 HDU4763)

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

  8. (KMP灵活运用 利用Next数组 )Theme Section -- hdu -- 4763

    http://acm.hdu.edu.cn/showproblem.php?pid=4763 Theme Section Time Limit: 2000/1000 MS (Java/Others)  ...

  9. hdu 4763 Theme Section(KMP水题)

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

随机推荐

  1. flex bison

    https://www.gnu.org/software/bison/manual/bison.html https://blog.csdn.net/sirouni2003/article/detai ...

  2. MySQL(3)-MySQL Workbench

    远程连接mysql不上,CentOS7下的防火墙关闭命令,别光看iptables的状态. # systemctl stop firewalld # systemctl mask firewalld   ...

  3. Oracle NID工具修改数据库DBID、数据库名称、数据库实例名

    DBID是数据库的唯一标识符,在一些特殊场合会涉及到DBID的相关内容,本篇文章的目的是演示将DB_NAME的值从ORCL修改为ORCL1. 一.查看当前的环境 [oracle@oracledb ad ...

  4. 【Java】关于MyBatis框架的总结

    最近在学习MyBatis框架,我在这里记录一下学习MyBatis过程中的心得体会. Mybatis是什么?使用它我们可以做什么? MyBatis是一个开源的数据持久层框架,它内部封装了通过JDBC访问 ...

  5. Tuxedo低版本客户端(Tuxedo 9)连接到高版本Tuxedo服务端(Tuxedo 12.1.3)的问题

    经过我实测,是没问题的.但是客户端的Tuxedo DLL必须全部是Tuxedo 9的DLL,不能混用.不然即使用Dependency Walker 分析DLL依赖完全正确,但是实际运行时结果也会出现奇 ...

  6. js事件探秘

    Javascript中的事件,可以和html交互. 事件流 IE&Opera:事件冒泡 其他浏览器: 事件捕获 事件冒泡:事件由最具体的元素(文档中嵌套层次最深的那个节点)接收,然后逐级向上传 ...

  7. MVC项目实践,在三层架构下实现SportsStore-11,使用Knockout实现增删改查

    SportsStore是<精通ASP.NET MVC3框架(第三版)>中演示的MVC项目,在该项目中涵盖了MVC的众多方面,包括:使用DI容器.URL优化.导航.分页.购物车.订单.产品管 ...

  8. 3.翻译系列:EF Code-First 示例(EF 6 Code-First系列)

    原文链接:http://www.entityframeworktutorial.net/code-first/simple-code-first-example.aspx EF 6 Code-Firs ...

  9. 【Vegas原创】查询SQL Server更改记录的语句

    指定数据库,然后: select Name,Create_date,Modify_Date from sys.objects where type in ('U','P', 'V','F', 'TR' ...

  10. windows下安装pycharm并连接Linux的python环境

    1. 下载安装Pycharm专业版 具体方法略.Pycharm5激活方法参考http://www.cnblogs.com/snsdzjlz320/p/7110186.html 2. 添加配置连接远程服 ...