HDU4763 Theme Section —— KMP next数组
题目链接:https://vjudge.net/problem/HDU-4763
Theme Section
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4264 Accepted Submission(s): 2056
To get well prepared for the festival, the hosts want to know the maximum possible length of the theme section of each song. Can you help us?
xy
abc
aaa
aaaaba
aaxoaaaaa
0
1
1
2
题解:
给定一个字符串,找出能构成"EAEBE"形式的字符串的E的最长长度。其中A和B任意。
1.可知next[len]就是:即是前缀又是后缀的最长子串。所以我们就解决的首尾两个E(如果大于三分之一长度,可以通过next数组回退)。
2.剩下的问题就是:在两个E之间的连续子串中,是否能找到第三个E。
3.怎么找到?枚举里面的每个位置(注意不能与首尾的E有重叠),通过next数组的回退,一直找。详情还是看代码吧。
代码如下:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <sstream>
#include <algorithm>
using namespace std;
typedef long long LL;
const double eps = 1e-;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e9+;
const int MAXN = 1e6+; char x[MAXN];
int Next[MAXN]; void get_next(char x[], int m)
{
int i, j;
j = Next[] = -;
i = ;
while(i<m)
{
while(j!=- && x[i]!=x[j]) j = Next[j];
Next[++i] = ++j;
}
} int main()
{
int T;
scanf("%d", &T);
while(T--)
{
scanf("%s", x);
int len = strlen(x);
get_next(x, len);
int r = Next[len];
while(r> && len/r<) r = Next[r]; bool hav_ans = false;
for(; Next[r]>=; r = Next[r])
{
for(int i = *r; i<=len-r; i++)
{
int k = i; //不要写成Next[i];
while(Next[k]>r) k = Next[k];
if(Next[k]==r)
{
hav_ans = true;
break;
}
}
if(hav_ans) break;
}
printf("%d\n", r);
}
}
HDU4763 Theme Section —— KMP next数组的更多相关文章
- 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 ...
- 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 ...
- 【kmp算法】hdu4763 Theme Section
kmp中next数组的含义是:next[i]表示对于s[0]~s[i-1]这个前缀而言,最大相等的前后缀的长度是多少.规定next[0]=-1. 迭代for(int i=next[i];i!=-1;i ...
随机推荐
- Laravel 5 Form 和 HTML 的使用
最近在用 laravel 5 做例子,在做到表单的时候,习惯性的使用 Form::open() 结果发现提示错误,没有这个类, 好吧,找了找,发现 在laravel 5 中,把 from 和 html ...
- linux网络性能评估
Linux网络性能评估 参考自:自学it网,http://www.zixue.it/. 网络性能评估(1)通过ping命令检测网络的连通性.(2)通过netstat -i 组合检测网络接口状况.(3) ...
- linux的at定时任务的使用
linux的at定时任务的使用 使用at只能执行一次性任务:使用at命令需要开启atd进程. 以下情况需要安装at命令: 情况1.查看是否开启atd进程:ps -ef | grep atd.[test ...
- bzoj 2153: 设计铁路
2153: 设计铁路 Time Limit: 5 Sec Memory Limit: 259 MBSubmit: 295 Solved: 172[Submit][Status][Discuss] ...
- Maven使用site-deploy(site:deploy)部署通过site生成的文档(Tomcat-WebDAV)
Maven可以通过site生成项目的帮助文档,并且格式为html,那么可以通过site-deploy把文档部署到远端,部署方式支持HTTP/FTP/SCM/WebDAV等. 更多部署方案,参考:htt ...
- 【Android开发—智能家居系列】(四):UDP通信发送指令
思路回顾 [1]手机连接WIFI模块 [2]UDP通信对WIFI模块发送指令,以和WIFI模块保持连接状态 [3]UDP通信对WIFI模块发送指令,让其搜索可用的无线网,返回WIFI列表 [4]发送指 ...
- Python基础语法04-数据结构
Python Number(数字) Python Number 数据类型用于存储数值. 数据类型是不允许改变的,这就意味着如果改变 Number 数据类型的值,将重新分配内存空间. Python 支持 ...
- [转]gzip,bzip2,tar,zip命令使用方法详解
原文:http://blog.chinaunix.net/uid-20779720-id-2547669.html 1 gzipgzip(1) 是GNU的压缩程序.它只对单个文件进行压缩.基本用法如下 ...
- 从机器码理解RIP 相对寻址
本作品采用知识共享署名 4.0 国际许可协议进行许可.转载联系作者并保留声明头部与原文链接https://luzeshu.com/blog/rip-relative-addressing 本博客同步在 ...
- 深入研究Clang(五) Clang Lexer代码阅读笔记之Lexer
作者:史宁宁(snsn1984) Clang的Lexer(词法分析器)的源代码的主要位置例如以下: clang/lib/Lex 这里是基本的Lexer的代码: clang/include/cla ...