题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4763

题目大意:给定一串字符,从中找出符合“EAEBE”格式的E的最大字符数。AB可以是任意数量的任意字符(a-z)。

Sample Input
5
xy
abc
aaa
aaaaba
aaxoaaaaa
 
Sample Output
0
0
1
1
2

分析:首先枚举判断开始和结尾是否满足作为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算法)的更多相关文章

  1. hdu 1711 KMP算法模板题

    题意:给你两个串,问你第二个串是从第一个串的什么位置開始全然匹配的? kmp裸题,复杂度O(n+m). 当一个字符串以0为起始下标时.next[i]能够描写叙述为"不为自身的最大首尾反复子串 ...

  2. hdu 1686 KMP算法

    题意: 求子串w在T中出现的次数. kmp算法详解:http://www.cnblogs.com/XDJjy/p/3871045.html #include <iostream> #inc ...

  3. hdu 4300 kmp算法扩展

    Clairewd’s message Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  4. hdu 4763 kmp ***

    找AEAEA形式的字符串最长的A长度,E可以为空 只可意会,不可言传,懂kmp即可 #include <stdio.h> #include <string.h> #includ ...

  5. hdu 3613 KMP算法扩展

    Best Reward Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total ...

  6. HDU 2594 kmp算法变形

    Simpsons’ Hidden Talents Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java ...

  7. HDU 4763 Theme Section(KMP+枚举公共前后缀)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4763 题目大意: 给你一个字符串s,存在一个子串E同时出现在前缀.中间.后缀,即EAEBE这种模式,A ...

  8. HDU 1711 Number Sequence (字符串匹配,KMP算法)

    HDU 1711 Number Sequence (字符串匹配,KMP算法) Description Given two sequences of numbers : a1, a2, ...... , ...

  9. HDU 3613 Best Reward(拓展KMP算法求解)

    题目链接: https://cn.vjudge.net/problem/HDU-3613 After an uphill battle, General Li won a great victory. ...

  10. hdu 1358:Period(KMP算法,next[]数组的使用)

    Period Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

随机推荐

  1. (JAVA版)冒泡排序

    核心代码: public void bubbleSort(){ ;i<length-;i++){ ;j<length-i-;j++){ ]) swap(j,j+); } } } publi ...

  2. cocos2d-x 精灵遮罩

    转自:http://bbs.9ria.com/thread-220210-1-4.html 首先得理解一些东西. 1.理解颜色混合.精灵有个成员函数:setBlendFunc(),这个函数以一个ccB ...

  3. hdu 5455 Fang Fang 坑题

    Fang Fang Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5455 ...

  4. 【异构计算】OpenCL矩阵转置

    介绍 矩阵转置,主要的技巧还是利用好local memory ,防止local memory,以及glabol memory的读取尽量是合并读写. 完整代码一: main.cpp代码 #include ...

  5. [Bootstrap] 1. container & container-fluid

    Container: 居中 <!DOCTYPE html> <html> <head> <title>Blasting Off With Bootstr ...

  6. json处理复杂对象jsonConfig

     我们通常的Json字符串和java当对象互转.经常有选择性地过滤掉一些属性值,和json-lib包JsonConfig为我们提供了这样一个 特征,有几种方法实施细则. (1)建立JsonConf ...

  7. [JavaScript]转--如何让JS代码高大上

    原文出处:http://www.cnblogs.com/wenber/p/3630373.html 1,创造简短的写法 你可以这么写: 1 var slice = Array.prototype.sl ...

  8. wcf自身作为宿主的一个小案例

    第一步:创建整个解决方案 service.interface:用于定义服务的契约(所有的类的接口)引用了wcf的核心程序集system.ServiceModel.dll service:用于定义服务类 ...

  9. 【转】C语言 字符数组与字符串

    原文:http://blog.csdn.net/metasearch/article/details/2856097 在C语言编程中,当我们声明一个字符串数组的时候,常常需要把它初始化为空串.总结起来 ...

  10. MySQL(15):Select-distinct(返回非重复的记录)

    1. 查询所有记录 和 查询 非重复记录 语法: SELECT    [ALL | DISTINCT ] All:返回所有记录 Distinct:返回非重复记录 针对获得的记录内的字段生效.   2. ...