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. [VS]VS快捷键

    VS快速跳到某一行:CTRL+G VS鼠标移动到下一个高亮处:Ctrl+Shift+上下箭头 VS转到定义后返回:Ctrl+- VS折叠全部代码:Ctrl.M+Ctrl.O VS代码格式化:Ctrl. ...

  2. C/C++文件指针偏移

    body, table{font-family: 微软雅黑; font-size: 10pt} table{border-collapse: collapse; border: solid gray; ...

  3. netcat 安装 及netcat 命名介绍

    netcat 安装 https://blog.csdn.net/transformer_wsz/article/details/77624087 http://www.cnblogs.com/php- ...

  4. No style sheet with given id found错误

    在chrome中打开html页面,报错No style sheet with given id found,解决方如下

  5. Git 配置ssh key的步骤

    First start by setting up your own public/private key pair set. This can use either DSA or RSA, so b ...

  6. Flexible 弹性盒子模型之CSS order 属性

    实例 设置弹性盒对象元素的顺序: div#myRedDIV {order:2;} div#myBlueDIV {order:4;} div#myGreenDIV {order:3;} div#myPi ...

  7. EventUtil对象

    var EventUtil = { addHandler : function(element,type,handler){ if(element.addEventListener){ element ...

  8. 机器人操作系统(ROS)在线实训平台学习实验指南

    机器人操作系统(ROS)在线学习指南       在高校开设ROS相关课程已经积累了一年多的经验,由于自动化类专业在课程安排中不同于计算机相关专业,通常没有Linux相关的课程基础,直接上手ROS较为 ...

  9. 【剑指offer】不使用新变量,交换两个变量的值,C++实现

    # 题目 不使用新变量,交换两个变量的值. # 思路 方法一:使用加减法操作,交换两个变量的值. A = A+B B = A-B A = A-B 方法二:使用异或运算,交换两个变量的值 A = A^B ...

  10. BZOJ4987:Tree (树形DP)

    Description 从前有棵树. 找出K个点A1,A2,…,Ak. 使得∑dis(AiAi+1),(1<=i<=K-1)最小. Input 第一行两个正整数n,k,表示数的顶点数和需要 ...