Theme Section

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 1114    Accepted Submission(s): 579
Problem Description
It's time for music! A lot of popular musicians are invited to join us in the music festival. Each of them will play one of their representative songs. To make the programs more interesting and challenging, the hosts are going to
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?
 
Input
The integer N in the first line denotes the total number of songs in the festival. Each of the following N lines consists of one string, indicating the notes of the i-th (1 <= i <= N) song. The length of the string will not exceed
10^6.
 
Output
There will be N lines in the output, where the i-th line denotes the maximum possible length of the theme section of the i-th song.
 
Sample Input
5
xy
abc
aaa
aaaaba
aaxoaaaaa
 
Sample Output
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】的更多相关文章

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

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

  2. HDU4763 Theme Section —— KMP next数组

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

  3. 【KMP】【最小表示法】NCPC 2014 H clock pictures

    题目链接: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1794 题目大意: 两个无刻度的钟面,每个上面有N根针(N<=200000),每个 ...

  4. 【动态规划】【KMP】HDU 5763 Another Meaning

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5763 题目大意: T组数据,给两个字符串s1,s2(len<=100000),s2可以被解读成 ...

  5. HDOJ 2203 亲和串 【KMP】

    HDOJ 2203 亲和串 [KMP] Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...

  6. 【KMP】Censoring

    [KMP]Censoring 题目描述 Farmer John has purchased a subscription to Good Hooveskeeping magazine for his ...

  7. 【KMP】OKR-Periods of Words

    [KMP]OKR-Periods of Words 题目描述 串是有限个小写字符的序列,特别的,一个空序列也可以是一个串.一个串P是串A的前缀,当且仅当存在串B,使得A=PB.如果P≠A并且P不是一个 ...

  8. 【KMP】Radio Transmission

    问题 L: [KMP]Radio Transmission 题目描述 给你一个字符串,它是由某个字符串不断自我连接形成的.但是这个字符串是不确定的,现在只想知道它的最短长度是多少. 输入 第一行给出字 ...

  9. 【kmp】似乎在梦中见过的样子

    参考博客: BZOJ 3620: 似乎在梦中见过的样子 [KMP]似乎在梦中见过的样子 题目描述 「Madoka,不要相信QB!」伴随着Homura的失望地喊叫,Madoka与QB签订了契约. 这是M ...

随机推荐

  1. git命令使用(三)

    git的使用--分支的使用 我们都知道拉取代码的时候,拉下来的是默认的分支,但我们需要的是,其他分支的使用操作 开始,拉取项目 git clone url 查看分支,显示默认分支 git branch ...

  2. 四、StaticList 和 DynamicList

    1.StaticList类模板 StaticList的设计要点:类模板 使用原生数组作为顺序存储空间 使用模板参数决定数组大小 template <typename T, int N> c ...

  3. transformer模型解读

    最近在关注谷歌发布关于BERT模型,它是以Transformer的双向编码器表示.顺便回顾了<Attention is all you need>这篇文章主要讲解Transformer编码 ...

  4. 上海苹果维修点分享苹果电脑MACBOOK故障维修常见案例

    苹果的电子设备无论是外观和性能都是无与伦比的美丽,很多开发者都开始选用苹果电脑macbook.近年来苹果售后维修点来维修苹果电脑的用户也越来越多,我们上海苹果维修点就整理分享了一些苹果电脑MACBOO ...

  5. day17-python之文件操作

    1.内置函数 #!/usr/bin/env python # -*- coding:utf-8 -*- # print(abs(-1)) # print(abs(1)) # # print(all([ ...

  6. 03 数据解析-Xpath

    Xpath解析 XPath在Python的爬虫学习中,起着举足轻重的地位,对比正则表达式 re两者可以完成同样的工作,实现的功能也差不多,但XPath明显比re具有优势,在网页分析上使re退居二线. ...

  7. Spring MVC 接入 rabbitMQ

    依赖包 <dependency> <groupId>org.springframework.amqp</groupId> <artifactId>spr ...

  8. CentOS 6.5 x64 安装MySql 5.6

    1.检测是否已经安装MySQL,输入以下命令 rpm -qa | grep mysql 如果存在,我们输入以下命令来删除 //强力删除 rpm -e --nodeps mysql 2.安装前环境准备 ...

  9. 洛谷P1276 校门外的树(增强版)未完工

    题目描述 校门外马路上本来从编号0到L,每一编号的位置都有1棵树.有砍树者每次从编号A到B处连续砍掉每1棵树,就连树苗也不放过(记 0 A B ,含A和B):幸运的是还有植树者每次从编号C到D 中凡是 ...

  10. bzoj 1432 [ZJOI2009]Function 思想

    [bzoj1432][ZJOI2009]Function Description Input 一行两个整数n; k. Output 一行一个整数,表示n 个函数第k 层最少能由多少段组成. Sampl ...