kmp(前中后最长相同长度)
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): 5815 Accepted Submission(s): 2890
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?
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.
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.
xy
abc
aaa
aaaaba
aaxoaaaaa
0
1
1
2
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <algorithm>
#include <iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include <stdio.h>
#include <string.h>
#define INF 10000000
using namespace std;
char a[] , b[], str[];
int ans = ; void getnext(char *a , int len , int *next)
{
next[] = - ;
int k = - , j = ;
while(j < len)
{
if(k == - || a[j] == a[k])
{
k++;
j++;
next[j] = k ;
}
else
{
k = next[k];
}
}
} int main()
{
int n ;
scanf("%d" , &n);
while(n--)
{
int next[];
int next1[];
scanf("%s" , a);
int len = strlen(a);
getnext(a , len , next);
int q = next[len];
while(q > )
{
if(q * > len)
{
q = next[q];
continue ;
}
for(int i = ; i < q ; i++)
{
str[i] = a[i] ;
}
getnext(a , q , next1);
int j = , i = q ;
while(i < len - q && j < q)
{
if(j == - || str[j] == a[i])
{
i++;
j++;
}
else
{
j = next[j] ;
}
}
if(j == q)
{
ans = q;
break ;
}
q = next[q]; }
printf("%d\n" , q);
} return ;
}
kmp(前中后最长相同长度)的更多相关文章
- Qt实现 动态化遍历二叉树(前中后层次遍历)
binarytree.h 头文件 #ifndef LINKEDBINARYTREE_H #define LINKEDBINARYTREE_H #include<c++/algorithm> ...
- 二叉树前中后/层次遍历的递归与非递归形式(c++)
/* 二叉树前中后/层次遍历的递归与非递归形式 */ //*************** void preOrder1(BinaryTreeNode* pRoot) { if(pRoot==NULL) ...
- [C++] 非递归实现前中后序遍历二叉树
目录 前置技能 需求描述 binarytree.h 具体实现 binarytree.cpp main.cpp 网上代码一搜一大片,大同小异咯. 书上的函数实现代码甚至更胜一筹,而且抄一遍就能用,唯一问 ...
- 前中后序递归遍历树的体会 with Python
前序:跟->左->右 中序:左->根->右 后序:左>右->根 采用递归遍历时,编译器/解释器负责将递归函数调用过程压入栈并保护现场,在不同位置处理根节点即可实现不 ...
- 【C++】二叉树的遍历(前中后)- 迭代法
力扣题目:https://leetcode-cn.com/problems/binary-tree-inorder-traversal/ 今天自己琢磨了很久如何不用递归将二叉树的遍历写出来,于是乎写出 ...
- C++实现对树的创建和前中后序遍历
#include<iostream>#include<stdio.h> using namespace std; class BitNode{ public: char dat ...
- java实现二叉树的前中后遍历(递归和非递归)
这里使用下图的二叉树作为例子: 首先建立树这个类: public class Node { private int data; private Node leftNode; private Node ...
- 数据结构-C语言递归实现树的前中后序遍历
#include <stdio.h> #include <stdlib.h> typedef struct tree { int number ; struct tree *l ...
- Java 非递归实现 二叉树的前中后遍历以及层级遍历
class MyBinaryTree<T> { BinaryNode<T> root; public MyBinaryTree() { root = new BinaryNod ...
随机推荐
- 线程屏障CyclicBarrier实现原理
生产环境中,存在需要等待多个线程都达到某种状态后,才继续运行的情景.并发工具CyclicBarrier就能够完成这种功能.本篇从源码方面,简要分析CyclicBarrier的实现原理. 使用示例 pu ...
- 解决SVN异常 cleanup failed
winndows上偶尔使用SVN的时候就会整出一些有的没的问题,比如"cleanup failed to process the following paths previous opera ...
- nginx安装配置目录
1.nginx日志轮转,用于lograte服务的日志切割. /etc/logrotate.d/nginx 2.cgi配置相关 /etc/nginx/fastcgi_params /etc/nginx/ ...
- phpStorm 配置PHP_CodeSniffer自动检查代码
环境 ubuntu18.4 phpstorm php7.2 最正确安装方法 sudo apt-get install php-codesniffer 一.composer安装PHP_CodeSniff ...
- flask之Twitter Bootstrap
一:Twitter Bootstrap是什么? 1.开源框架:提供用户页面组件. 2.可以创建整洁且具有吸引力的网站,并且网站能兼容所有现代的Web浏览器. 特点: Bootstrap 是客户端框架, ...
- SpringBoot 参数校验
一.添加依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId> ...
- QTimer不能同时使用两个,用QTimerEvent (QT)
最近写程序的时候有个界面想定两个QTimer定时器,不同时间干不同的事: QTimer *timer1 = new QTimer(this); QTimer *timer2 = new QTimer( ...
- Django2 --- cookie
1. 什么是cookie ?用途是什么? Cookies是一些存储在用户电脑上的小文件.它是被设计用来保存一些站点的用户数据,这样能够让服务器为这样的用户定制内容,后者页面代码能够获取到Cookie值 ...
- pl/sql中return和exit区别
经测试: 1.exit只能用于循环中,并且退出循环往下执行: 2.return可用于循环或非循环,并且退出整个程序模块不往下执行. declare i number :=1; j number :=1 ...
- 31 October
https://www.cnblogs.com/RabbitHu/p/51nod1353.html 树形 DP 求所有联通块 \(\ge K\) 的方案数. 切断:\(\forall i\in\lef ...