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 ...
随机推荐
- oracle学习笔记(四) DML数据控制语言和TCL 事务控制语言
DML 数据管理语言 Data manage language insert, update, delete以及select语句,不过,有人也把select单独出来,作为DQL 数据查询语言 data ...
- WebAuthorize(中间件对所有请求进行拦截)core只有通过添加中间件过滤请求方式 而非继承然后写特性的那种方式
一.WebAuthorize 1.项目名称 WebAuthorize 2.加个中间件 过滤请求. using Microsoft.AspNetCore.Builder; using Microsoft ...
- BZOJ 3729 GTY的游戏
伪ETT? 貌似就是Splay维护dfn = = 我们首先观察这个博弈 这个博弈直接%(l+1)应该还是很显然的 因为先手怎么操作后手一定能保证操作总数取到(l+1) 于是就变成阶梯Nim了 因为对于 ...
- springboot-启动一段时间图片不能上传
问题:[B2B]后台服务.PC服务.APP服务.仓储服务,启动一段时间图片不能上传. 原因:/tmp下以tomcat开头的目录被清理了. 处理方案:1.找到涉及服务器 注:后台服务.PC服务.APP服 ...
- python基本数据预处理语法函数(1)
numpy包: ####数组###########from numpy import * shape #获取维度 size #获取长度 arange(0,5,1) #生成数组函数,从0到5以1为间隔 ...
- vue项目中数学公式的展示
在这里有个mathjax的插件,可以将dom中的数学公式展示. 第一步安装mathjax npm install mathjax 安装完之后,你会在index.html中发现,已经引用了js文件,并且 ...
- postman与soapui操作
get和post请求的区别? get请求:直接在浏览器里直接调用就可以了,不用借助工具 向服务端获取数据的 数据是放在url里面 post请求:向服务端发送数据的 数据放在body里 ...
- ajax传递对象到MVC控制器
1.view层中ajax写法: function Add2() { var model = new Object(); model.UserName = $('#UserName').val(); m ...
- MapGIS IGServer for java
但是安装完之后,服务里面没有找到igserver服务 IGServer SManager cxf 怎么会找不到类呢?是服务映射出了问题,所以目录找不到.所以怎么配置目录呢?是在xml还是环境变量,还是 ...
- PHPExcel 之常用功能
PHPExcel基本操作: 定义EXCEL实体 即定义一个PHPEXCEL对象,并设置EXCEL对象内显示内容 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 ...