题目描述

给定一个字符串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)的更多相关文章

  1. HDU4763 Theme Section —— KMP next数组

    题目链接:https://vjudge.net/problem/HDU-4763 Theme Section Time Limit: 2000/1000 MS (Java/Others)    Mem ...

  2. HDU-4763 Theme Section KMP

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

  3. Theme Section(KMP应用 HDU4763)

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

  4. HDU4763 Theme Section 【KMP】

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

  5. hdu 4763 Theme Section(KMP水题)

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

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

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

  7. hdu4763 Theme Section

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

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

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

  9. 【kmp算法】hdu4763 Theme Section

    kmp中next数组的含义是:next[i]表示对于s[0]~s[i-1]这个前缀而言,最大相等的前后缀的长度是多少.规定next[0]=-1. 迭代for(int i=next[i];i!=-1;i ...

随机推荐

  1. 2014年度辛星html教程夏季版第七节

    经过前面六节的学习,我们大致清楚了HTML教程中的基础内容,那么接下来我们开始继续向后推进,可以说,下面我们介绍一下HTML中的区块. ***************区块*************** ...

  2. 开发设计模式(四) 代理模式(Proxy Pattern)

    转自http://blog.sina.com.cn/s/blog_89d90b7c0101803g.html 代理模式:代理模式的主要作用是为其他对象提供一种代理以控制对这个对象的访问.在某些情况下, ...

  3. iOS8的一些控件的变更---备用

    UISearchDisplayController变更为UISearchController UIAlertView变更为UIAlertController 如果添加点击事件则需要使用UIAlertC ...

  4. 教你6步定制你的Ubuntu桌面

    转自教你6步定制你的Ubuntu桌面 对于那些想要一个易于使用的界面的用户,Ubuntu是一个很好的Linux发行版,并且对于一个Linux新手也可以说是最好的Linux发行版.不过这产生了一些副作用 ...

  5. 数据库里面DataTime时间类型字段,如果为null时

    tran.TransactionTime = bet.CreationDate.ToString() == "0001/1/1 0:00:00" ? DateTime.Now : ...

  6. HDU3400+三分

    三分出两个中间的位置即可. /* 两次三分 */ #include<stdio.h> #include<string.h> #include<stdlib.h> # ...

  7. 一个用于清除loadrunner产生log文件的批处理

    @echo off set work_path="%~dp0" for /R %%s in (*.txt,*.log) do ( del /f "%%s" ) ...

  8. 第六章Audio设备

    6.1 Audio设备介绍 USB协议制定时,为了方便不同设备的开发商基于USB进行设计,定义了不同的设备类来支持不同类型的设备.虽然在USB标准中定义了USB_DEVICE_CLASS_AUDIO- ...

  9. VCL+FMX 双剑合壁编程

    VCL 是经典,FMX 是新生,新生事物总会带来一些好玩新奇的东西.舍弃经典是浪费,不了解新生事物是等死,那么我们来一个二合一双剑合壁又如何呢? 要双剑合壁,就得投些机,取些巧.由于 Delphi / ...

  10. DMG提取安装文件

    打开原版的DMG文件,选中安装文件,按下鼠标右键,选择显示包内容 <ignore_js_op> 按照下图的路径,一直找到installESD.dmg,双击打开它<ignore_js_ ...