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 ...
随机推荐
- Hadoop 3.1.0 在 Ubuntu 16.04 上的安装过程
安装过程主要参考官方文档: http://hadoop.apache.org/docs/current/hadoop-project-dist/hadoop-common/SingleCluster. ...
- python和scrapy的安装【转:https://my.oschina.net/xtfjt1988/blog/364577】
抓取网站的代码实现很多,如果考虑到抓取下载大量内容scrapy框架无疑是一个很好的工具.Scrapy = Search+Pyton.下面简单列出安装过程.PS:一定要按照Python的版本下载,要不然 ...
- *Codeforces891E. Lust
$n \leq 5000$的数列,$k \leq 1e9$次操作,每次随机选一个数-1,然后把其他数的积加入答案.问最后答案期望,$mod \ \ 1e9+7$. 略微观察可以发现答案=初始数列的积- ...
- msp430项目编程50
msp430综合项目---gsm无线采集传输平台系统50 1.电路工作原理 2.代码(显示部分) 3.代码(功能实现) 4.项目总结
- docker使用 命令
Dockerfile FROM golang:alpine3. AS build-stage WORKDIR /go/src/mypro.exportReport COPY . . RUN go bu ...
- android layout
android的视图分为两类,一类是布局,另一个类是控件 一.LinearLayout(线性布局) 最常用布局之一,线性布局的特性是每添加一个控件默认会在上个控件的下面占一行. <LinearL ...
- P1717 钓鱼 洛谷
https://www.luogu.org/problem/show?pid=1717 题目描述 话说发源于小朋友精心设计的游戏被电脑组的童鞋们藐杀之后非常不爽,为了表示安慰和鼓励,VIP999决定请 ...
- 服务器Out of Memory
之前有次把图片存储在数据库,结果读取时候报错了:Out of Memory.. 图片本来不应该存储在数据库中的,消耗内存太大,既然已经这样,那就先解决问题,不改存储方式. 如果一个应用程序为了提高性能 ...
- ActiveMQ消息的延时和定时投递
ActiveMQ对消息延时和定时投递做了很好的支持,其内部启动Scheduled来对该功能支持,也提供了一个封装的消息类型:org.apache.activemq.ScheduledMessage,只 ...
- python web框架企业实战具体解释(第六期)\第三课时-ajax&jquery&webpy
main.py __author__ = 'Liao' import web import time urls = ( '/gettime','gettime', '/(.*)', 'hello' ) ...