给一个串,让你找一个子串,形如EAEBE,就是一个串在开头结尾中间各出现一次,问这个E最长是多少

Sample Input
5
xy
abc
aaa
aaaaba
aaxoaaaaa

Sample Output
0
0
1
1
2

 # include <cstdio>
# include <cstring>
using namespace std; char S[] ;
int next[] ;
int slen ;
int tlen ; void getNext()
{
int j, k;
j = ; k = -; next[] = -;
while(j < slen)
if(k == - || S[j] == S[k])
next[++j] = ++k;
else
k = next[k]; } int main ()
{
int T ;
scanf("%d" , &T);
while (T--)
{
scanf("%s" , S) ;
slen = strlen(S) ;
getNext() ;
int t = slen ;
int ans = ;
while (t > slen/)
t = next[t] ;
while(t)
{
tlen = t ;
int i ;
int flag = ; for(i = t* ; i <= slen - t ; i++)//利用next数组的性质
{
if (next[i] == tlen)
{
ans = tlen ;
flag = ;
break ;
}
}
if (flag)
t = next[t] ;
else
break ;
}
printf("%d\n" , ans) ; } return ;
} # include <cstdio>
# include <cstring>
using namespace std; char S[] ;
int next[] ;
int slen ;
int tlen ; void getNext()
{
int j, k;
j = ; k = -; next[] = -;
while(j < slen)
if(k == - || S[j] == S[k])
next[++j] = ++k;
else
k = next[k]; } bool kmp(int a , int b)
{ int i, j = ; for(i = a; i <= b; i++)
{
while(j > && S[i] != S[j])
j = next[j];
if(S[i] == S[j])
j++;
if(j == tlen)
{
return true ;
}
}
return false;
} int main ()
{
int T ;
scanf("%d" , &T);
while (T--)
{
scanf("%s" , S) ;
slen = strlen(S) ;
getNext() ;
int t = slen ;
int ans = ;
while (t > slen/)
t = next[t] ;
while(t)
{
tlen = t ;
int flag = kmp(t ,slen - t - ) ;//查看模式串是否匹配主串
if (flag)
{
ans = t ;
break ;
}
t = next[t] ;
}
printf("%d\n" , ans) ; } return ;
}

HDU 4763 求最大长度的串A,使得S满足APAQA的更多相关文章

  1. 链表插入和删除,判断链表是否为空,求链表长度算法的,链表排序算法演示——C语言描述

    关于数据结构等的学习,以及学习算法的感想感悟,听了郝斌老师的数据结构课程,其中他也提到了学习数据结构的或者算法的一些个人见解,我觉的很好,对我的帮助也是很大,算法本就是令人头疼的问题,因为自己并没有学 ...

  2. C++ 数组长度 以及 数组名作为参数传递给函数 以及 为什么不在子函数中求数组长度

    在看排序,首先是插入排序,思路理清后想用代码实现,然后问题来了: 如何求数组长度? 如果没记错,在Java中应该是有直接可用的方法的, Python中(序列)也有.len,在C/C++中,字符串倒是有 ...

  3. 求任意长度数组的最大值(整数类型)。利用params参数实现任意长度的改变。

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  4. 求字符串长度 strlen(数组指针两种方式)

    问题: 求字符串中所含有字符的个数(包括空格),即求字符串长度: #include <stdio.h> #include <assert.h> int _strlen(cons ...

  5. HDU 5008 求第k小子串

    本题要求第k小的distinct子串,可以根据height数组,二分出这个第k小子串所在后缀的位置信息.由于题目要求子串起始下标尽可能小.所以再在rank数组中,二分出与当前后缀LCP大于等于所求子串 ...

  6. C陷阱:求数组长度

    // 这是一篇导入进来的旧博客,可能有时效性问题. 程序中,当我们建立了一个int型数组:int a[]={1,2,3,4,5,6};随后我们可能需要知道它的长度,此时可以用这种方法:length = ...

  7. Java50道经典习题-程序38 求字符串长度

    题目:写一个函数,求一个字符串的长度,在main函数中输入字符串,并输出其长度. import java.util.Scanner; public class Prog38 { public stat ...

  8. JAVA 基础编程练习题38 【程序 38 求字符串长度】

    38 [程序 38 求字符串长度] 题目:写一个函数,求一个字符串的长度,在 main 函数中输入字符串,并输出其长度. package cskaoyan; public class cskaoyan ...

  9. Java例题_38 自定义函数求字符串长度

    1 /*38 [程序 38 求字符串长度] 2 题目:写一个函数,求一个字符串的长度,在 main 函数中输入字符串,并输出其长度. 3 */ 4 5 /*分析 6 * 1.从键盘得到一个字符串 7 ...

随机推荐

  1. Neural Networks and Deep Learning(week4)Building your Deep Neural Network: Step by Step

    Building your Deep Neural Network: Step by Step 你将使用下面函数来构建一个深层神经网络来实现图像分类. 使用像relu这的非线性单元来改进你的模型 构建 ...

  2. spring集成ActiveMQ居然要依赖这么多包

    做spring和ActiveMQ的集成,作maven依赖的时候有感(以前都不在乎,现在不一样了........省略) <!-- https://mvnrepository.com/artifac ...

  3. VUE2.0 饿了吗视频学习笔记(七-终):compute,循环,flex,display:table

    一.star组件使用到了computed属性 computed相当于属性的一个实时计算,当对象的某个值改变的时候,会进行实时计算. computed: { starType() { return 's ...

  4. Leanote 蚂蚁笔记 云笔记

    Leanote 蚂蚁笔记  1.Leanote 数据存储是使用mongo存放的,所以需要安装mongo包 1.MongoDB 数据存储 Leanote 依赖 MongoDB 作为数据存储,下面开始安装 ...

  5. 记录一个PHP安装redis扩展时的问题

    安装过程:https://www.cnblogs.com/pengyunjing/p/8688320.html 由于我之前安装过该扩展,重新安装时没有执行make clean命令,所以安装好出现了下面 ...

  6. mod_wsgi 的两种模式

    mod_wsgi 的两种模式 http://ssmax.net/archives/977.html http://www.cnblogs.com/yuxc/p/3555005.html mod_wsg ...

  7. mysql 案例~mysql主从复制延迟处理(2)

    一 简介:今天来聊聊周期性从库延迟的问题,是上一篇的基础分析的一个场景 二 背景:近期每天的指定时间段,收到从库延迟的报警,然后过一段时间恢复.由于从库是提供读服务的,所以需要解决 三 分析思路: 1 ...

  8. Junit的Assert用法

    package junit.framework; /** * A set of assert methods. Messages are only displayed when an assert f ...

  9. python3爬虫一

    1.环境安装: 在py3env(虚拟环境)下安装scrapy,新建项目: pip install scrapy scrapy startproject ArticleSpider cd Article ...

  10. setfacl报错Operation not supported

    对文件目录setfacl权限设置时报错Operation not supported Google一下,发现是分区acl权限问题 一般情况下(ext4),默认acl支持都是加载的.但如果遇到二般情况, ...