HDU4763 Theme Section 【KMP】
Theme Section
add some constraints to the rhythm of the songs, i.e., each song is required to have a 'theme section'. The theme section shall be played at the beginning, the middle, and the end of each song. More specifically, given a theme section E, the song will be in
the format of 'EAEBE', where section A and section B could have arbitrary number of notes. Note that there are 26 types of notes, denoted by lower case letters 'a' - 'z'.
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?
10^6.
5
xy
abc
aaa
aaaaba
aaxoaaaaa
0
0
1
1
2
做这题时感觉非常愉快,由于coding时用的编辑器从单调的notepad++换成了炫酷的sublime。那配色方案、主题看着舒服多了。第二个原因是这题一次性通过~。
题意:给定一个字符串,长度在10^6之内,让这个字符串去匹配EAEBE形式的串,当中AB是随意长度的串(可为0)。求E串最长是多少。
题解:首先求出给定主串的next数组。能够确定的是。若主串符合EAEBE形式。那么设next[len]==i。则在范围j属于[2*i, len-i]范围内一定有next[j]==i,这样题目就可解了。
#include <stdio.h>
#include <string.h>
#define maxn 1000002 char str[maxn];
int next[maxn]; void getNext()
{
int i = 0, j = -1;
next[0] = -1;
while(str[i]){
if(j == -1 || str[i] == str[j]){
++i; ++j;
next[i] = j; //mode 1
}else j = next[j];
}
} int KMP()
{
getNext();
int i, j, len = strlen(str); for(i = next[len]; i; i = next[i]){
for(j = i << 1; j <= len - i; ++j){
if(next[j] == i) return i;
}
} return 0;
} int main()
{
//freopen("stdin.txt", "r", stdin);
int cas;
scanf("%d", &cas);
while(cas--){
scanf("%s", str);
printf("%d\n", KMP());
}
return 0;
}
HDU4763 Theme Section 【KMP】的更多相关文章
- 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 next数组
题目链接:https://vjudge.net/problem/HDU-4763 Theme Section Time Limit: 2000/1000 MS (Java/Others) Mem ...
- 【KMP】【最小表示法】NCPC 2014 H clock pictures
题目链接: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1794 题目大意: 两个无刻度的钟面,每个上面有N根针(N<=200000),每个 ...
- 【动态规划】【KMP】HDU 5763 Another Meaning
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5763 题目大意: T组数据,给两个字符串s1,s2(len<=100000),s2可以被解读成 ...
- HDOJ 2203 亲和串 【KMP】
HDOJ 2203 亲和串 [KMP] Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- 【KMP】Censoring
[KMP]Censoring 题目描述 Farmer John has purchased a subscription to Good Hooveskeeping magazine for his ...
- 【KMP】OKR-Periods of Words
[KMP]OKR-Periods of Words 题目描述 串是有限个小写字符的序列,特别的,一个空序列也可以是一个串.一个串P是串A的前缀,当且仅当存在串B,使得A=PB.如果P≠A并且P不是一个 ...
- 【KMP】Radio Transmission
问题 L: [KMP]Radio Transmission 题目描述 给你一个字符串,它是由某个字符串不断自我连接形成的.但是这个字符串是不确定的,现在只想知道它的最短长度是多少. 输入 第一行给出字 ...
- 【kmp】似乎在梦中见过的样子
参考博客: BZOJ 3620: 似乎在梦中见过的样子 [KMP]似乎在梦中见过的样子 题目描述 「Madoka,不要相信QB!」伴随着Homura的失望地喊叫,Madoka与QB签订了契约. 这是M ...
随机推荐
- Node.js实现简单的爬取
学习[node.js]也有几天时间了,所以打算写着练练手:索然我作为一个后端的选手,写起来还有那么一丝熟悉的感觉.emmm~~ ‘货’不多讲 ,开搞........ 首先是依赖选择: 代码块如下: ...
- 计算机网络之传输层 下(TCP)
1. TCP的特点 特点:它是一个点到点的通信机制,只能有一个发送方和一个接收方:它提供是一个可靠的,按序的字节流机制:使用流水线机制,通过拥塞控制和流量控制的机制设置窗口尺寸:发送方和接收方都有缓存 ...
- js event loop事件循环
浏览器环境 以下两段代码是等价的.req对事件的回调设置,实际上就是当前主线程任务队列的任务. var req = new XMLHttpRequest(); req.open('GET', url) ...
- win7下qt error: undefined reference to `_imp__getnameinfo@28'解决
_imp__getnameinfo@28对应着winsock2.h的getnameinfo函数 首先需要导入对应的头文件 #ifndef WIN32 #include <sys/socket.h ...
- 【Python基础】迭代器、生成器
迭代器和生成器 迭代器 一 .迭代的概念 #迭代器即迭代的工具,那什么是迭代呢? #迭代是一个重复的过程,每次重复即一次迭代,并且每次迭代的结果都是下一次迭代的初始值 while True: #只是单 ...
- 大数据学习——linux常用命令(五)
1 挂载外部存储设备 可以挂载光盘.硬盘.磁带.光盘镜像文件等 1/ 挂载光驱 mkdir /mnt/cdrom 创建一个目录,用来挂载 mount -t iso9660 -o ro / ...
- 大数据学习——linux常用命令(二)四
系统管理操作 1 挂载外部存储设备 可以挂载光盘.硬盘.磁带.光盘镜像文件等 1/ 挂载光驱 mkdir /mnt/cdrom 创建一个目录,用来挂载 mount -t iso9660 ...
- python蛋疼的编码decode、encode、unicode、str、byte的问题都在这了
相信很多人和我一样,被python蛋疼的编码问题纠缠不清,比如下面的 私以为出现这种错误的原因还是对一些基本的编解码概念不够熟悉,下面就说说我的理解: 首先python刚出来的时候unicode还没有 ...
- 【UTR #2】[UOJ#278]题目排列顺序 [UOJ#279]题目交流通道 [UOJ#280]题目难度提升
[UOJ#278][UTR #2]题目排列顺序 试题描述 “又要出题了.” 宇宙出题中心主任 —— 吉米多出题斯基,坐在办公桌前策划即将到来的 UOI. 这场比赛有 n 道题,吉米多出题斯基需要决定这 ...
- OpenJudge 6042 雇佣兵
37:雇佣兵 提问 总时间限制: 1000ms 内存限制: 65536kB 描述 雇佣兵的体力最大值为M,初始体力值为0.战斗力为N.拥有X个能量元素. 当雇佣兵的体力值恰好为M时,才可以参加一个 ...