hdu 4763 Theme Section(kmp)
2015-09-06 21:46
411 查看
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=4763
解题思路:
题目大意:
给你一个字符串,问你是否可以表示为“EAEBE”的形式,E、A、B表示一个字符串,可以为空,输出最长的E串的长度。
解题思路:
KMP,首先先求出最长后缀,然后用KMP算法判断去掉前后部分后是否存在该串,不存在则缩小长度,直到为0为止。。。
AC代码:
http://acm.hdu.edu.cn/showproblem.php?pid=4763
解题思路:
题目大意:
给你一个字符串,问你是否可以表示为“EAEBE”的形式,E、A、B表示一个字符串,可以为空,输出最长的E串的长度。
解题思路:
KMP,首先先求出最长后缀,然后用KMP算法判断去掉前后部分后是否存在该串,不存在则缩小长度,直到为0为止。。。
AC代码:
#include <iostream> #include <cstdio> #include <cstring> using namespace std; const int maxn = 1000005; int len,Next[maxn]; char str[maxn]; void getNext(){ int j = 0,k = -1; Next[0] = -1; while(j < len){ if(k==-1 || str[j]==str[k]){ j++; k++; Next[j] = k; } else k = Next[k]; } } int main(){ int T; scanf("%d",&T); while(T--){ scanf("%s",str); len = strlen(str); getNext(); int k = Next[len],ans = 0; while(k && !ans){ int j = 0; for(int i = k; i < len-k; i++){ if(j == k) break; while(!(j==-1 || str[i]==str[j])) j = Next[j]; j++; } if(j == k) ans = k; k = Next[k]; } printf("%d\n",ans); } return 0; }
相关文章推荐
- LeetCode Reverse Integer
- 用NSOperation写下载队列
- Fresco 源码分析(二) Fresco客户端与服务端交互(2) Fresco.initializeDrawee()分析 续
- oc- 关键字
- 密码123456,这意味着什么?
- 关于测试程序运行时间
- 杭电OJ-2023_求平均成绩
- UIMenuController的使用,对UILabel拷贝以及定制菜单
- 扬州一大爷数落宝马横停占道 车主驾车将其撞飞
- 进出口贸易被指遭港口部门挡路收费2000亿
- 写一个标题试一试。新的开始。
- SGU106 The equation
- Fresco 源码分析(二) Fresco客户端与服务端交互(1) 解决遗留的Q1问题
- 如何修改MFC对话框的窗口标题
- testlink和mantis角色整理
- 计算机网络基础知识总结
- TCP连接的11种状态
- Regionals 2014 Asia Xian(几道简单题)
- magento注册
- css制作三角形,下拉框三角形