【kmp算法】hdu4763 Theme Section
kmp中next数组的含义是:next[i]表示对于s[0]~s[i-1]这个前缀而言,最大相等的前后缀的长度是多少。规定next[0]=-1。
迭代for(int i=next[i];i!=-1;i=next[i]) 就可以得到某个前缀所有长度相等的前后缀的长度。
这题你就暴力枚举整个字符串的所有相等的前后缀,然后暴力判中间的部分是否存在即可。当然使用next数组会很简便。
#include<cstdio>
#include<cstring>
using namespace std;
int next[1000100],T,n;
char s[1000100];
void GetFail(char P[],int next[])//next[i]表示s[0]~s[i-1]的前缀中,最大相等的前后缀的长度是多少
{
next[0]=-1;
int len=strlen(P);
for(int i=0;i<len;i++)
{
int j=next[i];
while(j>=0&&P[i]!=P[j])
j=next[j];
if(j!=-1 && P[i]==P[j])
next[i+1]=j+1;
else next[i+1]=0;
}
for(int i=0;i<len;++i)//左移一位形成最大长度表
next[i]=next[i+1];
}
int main()
{
// freopen("c.in","r",stdin);
scanf("%d",&T);
for(;T;--T)
{
scanf("%s",s);
n=strlen(s);
GetFail(s,next);
int E=next[n-1];
while(E)
{
int k=n-E-1;
while(next[k]<E && k>=2*E-1)
--k;
if(k>=2*E-1)
{
printf("%d\n",E);
goto OUT;
}
E=next[E-1];
}
puts("0");
OUT:;
}
return 0;
}
【kmp算法】hdu4763 Theme Section的更多相关文章
- HDU4763 Theme Section —— KMP next数组
题目链接:https://vjudge.net/problem/HDU-4763 Theme Section Time Limit: 2000/1000 MS (Java/Others) Mem ...
- HDU4763 Theme Section 【KMP】
Theme Section Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) To ...
- hdu4763 Theme Section
地址:http://acm.hdu.edu.cn/showproblem.php?pid=4763 题目: Theme Section Time Limit: 2000/1000 MS (Java/O ...
- hdu4763 Theme Section【next数组应用】
Theme Section Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...
- HDU4763 - Theme Section(KMP)
题目描述 给定一个字符串S,要求你找到一个最长的子串,它既是S的前缀,也是S的后缀,并且在S的内部也出现过(非端点) 题解 CF原题不解释....http://codeforces.com/probl ...
- HDU-4763 Theme Section KMP
题意:求最长的子串E,使母串满足EAEBE的形式,A.B可以任意,并且不能重叠. 题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=4763 思 ...
- Theme Section(KMP应用 HDU4763)
Theme Section Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- hdu 4763 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灵活应用)
Theme Section Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) To ...
随机推荐
- [bzoj 2115]线性基+图论
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2115 给定一个带权无向图,要找出从1到n路径权值异或和最大的那一条的路径异或和. 考虑1到 ...
- saltshaker填坑
参考资料: https://github.com/yueyongyue/saltshaker http://blog.sina.com.cn/s/blog_b21312340102whzw.html ...
- 构建一个类jq的函数库
jqfree core var $ = function(selector, context) { return new $.fn.init(selector, context); }; $.fn = ...
- Phantomjs设置浏览器useragent的方式
Selenium中使用PhantomJS,设置User-Agent的方法. 默认情况下,是没有自动设置User-Agent的:设置PhantomJS的user-agent def __init__(s ...
- 深入探索 高效的Java异常处理框架
转载自:http://www.sunwei.org/archives/196 摘要:本文从Java异常最基本的概念.语法开始讲述了Java异常处理的基本知识,分析了Java异常体系结构,对比Sprin ...
- jzoj2701 【GDKOI2012模拟02.01】矩阵
传送门:https://jzoj.net/senior/#main/show/2701 [题目大意] 给出矩阵A,求矩阵B,使得
- POJ2186 (强连通分量缩点后出度为0的分量内点个数)
Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 27820 Accepted: 11208 De ...
- 大话Linux内核中锁机制之原子操作、自旋锁【转】
转自:http://blog.sina.com.cn/s/blog_6d7fa49b01014q7p.html 多人会问这样的问题,Linux内核中提供了各式各样的同步锁机制到底有何作用?追根到底其实 ...
- Google I/O完整盘点,这才是地球上最「性感」的发布会
https://news.cnblogs.com/n/569588/ Google.ai:展现 AI 最好的一面 Google 今天新发布了第二代的 Tensor 处理单元(TPU),这是一个云计算硬 ...
- 4.flask数据库
1.安装MySQL 直接去下载即可,如果是windows建可以下载msi,一路next即可.我已经安装过了,这里就不再演示了. 最后使用Navicat连接测试一下,我这里是没有问题的 2.SQLAlc ...