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的更多相关文章

  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 【KMP】

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

  3. hdu4763 Theme Section

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

  4. hdu4763 Theme Section【next数组应用】

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

  5. HDU4763 - Theme Section(KMP)

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

  6. HDU-4763 Theme Section KMP

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

  7. Theme Section(KMP应用 HDU4763)

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

  8. hdu 4763 Theme Section(KMP水题)

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

  9. HDU 4763 Theme Section(KMP灵活应用)

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

随机推荐

  1. EditPlus直接连接Linux服务器编辑文本文件

    填写好:描述,ip地址,用户名,密码, 然后点下面的高级选项: 然后返回上一个页面,继续 确定 OK: 然后,在主界面左侧点倒三角: 就可以选择我们之前配置的远程服务器地址,弹出提示框 点确定, 就连 ...

  2. pmap用法小计

    By francis_hao    Aug 4,2017   pmap-报告进程的内存映射.   概要 pmap [options] pid [...]   描述 pmap命令用来报告一个或多个进程的 ...

  3. Elasticsearch报错

    [2018-07-12T10:32:47,642][INFO ][o.e.b.BootstrapChecks ] [VfCcJIq] bound or publishing to a non-loop ...

  4. Codeforces Round #351 (VK Cup 2016 Round 3, Div. 2 Edition) A

    A. Bear and Game time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  5. Eclipse来push,fetch,rebase代码

    如何与项目里的其他人一起合作项目,提交代码并更新呢?这里提出我比最近用到的两种工具:一种是Eclipse,另外一个是SourceTree.个人推荐从事Java开发的话,可以用Eclipse.当然,还有 ...

  6. CSS3学习笔记之立体线框球形动画

    效果截图: HTML代码: <div class="ball-box"> <div class="ball"> <div clas ...

  7. SVG布局

    http://www.w3cplus.com/html5/nesting-svgs.html

  8. [BZOJ1441&BZOJ2257&BZOJ2299]裴蜀定理

    裴蜀定理 对于整系数方程ax+by=m,设d =(a,b) 方程有整数解当且仅当d|m 这个定理实际上在之前学习拓展欧几里得解不定方程的时候就已经运用到 拓展到多元的方程一样适用 BZOJ1441 给 ...

  9. 在Unity(C#)下实现Lazy Theta*寻路

    在上篇文章中我们介绍了Lazy Theta*.本篇中我会演示一下我实现的Lazy Theta*. 先上代码 //在一个点被expand时是否调用DebugOnExpanded事件,用于debug查看e ...

  10. DotNETCore 学习笔记 全球化和本地化

    Globalization and localization ********************************************************************* ...