Theme Section

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

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
 
思路:本题考查对KMP算法中的next函数的理解。对于字符串s来说,next[l]表示在字符串s中使前l个字符的前缀与后缀相等最长长度。
#include <cstdio>
#include <cstring>
using namespace std;
const int MAXN = ;
char buf[MAXN], fa[MAXN], son[MAXN];
int net[MAXN], len;
void getNext(char s[])
{
int k = -;
int i = ;
net[] = -;
while(i < len)
{
if(k == - || s[i] == s[k])
{
i++;
k++;
net[i] = k;
}
else
{
k = net[k];
}
}
}
int main()
{
int T;
scanf("%d", &T);
while(T--)
{
scanf("%s", buf);
len = strlen(buf);
if(len < )
{
printf("0\n");
continue;
}
getNext(buf);
int l = net[len];
while(l > && * l > len) l = net[l];
while(l > )
{
int size = ;
for(int i = l; i < len - l; i++)
{
fa[size++] = buf[i];
}
fa[size] = '\0';
for(int i = ; i < l; i++)
{
son[i] = buf[i];
}
son[l] = '\0';
if(strstr(fa, son) != NULL)
{
break;
}
l = net[l];
}
printf("%d\n", l);
}
return ;
}
 

HDOJ4763(KMP原理理解)的更多相关文章

  1. JUC回顾之-ConcurrentHashMap源码解读及原理理解

    ConcurrentHashMap结构图如下: ConcurrentHashMap实现类图如下: segment的结构图如下: package concurrentMy.juc_collections ...

  2. POJ1523(割点所确定的连用分量数目,tarjan算法原理理解)

    SPF Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 7406   Accepted: 3363 Description C ...

  3. java的classLoader原理理解和分析

    java的classLoader原理理解和分析 学习了:http://blog.csdn.net/tangkund3218/article/details/50088249 ClassNotFound ...

  4. 【KMP原理】【整理回顾】

    今儿套KMP模板做了个题,敏敏找我讲next[]数组的时候把我问懵了.具体原理都记不清了光靠模板凑得了一时凑不了一世啊,所以再捋一捋顺一顺,这次印象要深刻一点了: KMP与暴力匹配的优化区别就不再提了 ...

  5. js 闭包原理理解

    问题?什么是js(JavaScript)的闭包原理,有什么作用? 一.定义 官方解释:闭包是一个拥有许多变量和绑定了这些变量的环境的表达式(通常是一个函数),因而这些变量也是该表达式的一部分. 很显然 ...

  6. 关于KMP算法理解(快速字符串匹配)

    参考:http://www.ruanyifeng.com/blog/2013/05/Knuth%E2%80%93Morris%E2%80%93Pratt_algorithm.html 2016-08- ...

  7. KMP原理、分析及C语言实现

    (是在matrix67博客基础上整理而来,整理着:华科小涛@http://www.cnblogs.com/hust-ghtao/) 有些算法可以让人发疯,KMP算法就是一个.在网上找了很多资料讲的都让 ...

  8. kalman filter卡尔曼滤波器- 数学推导和原理理解-----网上讲的比较好的kalman filter和整理、将预测值和观测值融和

    = 参考/转自: 1 ---https://blog.csdn.net/u010720661/article/details/63253509 2----http://www.bzarg.com/p/ ...

  9. [转]KMP算法理解及java实现

    这大概是我看的最好懂的KMP算法讲解了,不过我还只弄懂了大概思想,算法实现我到时候用java实现一遍 出处:知乎 https://www.zhihu.com/question/21923021/ans ...

随机推荐

  1. LNMP的搭建与原理

    常见的web框架结构:例如:lnmp和:ampL=LINUX N=NGINX A=APACHE P=php T=Tomcat lnmp的原理 在LNMP组合工作时,首先是用户通过浏览器输入域名请求Ng ...

  2. ZOJ 1609 Equivalence(状压+dfs减枝)

    ZOJ Problem Set - 1609 Equivalence Time Limit: 5 Seconds      Memory Limit: 32768 KB When learning m ...

  3. L169 Komodo dragons

    Komodo dragons live on a handful of islands in Indonesia, but their reputation has spread far and wi ...

  4. Java之JVM逃逸分析

    引言: 逃逸分析(Escape Analysis)是众多JVM技术中的一个使用不多的技术点,本文将通过一个实例来分析其使用场景. 概念 逃逸分析,是一种可以有效减少Java 程序中同步负载和内存堆分配 ...

  5. 从JDK源码角度看Byte

    Java的Byte类主要的作用就是对基本类型byte进行封装,提供了一些处理byte类型的方法,比如byte到String类型的转换方法或String类型到byte类型的转换方法,当然也包含与其他类型 ...

  6. angular使用遇到的问题

    http://www.angularjs.cn/A0a6 用路由的时候,在js里写jquery的事件dom操作失效,只有在子页面里嵌套js才生效(jquery也不行).解决办法就是把dom操作用ng- ...

  7. 【pandas】pandas.to_datatime()---时间格式转换

    标准时间格式:2012-12-21 时间转换函数:pandas.to_datatime() # -*- coding: utf- -*- # 生成数据 import pandas as pd data ...

  8. 使用css固定table第一列

    .table{width:100%;overflow-x: scroll;background-color:#7c95b5;} .fixedTable{width:160%;text-align: c ...

  9. using中StreamWriter XmlWriter 区别

    使用StreamWriter using (var writer = new StreamWriter(File.Create(path))) { writer.WriteLine("sdf ...

  10. 21天学通C++_Day6

    0.指针&数组 数组是指向其第一个元素的指针,即数组变量就是指针.故可将(*)用于数组,也可将([])用于指针,eg: int MyNums[5] = {0}; int* pNums = My ...