HDU 4763 (KMP算法)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4763
题目大意:给定一串字符,从中找出符合“EAEBE”格式的E的最大字符数。AB可以是任意数量的任意字符(a-z)。
分析:首先枚举判断开始和结尾是否满足作为E,再KMP计算中间是否存在E
代码如下:
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define N 1000001
int next[N];
char a[N]; void getnext(int m){
int i=,j=-;
next[]=-;
while(i<=m){
if(j==-||a[i]==a[j])
{
i++;
j++;
next[i]=j;
}
else j=next[j];
}
}
bool kmp(int st,int l){
int j=;
int i=st+;
int ed=l-st-;
while(i<ed)
{
if(j==-||a[i]==a[j]){
i++;
j++;
if(j==st+) return true;
}
else j=next[j];
}
return false;
}
bool judge(int ans,int l){
for(int i=;i<=ans;i++)
{
if(a[i]!=a[l-ans+i-]) return false;
}
return true;
}
int getsolve(){
int l=strlen(a);
int ans=;
getnext(int(l/)-);
if(l<) return ;
for(int i=l/-;i>=;i--){
if(judge(i,l)){
if(kmp(i,l))
{
ans=i+;
break;
}
}
}
return ans;
}
int main(){
int t;
scanf("%d",&t);
while(t--){
scanf("%s",a);
printf("%d\n",getsolve());
}
return ;
}
HDU 4763 (KMP算法)的更多相关文章
- hdu 1711 KMP算法模板题
题意:给你两个串,问你第二个串是从第一个串的什么位置開始全然匹配的? kmp裸题,复杂度O(n+m). 当一个字符串以0为起始下标时.next[i]能够描写叙述为"不为自身的最大首尾反复子串 ...
- hdu 1686 KMP算法
题意: 求子串w在T中出现的次数. kmp算法详解:http://www.cnblogs.com/XDJjy/p/3871045.html #include <iostream> #inc ...
- hdu 4300 kmp算法扩展
Clairewd’s message Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- hdu 4763 kmp ***
找AEAEA形式的字符串最长的A长度,E可以为空 只可意会,不可言传,懂kmp即可 #include <stdio.h> #include <string.h> #includ ...
- hdu 3613 KMP算法扩展
Best Reward Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total ...
- HDU 2594 kmp算法变形
Simpsons’ Hidden Talents Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java ...
- HDU 4763 Theme Section(KMP+枚举公共前后缀)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4763 题目大意: 给你一个字符串s,存在一个子串E同时出现在前缀.中间.后缀,即EAEBE这种模式,A ...
- HDU 1711 Number Sequence (字符串匹配,KMP算法)
HDU 1711 Number Sequence (字符串匹配,KMP算法) Description Given two sequences of numbers : a1, a2, ...... , ...
- HDU 3613 Best Reward(拓展KMP算法求解)
题目链接: https://cn.vjudge.net/problem/HDU-3613 After an uphill battle, General Li won a great victory. ...
- hdu 1358:Period(KMP算法,next[]数组的使用)
Period Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
随机推荐
- PowerDesigner 正向工程 和 逆向工程 说明
PowerDesigner 正向工程 和 逆向工程 说明 database数据库脚本oraclegenerationsql 目录(?)[+] 一. 正向工程与逆向工程说明 在前面几篇里介绍了几 ...
- 将java源码打成jar包
方法一:通过jar命令 jar命令的用法: 下面是jar命令的帮助说明: 用法:jar {ctxui}[vfm0Me] [jar-file] [manifest-file] [entry-point] ...
- Redis实战之征服 Redis + Jedis + Spring (二)
不得不说,用哈希操作来存对象,有点自讨苦吃! 不过,既然吃了苦,也做个记录,也许以后API升级后,能好用些呢?! 或许,是我的理解不对,没有真正的理解哈希表. 相关链接: Redis实战 Redis实 ...
- [置顶] ORACLE分析函数(1)
分析函数式ORACLE提供的用来进行数据统计的强有力工具,与我们常用的聚合函数具有一些相似性,但又完全不同.聚合函数,首先会将数据进行分组,然后对每一组数据进行运算,如求和sum,求平均AVG等,对于 ...
- [Practical Git] Filter commit history with git log arguments
In the last lesson, we learned how to format the git log output; in this lesson we will learn how to ...
- android学习日记16--GridView(网格视图)
一.GridView 1.简述 GridView按照行列来显示图片或文本的一种视图,排列其实有点类似TableLayout布局, 不过和TableLayout还是差别很大的,倒比较像二维的ListVi ...
- V9最新手机门户域名绑定教程。
如需要绑定域名为wap.domain.com,作下如操作: 一.把wap.domain.com域名绑定到你的这个网站主机上.二.在网站后台模块>手机门户域名里面填写“http://wap.dom ...
- How to Map Distinct Value Types Using Java Generics--reference
原文:http://www.codeaffine.com/2015/03/04/map-distinct-value-types-using-java-generics/ Occasionally t ...
- C# 工厂模式示例
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace 工厂模式 ...
- 手把手教你使用UICollectionView写公司的项目
在很多app中都有这样通用的页面,一直没有机会使用UICollectionView,只是简单的看过他的使用方法.今天公司美工出图,使用了他,并且遇到了好多的坑.记录一下过程,不确定使用的方法是不是最优 ...