http://acm.hdu.edu.cn/showproblem.php?pid=4763

Theme Section

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2129    Accepted Submission(s): 997

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

就是找到一个 类似 EAEBE 这样的结构, 其中找到 E 最长为多少

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm> using namespace std;
#define INF 0x3f3f3f3f
#define N 1000007 char s[N];
int Next[N]; void FindNext()
{
int i=, j=-;
int len = strlen(s); Next[] = -; while(i<len)
{
if(j==- || s[i]==s[j])
Next[++i] = ++j;
else
j = Next[j];
}
} bool KMP(int Start, int End)
{
int i=, j=Start;
/// i 是子串的开始, j 是母串的开始
/// 0~Start-1 是子串, Start~End-1 是母串,在母串中查找是否含有子串 while(i<Start && j<End)
{
while(i==- && (s[i]==s[j] && i<Start))
i++, j++; if(j==Start)
return true;
i = Next[i];
}
return false;
} int main()
{
int t;
scanf("%d", &t);
while(t--)
{
scanf("%s", s); FindNext(); int len = strlen(s); int j = len; while(Next[j]>)
{
/// next[j] 是最大前缀,就是母串的开始位置
/// 因为前缀也是后缀, 所以用总长度减后缀就是母串结束的位置
if(KMP(Next[j], len-Next[j])==true)
break;
j = Next[j];
} printf("%d\n", Next[j]);
}
return ;
}

(KMP灵活运用 利用Next数组 )Theme Section -- hdu -- 4763的更多相关文章

  1. Theme Section - HDU 4763(KMP)

    题目大意:给你一个串,从这个串里面找出一个前缀后缀中间相等的串的最大长度也就是 EAEBE,每个字母都代表一个串,E出现了三次,找出最长的那个E.   分析:我们知道KMP里面保存的就是前缀和后缀的最 ...

  2. Theme Section HDU - 4763(些许暴力)

    题意: 求出最长公共前后缀 不能重叠  而且 这个前后缀 在串的中间也要出现一次 解析: 再明确一次next数组的意思:完全匹配的最长前后缀长度 求一遍next 然后暴力枚举就好了 #include ...

  3. HDU 4763 Theme Section(KMP灵活应用)

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

  4. hdu 4763 Theme Section(KMP水题)

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

  5. HDU 4763:Theme Section(KMP)

    http://acm.hdu.edu.cn/showproblem.php?pid=4763 Theme Section Problem Description   It's time for mus ...

  6. HDU 4763 Theme Section (2013长春网络赛1005,KMP)

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

  7. HDU4763 Theme Section —— KMP next数组

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

  8. hdu 4763 Theme Section(next数组找串中三段相等)

    题意:在一个串中找 EAEBE 的形式的最长的E,其中E为一个字符串,也就是说找到前缀与后缀相同,并且串中还存在相同的一段,它们不能重复. 思路:利用next数组,next[len]代表的即是最大的相 ...

  9. Theme Section(KMP应用 HDU4763)

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

随机推荐

  1. scala private 和 private[this] 的区别

    class PackageStudy { private var a = 0 private[this] var b = 1 // 只能在类内部使用,对象都不能直接使用 def getb(): Int ...

  2. JSP通过表格显示数据库的信息

    [step one] 1-1 建立数据库 在jsp中,我们使用的是mysql数据库,对于此数据的优缺点本篇不予以讲述,首先建立news数据库,其数据库中表的信息为: eg:< id :1 ; n ...

  3. fiddler 发送get请求

    点击Composer 点击执行(Execute) \ 这里演示的是带cookie

  4. java线程状态及转换

    java线程有6种状态: 新建线程new,启动线程runnable,阻塞block,限时等待timed_waiting,等待线程waiting,终止线程terminated 1.限时等待timed w ...

  5. clion register

    1. 使用 activation code 激活 安装完软件后,启动,在要求输入注册码的界面(菜单栏 ⇒ help ⇒ register)选择“License server”输入“http://ide ...

  6. Python3 enumerate() 函数

    Python3 enumerate() 函数  Python3 内置函数 描述 enumerate() 函数用于将一个可遍历的数据对象(如列表.元组或字符串)组合为一个索引序列,同时列出数据和数据下标 ...

  7. INI

    .ini 文件是Initialization File的缩写,即初始化文件,是windows的系统配置文件所采用的存储格式,统管windows的各项配置,一般用户就用windows提供的各项图形化管理 ...

  8. 鼠标经过的图片高亮显示,其余变暗效果[xyytit]

    初始代码: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w ...

  9. python之面向对象之反射运用

    先看下hasattr和getattr在反射中的用法 import sys class apache(object): def __init__(self,tcp): self.tcp = tcp de ...

  10. SSH三大框架需要的配置文件

    1. Struts2框架 * 在web.xml中配置核心的过滤器 <filter> <filter-name>struts2</filter-name> <f ...