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 ...
随机推荐
- 一步步学习NHibernate(2)——配置NHibernate的环境
请注明转载地址:http://www.cnblogs.com/arhat 第二章 环境搭建 在上一章中,我们知道了NHibernate是用来干什么的了,那么今天在本章中,我们开始搭建NHibernat ...
- 制作进度条(UISlider)
怎样判断是否应当使用进度条 用进度条的主要目的是为了用一根管子的充满程度来直观地表示某种数值的百分比,进度条分为可拖动和不可拖动两种. 可拖动进度条和不可拖动进度条的原理几乎是一模一样,唯一的区别是可 ...
- WIN32api总结
1.鼠标操作: win32api.SetCursorPos((101,156)) win32api.mouse_event(win32con.MOUSEEVENT_LEFTDOWN,0,0,0,0) ...
- JavaScript闭包底层解析
1. 闭包是一个函数,这个函数有权访问另一个函数作用域中的变量,创建闭包最常见的方式,就是在函数内部创建函数.要想彻底搞清其中细节,必须从函数从创建到调用的时候都发生了什么入手 2. 函数第一次被调用 ...
- C#读取Excel五种方式的体会
原地址: http://blog.csdn.net/dapengbusi/article/details/38117817 http://blog.csdn.net/dapengbusi/articl ...
- sql 复杂自动编号错误批量修改方案
[一篮饭特稀原创,转载请注明出自http://www.cnblogs.com/wanghafan/p/5133953.html] 前提:自动编号为18位,前4位是年份,中间10位是XXXX,最后四位 ...
- easyui源码翻译1.32--ComboBox(下拉列表框)
前言 扩展自$.fn.combo.defaults.使用$.fn.combobox.defaults重写默认值对象.下载该插件翻译源码 下拉列表框显示一个可编辑文本框和下拉式列表,用户可以选择一个值或 ...
- C++中为什么要用虚函数、指针或引用才能实现多态?
原文链接:http://blog.csdn.net/zoopang/article/details/14071779 学过C++的都知道,要实现C++的多态性必须要用到虚函数,并且还要使用引用或者指针 ...
- html5 鼠标跟随运动
<!DOCTYPE HTML> <html lang="en-US"> <head> <meta charset="UTF-8& ...
- 【HDOJ】1073 Online Judge
这道题TLE了N多次,完全不明白为什么,稍微改了一下,居然过了.使用gets过的,看讨论帖有人还推荐用hash. #include <stdio.h> #include <strin ...