[KMP求最小周期]POJ 2406 Power Strings
来源:互联网 发布:交男朋友的软件 编辑:程序博客网 时间:2024/04/29 20:03
传送门:http://poj.org/problem?id=2406
题意:给定一个字符串,让你求出他最多由几个相同的连续子串连接而成。
思路:求出这个字符串的最小周期,然后用总长度/最小周期长度即是解。
关于如何求最小周期:这里YY了一个方法,就是把该字符串增长一倍,然后拿原来的字符串做模式,增长后的字符串做主串,用KMP求模式在主串第1个位置(下标为0,包含第一个位置)之后的第一个匹配位置,就是最小周期长度。(如何证明当然不会啦。。。。。仅仅是YY出来的,没想到AC了。)
代码:
#include<iostream>#include<cstdio>#include<cstring>using namespace std;const int MAXN = 11111111;char T[MAXN<<1],P[MAXN];int Next[MAXN];void MakeNext(int M){ int i=0,j=-1; Next[0] = -1; while(i<M){ if(j==-1||P[i]==P[j]){ i++,j++; if(P[i]!=P[j])Next[i]=j; else Next[i] = Next[j]; } else j = Next[j]; }}int KMP(int pos,int N,int M){ int i = pos,j = 0; while(i<N&&j<M){ if(j==-1||T[i]==P[j])i++,j++; else j = Next[j]; } if(j==M) return i-M; else return -1;}int main(){ while(scanf("%s",P),P[0]!='.'){ int N,M; M = strlen(P); N = M<<1; strcpy(T,P); strcpy(T+M,P); MakeNext(M); int pos = KMP(1,N,M); int m = pos; printf("%d\n",M/m); } return 0;}
- [KMP求最小周期]POJ 2406 Power Strings
- POJ--2406Power Strings+KMP求字符串最小周期
- POJ 2406 Power Strings KMP求周期
- POJ 2406 Power Strings (kmp求周期)
- POJ 2406 Power Strings KMP 求最小循环节
- POJ 2406 Power Strings(KMP求最小循环节)
- KMP + 求最小循环节 --- POJ 2406 Power Strings
- 【poj 2406】 Power Strings 【KMP 求最小循环节】
- poj2406--Power Strings+KMP求周期
- POJ 2406 Power Strings(KMP找周期)
- poj 2406 Power Strings 【KMP求最小循环节】【后缀数组求连续重复子串】
- POJ 2406(KMP求最小周期)
- poj 2406 Power Strings ( fail 数组求周期)
- POJ 2406 Power Strings【KMP求最小循环节/后缀数组】
- POJ 2406 Power Strings 求最小循环节数(KMP)
- poj 2406 Power Strings (KMP+最小循环节)
- POJ 2406 Power Strings(KMP+最小循环节)
- POJ 2406 Power Strings 求连续重复字串(kmp)
- 打造ubuntu超赞图形界面(测试环境:ubuntu12.04)
- 【翻译】Ext JS 4之HelloWorld
- android自定义实现圆形播放进度条
- IOS 如何选择delegate、notification、KVO?
- 解决php中文乱码问题
- [KMP求最小周期]POJ 2406 Power Strings
- junit4中annotation的几种应用
- HttpModule的认识
- vc6.0中出现Error spawning cl.exe 错误解决办法
- python oss alioss 使用阿里开放云存储服务
- 那些值得我们用心体会的惊艳歌词
- 一些好看的网页(休闲一下)
- Android游戏开发基础part6--动画
- Linux c 开发 - Socket