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

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
 
Source
 
Recommend
liuyiding
 题意:求前中后最长相同串长度,不能有重叠,
我还以为我这种方法会超时,没想到过了。!!
#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(前中后最长相同长度)的更多相关文章

  1. Qt实现 动态化遍历二叉树(前中后层次遍历)

    binarytree.h 头文件 #ifndef LINKEDBINARYTREE_H #define LINKEDBINARYTREE_H #include<c++/algorithm> ...

  2. 二叉树前中后/层次遍历的递归与非递归形式(c++)

    /* 二叉树前中后/层次遍历的递归与非递归形式 */ //*************** void preOrder1(BinaryTreeNode* pRoot) { if(pRoot==NULL) ...

  3. [C++] 非递归实现前中后序遍历二叉树

    目录 前置技能 需求描述 binarytree.h 具体实现 binarytree.cpp main.cpp 网上代码一搜一大片,大同小异咯. 书上的函数实现代码甚至更胜一筹,而且抄一遍就能用,唯一问 ...

  4. 前中后序递归遍历树的体会 with Python

    前序:跟->左->右 中序:左->根->右 后序:左>右->根 采用递归遍历时,编译器/解释器负责将递归函数调用过程压入栈并保护现场,在不同位置处理根节点即可实现不 ...

  5. 【C++】二叉树的遍历(前中后)- 迭代法

    力扣题目:https://leetcode-cn.com/problems/binary-tree-inorder-traversal/ 今天自己琢磨了很久如何不用递归将二叉树的遍历写出来,于是乎写出 ...

  6. C++实现对树的创建和前中后序遍历

    #include<iostream>#include<stdio.h> using namespace std; class BitNode{ public: char dat ...

  7. java实现二叉树的前中后遍历(递归和非递归)

    这里使用下图的二叉树作为例子: 首先建立树这个类: public class Node { private int data; private Node leftNode; private Node ...

  8. 数据结构-C语言递归实现树的前中后序遍历

    #include <stdio.h> #include <stdlib.h> typedef struct tree { int number ; struct tree *l ...

  9. Java 非递归实现 二叉树的前中后遍历以及层级遍历

    class MyBinaryTree<T> { BinaryNode<T> root; public MyBinaryTree() { root = new BinaryNod ...

随机推荐

  1. Spark MLlib机器学习(一)——决策树

    决策树模型,适用于分类.回归. 简单地理解决策树呢,就是通过不断地设置新的条件标准对当前的数据进行划分,最后以实现把原始的杂乱的所有数据分类. 就像下面这个图,如果输入是一大堆追求一个妹子的汉子,妹子 ...

  2. python基础--5字典

    #字典#dict#基本结构info={ "k1":'v1' #键值对 ,"k2":"v2"} #字典的value可以是任何值# info={ ...

  3. 2018ICPC银川 L Continuous Intervals 单调栈 线段树

    题意:给你一个序列,问你这个序列有多少个子区间,满足把区间里的数排序之后相邻两个数之间的差 <= 1 ? 思路:https://blog.csdn.net/u013534123/article/ ...

  4. 1146. Topological Order (25)

    This is a problem given in the Graduate Entrance Exam in 2018: Which of the following is NOT a topol ...

  5. [sql 注入] 注入类型

    基于整型的注入: url:http://localhost/?id=12 拼接sql:$sql = "select * from user where id = {$_GET['id']}& ...

  6. CS与BS的比较

    对象 硬件环境 客户端要 求           软件安装 升级和维护 安全性 C/S 用户固定,并且处于相同区域, 要求拥有相同的操作系统. 客户端的计算机电脑配置要求较高. 每一个客户端都必须安装 ...

  7. js把两个对象合并成一个对象

    Object.assign() 方法用于将所有可枚举属性的值从一个或多个源对象复制到目标对象.它将返回目标对象 语法: Object.assign(target, ...sources)参数 targ ...

  8. Redis之数据类型

    一.概念: Redis:一个开源.支持网络.基于内存.键值对存储数据库. 特点:它可以支持多种数据类型. 二.数据类型 1)Redis String 具体说明: 一般的普通的k到v一个映射是Strin ...

  9. django 我的博客 (慕课网视频)笔记

    用到的命令 1.创建项目 django-admin startproject myBlog 2.创建appcd [项目名] python3 manage.py startapp blog 3.数据迁移 ...

  10. C# 与 C++,语法差别有多小-其他2

    1. 内存分配 C++:指针在堆区(new),定义在栈区 C#:  对象在堆区,值在栈区.不允许不通过new在构造对象 2.变量 C++: char 1个字节 ascii C#:   char 2个字 ...