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 ...
随机推荐
- hdu 5432 Pyramid Split 二分
Pyramid Split Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://bestcoder.hdu.edu.cn/contests/conte ...
- [MODx] 8. Snippet get data, chunk display
Simple Example: Lets process this chunk and output its value. We have this Chunk, called "Welco ...
- 学习笔记之Python
http://baike.baidu.com/view/21087.htm?fr=aladdin#reference-[12]-21087-wrap Python 基础教程(http://www.w3 ...
- Mybatis案例
MyBatis MyBatis 是支持普通 SQL 查询,存储过程和高级映射的优秀持久层框架. MyBatis 消除了几乎所有的 JDBC 代码和参数的手工设置以及对结果集的检索. MyBatis 可 ...
- Asp.Net 之 当前上下文中不存在名称" Server "
在开发中经常用到应用程序的物理路径,在获取应用程序中文件的物理路径时最常用: string path = Server.MapPath("Document/example.doc" ...
- js中数组内置方法
var arr = ['A','B','C','D']; length 计算数组的长度 arr.length//4 indexOf() 搜索一个指定的元素的位置 arr.indexOf('C');// ...
- CSS——选择器
css选择器 css选择器可分为:标签(元素)选择器,ID选择器,类选择器,属性选择器,后代选择器,子代选择器,相邻兄弟选择器和兄弟选择器.... 标签选择器: //E{attr:value;attr ...
- WebKit笔记
加载网页时执行javascript代码 let mWebView = WKWebView.init(frame: self.view.bounds) self.view.addSubview(mWeb ...
- UIPickerView
1.UIPickView什么时候用? 通常在注册模块,当用户需要选择一些东西的时候,比如说城市,往往弹出一个PickerView给他们选择. UIPickView常见用法,演示实例程序 1> 独 ...
- 使用jstack分析java程序cpu占用率过高
在项目中经常会碰到CPU占用率过高的问题,那么碰到这类问题应当如何处理呢?下面提供一种处理思路: 首先top -H -p <pid>以线程的模式查看java应用的运行情况,找到占用cpu或 ...