HDU 4763 求最大长度的串A,使得S满足APAQA
给一个串,让你找一个子串,形如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的更多相关文章
- 链表插入和删除,判断链表是否为空,求链表长度算法的,链表排序算法演示——C语言描述
关于数据结构等的学习,以及学习算法的感想感悟,听了郝斌老师的数据结构课程,其中他也提到了学习数据结构的或者算法的一些个人见解,我觉的很好,对我的帮助也是很大,算法本就是令人头疼的问题,因为自己并没有学 ...
- C++ 数组长度 以及 数组名作为参数传递给函数 以及 为什么不在子函数中求数组长度
在看排序,首先是插入排序,思路理清后想用代码实现,然后问题来了: 如何求数组长度? 如果没记错,在Java中应该是有直接可用的方法的, Python中(序列)也有.len,在C/C++中,字符串倒是有 ...
- 求任意长度数组的最大值(整数类型)。利用params参数实现任意长度的改变。
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- 求字符串长度 strlen(数组指针两种方式)
问题: 求字符串中所含有字符的个数(包括空格),即求字符串长度: #include <stdio.h> #include <assert.h> int _strlen(cons ...
- HDU 5008 求第k小子串
本题要求第k小的distinct子串,可以根据height数组,二分出这个第k小子串所在后缀的位置信息.由于题目要求子串起始下标尽可能小.所以再在rank数组中,二分出与当前后缀LCP大于等于所求子串 ...
- C陷阱:求数组长度
// 这是一篇导入进来的旧博客,可能有时效性问题. 程序中,当我们建立了一个int型数组:int a[]={1,2,3,4,5,6};随后我们可能需要知道它的长度,此时可以用这种方法:length = ...
- Java50道经典习题-程序38 求字符串长度
题目:写一个函数,求一个字符串的长度,在main函数中输入字符串,并输出其长度. import java.util.Scanner; public class Prog38 { public stat ...
- JAVA 基础编程练习题38 【程序 38 求字符串长度】
38 [程序 38 求字符串长度] 题目:写一个函数,求一个字符串的长度,在 main 函数中输入字符串,并输出其长度. package cskaoyan; public class cskaoyan ...
- Java例题_38 自定义函数求字符串长度
1 /*38 [程序 38 求字符串长度] 2 题目:写一个函数,求一个字符串的长度,在 main 函数中输入字符串,并输出其长度. 3 */ 4 5 /*分析 6 * 1.从键盘得到一个字符串 7 ...
随机推荐
- 服务器上的XML
若想让浏览器能访问Web项目,需要配置服务器里的XML文件,XML文件是类似于HtML文件的纯文本文件,可以通过Web服务器轻松的存储和生成. XML可以通过ASP,PHP,数据库生成XML
- Spring 学习04
一.上节内容回顾 1 基于aspectj的注解aop操作 2 spring的jdbcTemplate操作 (1)实现crud操作 - 添加.修改.删除update方法 - 查询 -- 查询某个值 qu ...
- 词典的实现(4)-使用Hash方式来实现词典
1,实现思路 public class HashedDictionary<K, V> implements DictionaryInterface<K, V>, Seriali ...
- java程序在windows后台执行的办法
1.新建run.txt文件 2.在文件中输入一下内容: @echo off start javaw -jar xx.jar exit 3.保存,修改文件名为run.bat4.双击即可 5.删除wind ...
- C# 简单线程实例
1.简单线程实例 以及委托(同步委托.异步委托) using System; using System.Collections.Generic; using System.Linq; using Sy ...
- Nginx 内核优化
内核参数的优化示例: /etc/sysctl.conf net.ipv4.tcp_max_tw_buckets = // timewait的数量,默认是180000. net.ipv4.ip_loca ...
- pandas数据表
安装 pip3 install pandas s=pd.Series([1,3,6,90,44,1]) #创建序列[用列表创建].数据源的维度必须是一维 #data 指定数据源 print(s ...
- php 全局变量问题
当在函数里通过require_once包含另外php文件. 而另外php文件包含了另外php文件,而该php文件的函数需要另外的php文件. 例子: installment_maintenance_s ...
- python - 类的内置 attr 方法
类的内置 attr 方法 #类的内置 attr 方法: # __getattr__ # __setattr__ # __delattr__ # __getattr__ #到调用一个类不存在数参数时,将 ...
- css scrollbar样式设置
参考链接:https://segmentfault.com/a/1190000012800450