hdu4763 Theme Section【next数组应用】
Theme Section
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5091 Accepted Submission(s): 2550
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?
xy
abc
aaa
aaaaba
aaxoaaaaa
0
1
1
2
题意:
有$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数组应用】的更多相关文章
- HDU4763 Theme Section —— KMP next数组
题目链接:https://vjudge.net/problem/HDU-4763 Theme Section Time Limit: 2000/1000 MS (Java/Others) Mem ...
- hdu4763 Theme Section
地址:http://acm.hdu.edu.cn/showproblem.php?pid=4763 题目: Theme Section Time Limit: 2000/1000 MS (Java/O ...
- HDU4763 Theme Section 【KMP】
Theme Section Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) To ...
- HDU-4763 Theme Section KMP
题意:求最长的子串E,使母串满足EAEBE的形式,A.B可以任意,并且不能重叠. 题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=4763 思 ...
- 【kmp算法】hdu4763 Theme Section
kmp中next数组的含义是:next[i]表示对于s[0]~s[i-1]这个前缀而言,最大相等的前后缀的长度是多少.规定next[0]=-1. 迭代for(int i=next[i];i!=-1;i ...
- HDU4763 - Theme Section(KMP)
题目描述 给定一个字符串S,要求你找到一个最长的子串,它既是S的前缀,也是S的后缀,并且在S的内部也出现过(非端点) 题解 CF原题不解释....http://codeforces.com/probl ...
- Theme Section(KMP应用 HDU4763)
Theme Section Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- (KMP灵活运用 利用Next数组 )Theme Section -- hdu -- 4763
http://acm.hdu.edu.cn/showproblem.php?pid=4763 Theme Section Time Limit: 2000/1000 MS (Java/Others) ...
- hdu 4763 Theme Section(KMP水题)
Theme Section Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) To ...
随机推荐
- [Aaronyang紫色博客] 写给自己的WPF4.5-Blend5公开课系列 1
我的文章一定要做到对读者负责,否则就是失败的文章 --------- www.ayjs.net aaronyang技术分享 欢迎大家支持我的力作<[Aaronyang] 写给自己的 ...
- 关于Installation error: INSTALL_FAILED_NO_MATCHING_ABIS的解决方法
遇到过好几次这种错误提示,工程代码没有任何错误,但是连安装都安装不上模拟器,console控制台就报出上面的错误: [2015-11-25 15:15:37 - Em4.x] Installation ...
- [转]关于ios 推送功能的终极解决
刚刚做了一个使用推送功能的应用 遇到了一些问题整的很郁闷 搞了两天总算是弄明白了 特此分享给大家 本帖 主要是针对产品发布版本的一些问题 综合了网上一些资料根据自己实践写的 不过测试也可以看看 首先要 ...
- 【Java多线程】JDK1.5并发包API杂谈
并发与并行 并发 一个或多个处理器执行更多的任务(通过划分时间片来执行更多的任务),从逻辑上实现同时运行: 如,N个并发请求在一个两核CPU上: 并行 N个处理器分别同时执行N个任务,从物理上实现同时 ...
- linux每日命令(3):ln命令
ln是linux中又一个非常重要命令,它的功能是为某一个文件在另外一个位置建立一个同步的链接.当我们需要在不同的目录,用到相同的文件时,我们不需要在每一个需要的目录下都放一个必须相同的文件,我们只要在 ...
- Oralce 日期操作
1.日期比较 --1.在确定时间之前: select * from up_date where update < to_date('2018-06-05 00:00:00','yyyy-mm-d ...
- CTimeSpan
要获取两个时间差,如两个CTime的时间差,可以使用MFC中的CTimeSpan类. CTime time1 = CTime::GetCurrentTime(); CTime time2 = CTim ...
- Java知多少(98)Graphics类的绘图方法
Graphics类提供基本绘图方法,Graphics2D类提供更强大的绘图能力.本节讲解Graphics类,下节讲解Graphics2D. Graphics类提供基本的几何图形绘制方法,主要有:画线段 ...
- ASP.NET IIS Registration Tool (Aspnet_regiis.exe)
IIS Version Special cases for 32-bit versions of Aspnet_regiis.exe 6.0 You can run the 32-bit versio ...
- Java查找出现的单词
如何找到一个单词的每个出现? 解决方法 下面的例子演示了如何使用Pattern.compile()方法和m.group()方法找到一个词出现次数. import java.util.regex.Mat ...