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. web,xml中关于filter的使用

    从J2EE1.3开始,Servlet2.3规范中加入了对过滤器的支持.过滤器能够对目标资源的请求和响应进行截取.过滤器的工作方式分为四种,下面让我们分别来看看这四种过滤器的工作方式:1.request ...

  2. 实现一个原子的正整数类:AtomicPositiveInteger

    import java.util.concurrent.atomic.AtomicInteger; public class AtomicPositiveInteger extends Number ...

  3. 使用js获取QueryString的方法小结

    一.<script> urlinfo=window.location.href; //获取当前页面的url len=urlinfo.length;//获取url的长度 offset=url ...

  4. 关于html,body{height:100%}的解释

    来源:https://www.cnblogs.com/QianBoy/p/8571682.html 今天看到一个CSS样式:html,body{height:100%},第一次看到,感觉挺奇怪,为什么 ...

  5. 预防 app crash 之 unrecognized selector

    处理unrecognized selector异常原因 假如封装一个方法,在其他模块调用该方法时,传入参数不匹配则crash.比如下面的方法:本应该传入的参数类型为NSMutableArray,如果传 ...

  6. Ubuntu菜鸟入门(十七)—— E: Sub-process /usr/bin/dpkg returned an error code (1) 出错解决方案

    cd /var/lib/dpkg sudo mv info info.bak sudo mkdir infosudo apt-get install shotwel

  7. android保持服务不休眠(持续运行)以及唤醒屏幕的方法

    假设有这样一个应用场景,一个服务一直在默默的工作(比如即时地获取服务器的消息),即使在屏幕已经因为长时间无操作而关闭,或者用户按了电源键让屏幕关闭,手机进入休眠状态,他必须依然在工作中.一旦从服务器获 ...

  8. Socket网络编程--聊天程序(1)

    很早的一段时间,看了APUE和UNPv1了解了网络编程,但是但是只是看而已,没有具体的实践,趁现在没有什么事做,就来实践了解一下网络编程.写博客保存下来,方便以后用到的时候可以查到. 此次的聊天程序是 ...

  9. python prettytable模块

    简介 Python通过PrettyTable模块可以将输出内容如表格方式整齐地输出. 安装 pip install prettytable 1 示例 from prettytable import P ...

  10. Haproxy全透明代理

    1. 系统环境搭建 操作系统Centos7 内核版本3.10 Centos7已自带TPROXY模块,不需要安装TPROXY 2. Haproxy下载,编译,安装,配置 下载地址 http://www. ...