HDU4763 - Theme Section(KMP)
题目描述
给定一个字符串S,要求你找到一个最长的子串,它既是S的前缀,也是S的后缀,并且在S的内部也出现过(非端点)
题解
CF原题不解释。。。。http://codeforces.com/problemset/problem/126/B
KMP的失配函数fail[i]的值就是s[0..i]的最长前缀且是后缀的长度~~~,因此我们从S的末尾位置开始沿着失配函数跑即可,对于当前fail[i],判断前缀s[0…i]是否在s[i+1..length(s)-i]是否出现即可~~~~如果存在则是最长子串的长度,否则继续判断长度为fail[fail[i]]的前缀是否符合上述情况,一直到找到就OK了。。。
一个多小时才看到此题~~~~~坑爹。。。。
代码:
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
using namespace std;
#define MAXN 1000005
char s[MAXN];
int f[MAXN];
void getfail(char *p,int len)
{
int j;
f[0]=j=-1;
for(int i=1; i<len; i++)
{
while(j>=0&&p[j+1]!=p[i]) j=f[j];
if(p[j+1]==p[i]) j++;
f[i]=j;
}
}
int find(int len)
{
int x=strlen(s);
int j=-1;
for(int i=len-1; i<x-len; i++)
{
while(j>=0&&s[j+1]!=s[i]) j=f[j];
if(s[j+1]==s[i]) j++;
if(j+1==len) return len;
}
return -1;
}
int main()
{
int n;
scanf("%d",&n);
while(n--)
{
int pp=-1;
scanf("%s",s);
if(strlen(s)<3) printf("0\n");
else
{
getfail(s,strlen(s));
int j=strlen(s)-1;
while(f[j]>=0)
{
if(f[j]+1>pp)
{
int t=find(f[j]+1);
if(t>pp)
{
pp=t;
break;
}
}
j=f[j];
}
if(pp!=-1)
printf("%d\n",pp);
else
printf("0\n");
}
}
return 0;
}
HDU4763 - Theme Section(KMP)的更多相关文章
- HDU4763 Theme Section —— KMP next数组
题目链接:https://vjudge.net/problem/HDU-4763 Theme Section Time Limit: 2000/1000 MS (Java/Others) Mem ...
- 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 ...
- 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水题)
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 ...
- 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 ...
- 【kmp算法】hdu4763 Theme Section
kmp中next数组的含义是:next[i]表示对于s[0]~s[i-1]这个前缀而言,最大相等的前后缀的长度是多少.规定next[0]=-1. 迭代for(int i=next[i];i!=-1;i ...
随机推荐
- asp.net mvc 事务处理:Transactions
1.在控制器里引用using System.Transactions; 2.在你需要事务回滚的地方外面套一层using (TransactionScope sc = new TransactionSc ...
- 使用.net 的Chart控件绘制曲线图
在进行软件开发过程中我们可能会碰到需要生成图表的情况,在.NET中以前经常用GDI去绘制,虽然效果也不错,自从.NET 4.0开始,专门为绘制图表而生的Chart控件出现了,有了它,就可以轻松的绘制你 ...
- 《javascript高级程序设计》对象图
1.原型链图 2.作用域链图 3.继承
- 《ASP.NET 本质论》HttpApplication的处理管道 ,HttpMoudle,HttpHandler
http://blog.csdn.net/sky1069/article/details/6659667 handler :http://blog.csdn.net/keymo_/article/de ...
- BZOJ 1596: [Usaco2008 Jan]电话网络
Description Farmer John决定为他的所有奶牛都配备手机,以此鼓励她们互相交流.不过,为此FJ必须在奶牛们居住的N(1 <= N <= 10,000)块草地中选一些建上无 ...
- Unity3d 调用C++的DLL
原地址:http://www.cnblogs.com/alongu3d/archive/2013/04/20/3031904.html Unity 3D 调用DLL的方法 本文转载:渡蓝的博客园 ht ...
- 用PYTHON硬写SOCKET
这个文章的系列很有意思,练练~~: http://python.jobbole.com/82763/ :) 第一步,最简单的SERVER和CLIENT.感觉和写JAVA,C的最简单的一样一样的,,看来 ...
- Java中对List集合排序的两种方法
第一种方法,就是list中对象实现Comparable接口,代码如下: public class Person implements Comparable<Person> { privat ...
- [JAVA]各种杂七杂八的东西......
BigInteger / BigDecimal / string 一些常用的函数: 加 add减 substract乘 multiply除 divid取余 mod / remainder (remin ...
- android 点滴记录 ICCID IMSI IMEI MEID 关系 和 区别,相关参数在什么情况下可以获取...
1:ICCID:Integrate circuit card identity 集成电路卡识别码(固化在手机SIM卡中) ICCID为IC卡的唯一识别号码,共有20位数字组成,其编码格式为:XXXXX ...