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. 学习写domready

    原视频参考http://www.imooc.com/learn/488 --博主个人尝试学习写的-- /** * Created by ty on 2016/1/3. */ //尝试自己写domrea ...

  2. 【css】CSS3 Media Queries 详解【转】

    说起CSS3的新特性,就不得不提到 Media Queries .最近 Max Design 更新的一个泛读列表里,赫然就有关于 Media Queries 的文章.同时位列其中的也有前天我刚刚翻译的 ...

  3. vue开发环境搭建Mac版

    一.前言 要做一个移动端app,面对webapp最流行的三个技术React,angular,vue,三选一,如何选,可参考blog移动app技术选型,react,angular, vue, 下面是对  ...

  4. L152

    For the first time, one of the new immunotherapy drugs has shown promise against breast cancer in a ...

  5. html与css与JavaScript的关系

    “HTML是网页的结构,CSS是网页的外观,而JavaScript是页面的行为.” 1)HTML—Hypertext Markup Language. 超文本标记语言.用来描述网页的语言. <h ...

  6. boost库之udp广播实例

    //UdpLinkServer.h //udp服务 #pragma once #include <boost/asio/ip/tcp.hpp> #include <boost/asi ...

  7. matlab load

    参考文献:http://jingyan.baidu.com/article/fec4bce2257963f2618d8bfa.html 对应save,load 命令更加简单. load的方式有三种: ...

  8. http协议知识整理

    HTTP 协议 作为web开发人员,了解一些http协议的知识很有必要.本文简单介绍了HTTP协议的知识,若有错误的地方,望大家指正. 1.HTTP协议是什么? http协议是一个应用层的协议.规定了 ...

  9. HDU1757

    解题思路:分析需要不少时间,比较懒,直接把别人的分析贴在这里, 然后贴上自己写的代码: K相当之大.所以逐一递推的算法无法胜任.这时我们就不得不运用矩阵加速.首先来讲一下矩阵乘法: 若一矩阵的列数与另 ...

  10. Android:BroadcastReceiver

    参考:<第一行代码:Android> 郭霖(著)   Broadcast分类 注册方式: 动态广播 在代码中注册receiver 一定要手动在onDestroy()时调用unregiste ...