HDU4763 Theme Section KMP-next应用
来源:互联网 发布:java循环读取txt文件 编辑:程序博客网 时间:2024/05/22 10:48
题意:给你一个字符串,若将它分割成 AXAXA 的形式,A最长是多少?
思路:对于这道题我看了很多题解,但感觉讲的还是很模糊,这道题从next数组入手即可,很短的代码便可搞定,主要要了解一点如果给你一个长度为n的字符串,那么前后缀的最大匹配值自然是next[n],那么次长前后缀匹配值是什么呢? 其实就是next[ next[n] ] ,我们可以通过找出前后缀的匹配值然后知道中间子串的上下界,然后再这个区间看能否找到子串即可,若不能,换成次长前后缀匹配继续求解,以此类推。
代码如下:
#include <bits/stdc++.h>using namespace std;const int maxn=1e6+5;char p[maxn];int NEXT[maxn];void get_NEXT(){ NEXT[0] = -1; int k = -1; int j = 0; int n = strlen(p); while(j < n){ if(k == -1 || p[k] == p[j]){ k++; j++; NEXT[j] = k; } else k = NEXT[k]; }}int main(){ ios::sync_with_stdio(false); int T; cin>>T; while(T--){ cin>>p; get_NEXT(); int n = strlen(p); int F = NEXT[n]; int ans = 0; for(int i=F;i>0;i=NEXT[i]){ for(int j=i;j<=n-i;j++){ if(NEXT[j] == i){ ans=i; goto stop; } } } stop: cout<<ans<<endl; } return 0;}
0 0
- HDU4763 Theme Section KMP-next应用
- Theme Section(KMP应用 HDU4763)
- hdu4763 Theme Section KMP
- hdu4763 Theme Section (KMP)
- HDU4763 Theme Section 【KMP】
- HDU4763 Theme Section KMP
- HDU4763 - Theme Section(KMP)
- hdu4763 Theme Section(kmp)
- HDU4763 Theme Section(KMP+扩展KMP)
- HDU4763 Theme Section(next数组__kmp+扩展)
- hdu 4763 Theme Section (kmp next应用)
- HDU4763-Theme Section(KMP+二分)
- hdu4763 Theme Section(KMP水题)
- hdu4763---Theme Section
- HDU4763 Theme Section
- hdu 4763 Theme Section (kmp, Next数组的应用)
- HDU 4763 Theme Section【KMP的next数组练习】
- hdu4763(KMP的应用)
- python 输出冒号;引号嵌套问题
- 索引数组与删除数组
- String源代码
- 中缀表达式求值
- 制作Ubuntu server 12.04及新版系统安装U盘的正确方法
- HDU4763 Theme Section KMP-next应用
- Apache Cordova开发环境搭建(一)-Visual Studio
- 初学Redis(2)——用Redis作为Mysql数据库的缓存
- 自定义dialog
- 大数乘法
- STC89C52单片机串口调试(http://blog.sina.com.cn/s/blog_7cae472801015idp.html)
- [C#]第30位数字是多少
- 16年腾讯笔试模拟题
- 互动投影系统