【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 ...
随机推荐
- HDU 1698 Just a Hook(线段树
Just a Hook Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- Java节假日算法
类:Vacation package test; import java.io.Serializable; import java.util.Date; public class Vacation i ...
- POJ 1064---二分搜索法
///2.假定一个解并判断是否可行 ///POJ1064 /** Q:有N条绳子,长度分别为Li,从中切割出k条长度相同的绳子, 这K条绳子最长能有多长?保留两位小数 A: 二分搜索模型. 条件C(x ...
- HDU2177取(2堆)石子游戏---(威佐夫博弈)
http://acm.hdu.edu.cn/showproblem.php?pid=2177 取(2堆)石子游戏 Time Limit: 3000/1000 MS (Java/Others) M ...
- 百练2505:A multiplication game
传送门:http://bailian.openjudge.cn/practice/2505/ [题解] 我们找找规律: 1~9显然是Stan wins. 10~18是Ollie wins. 19~16 ...
- 【BZOJ】1593: [Usaco2008 Feb]Hotel 旅馆
[算法]线段树(经典线段树上二分) [题意]n个房间,m个询问,每次订最前的连续x个的空房间,或退订从x开始y个房间,求每次订的最左房间号. [题解]关键在于找连续x个空房间,经典二分. 线段树标记s ...
- python函数篇:名称空间、作用域和函数的嵌套
一.名称空间:(有3类) (1)内置名称空间(全局作用域) (2)全局名称空间(全局作用域) (3)局部名称空间(局部作用域) 关于名称空间的查询: x=1 def func(): print('fr ...
- wxpython布局管理部件wx.gridbagsizer用法示例
text = ("This is text box") panel = wx.Panel(self, -1) chkAll1 = wx.CheckB ...
- Apache服务器
Apache服务器 一 简介 1 www:world wide web 万维网 http 协议: 超文本传输协议 HTML语言: 超文本标识语言 2 URL:统一资源定位 ...
- vue+axios下载文件的实现
HTML代码: <el-button size="medium" @click="download">下载表格</el-button> ...