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. Win10更新搜狗输入法后重启输入密码蓝屏

    解决办法:如果能进入安全模式,卸载搜狗输入法:不行的话(好像不行)只能重装系统:因为蓝屏后就基本开不了了!!!生气!! win10 1809 19.3月累积更新之后蓝屏:安装了搜狗输入法的win10 ...

  2. SpringCloud Stream生产者配置RabbitMq的动态路由键

    在写这个文章前不得不吐槽目前国内一些blog的文章,尽是些复制粘贴的文章,提到点上但没任何的深入和例子.......... 经过测试下来总结一下RabbitMQ的Exchange的特性: 1.dire ...

  3. eclipse alt+/智能提示错误问题

    转自: https://blog.csdn.net/u013066244/article/details/69054447

  4. C#后台执行js

    StringBuilder sb = new StringBuilder(); sb.Append("<script type='text/javascript'>") ...

  5. C#获取网页的HTML码、下载网站图片 get post

    /// <summary> /// 获取网页的HTML码 /// </summary> /// <param name="url">链接地址&l ...

  6. export / import 温故而知新

    认知一: 导出的对象被修改,依然会影响原来的对象. 仔细想想这是理所当然的事(说明导出的依然是对象指向内存的地址,所以通常还需要结合深拷贝使用) /** export const state = { ...

  7. 使用Nginx实现灰度发布(转)

    灰度发布是指在黑与白之间,能够平滑过渡的一种发布方式.AB test就是一种灰度发布方式,让一部分用户继续用A,一部分用户开始用B,如果用户对B没有什么反对意见,那么逐步扩大范围,把所有用户都迁移到B ...

  8. tensorflow 笔记11:tf.nn.dropout() 的使用

    tf.nn.dropout:函数官网说明: tf.nn.dropout( x, keep_prob, noise_shape=None, seed=None, name=None ) Defined ...

  9. linux每日命令(10):touch命令

    linux的touch命令一般用来修改文件时间戳,或者新建一个不存在的文件. 一.命令格式: touch [参数]... 文件... 二.命令参数: 参数 描述 -a 或--time=atime或-- ...

  10. Oracle分析函数-排序排列(rank、dense_rank、row_number、ntile)

    (1)rank函数返回一个唯一的值,除非遇到相同的数据时,此时所有相同数据的排名是一样的,同时会在最后一条相同记录和下一条不同记录的排名之间空出排名. (2)dense_rank函数返回一个唯一的值, ...