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 ...
随机推荐
- iphone下overflow失效问题的解决方法
overflow-y: auto; -webkit-overflow-scrolling:touch; /*加上这个让浏览器支持touch和自动滚动这样界面就可以滚动了*/
- css3创建动画
@keyframes ico{ 0% { top: -100%; } 100%{ top:4%; } } @-webkit-keyframes ico{ 0% { top: -100%; } 100% ...
- 服务器部署_linuix下 一台nginx 多域名
近日朋友要我帮他调服务器, 一. 初步需求如下: 1. 一台服务器下要放三个应用,对应三个域名:www.aaa.com,www.bbb.com,www.ccc.com. 2. 其中后两个应用也要可以用 ...
- [Gauss]HDOJ3976 Electric resistance
题意: 一看图就明白了 要求的是1与n端点间的等效电阻 重点在于转化成考虑电流 根据KCL定理:在任一瞬间流出(流入)该节点的所有电流的代数和恒为零 U = IR 可以令1点的电势为零 那么n点的电势 ...
- ANDROID_MARS学习笔记_S01原始版_020_Mp3player001_歌曲列表
一.项目设计 二.歌曲列表简介 1.利用java.net.HttpURLConnection以流的形式下载xml文件为String 2.自定义ContentHandler-->Mp3ListCo ...
- 关于shell环境变量的思考
shell中执行sh文件时需要开子进程,因此export的环境变量在子进程中,若想要sh中定义的脚本生效,使用source命令,并且可以被子进程继承.而子进程export的内容父进程获取不到
- Ember.js demo8
<!DOCTYPE html> <html> <head> <meta name="description" content=" ...
- BOM List demo
select level level_id, t.* from (select msi1.segment1 farther_item, msi1.inv ...
- Hibernate一级缓存、二级缓存
缓存就是把以前从数据库中查询出来和使用过的对象保存在内存中,准确说就是一个数据结构中,这个数据结构通常是或类似HashMap,当以后要使用某个对象时,先查询缓存中是否有这个对象,如果有则使用缓存中的对 ...
- 海量数据的二度人脉挖掘算法(Hadoop 实现)
最近做了一个项目,要求找出二度人脉的一些关系,就好似新浪微博的“你可能感兴趣的人” 中,间接关注推荐:简单描述:即你关注的人中有N个人同时都关注了 XXX . 在程序的实现上,其实我们要找的是:若 U ...