POJ 2406--Power Strings(kmp算法)
来源:互联网 发布:软件项目计划书 编辑:程序博客网 时间:2024/04/30 23:46
题意:给定一规模为1e6的字符串S,求出S的最短重复子串a。
题解:利用KMP算法求字符串最大周期。
KMP算法:
- 目的就是求一字符串的前缀数组pi[q] = max{k: k < q且P[k]是P[q]的后缀},其中数组索引从1开始,P[k]代表从S[1]到S[k]组成的S的子串。这里有一个很重要的概念就是P[k]显然也是S的前缀。
int calc_prefix(char* s,int* pi){ int k,q; pi[1] = 0; k = 0; for(q = 2;q <= N;q++) { while(k != 0&&s[k+1] != s[q]) { k = pi[k]; } if(s[k+1] == s[q]) { k++; } pi[q] = k; } return 0;}
代码中最NB的就是那个while的迭代了。
其中的迭代我们用另一个集合来解释:pi*[q] = {pi[q],pi[pi[q]],...,0}。
- 注意到P[pi[q]]是P[q]的前缀以及后缀,同理P[pi[pi[q]]]是P[pi[q]]的前缀以及后缀,易知P[pi[pi[q]]]也是P[q]的前缀以及后缀。于是我们有结论:pi*[q] = {k:k < q且P[k]是P[q]的后缀}。
- 这样代码中的迭代也就容易理解了,因为进入while时k = pi[q-1],且经过迭代P[k]都一直是P[q-1]的后缀,所以当遇到一个S[k+1] = S[q]时,此时P[k+1]就是P[q]的后缀,显然它也是最大的k。
KMP求最大周期:
- 先给出结论:当len%(len-pi[len]) == 0时,len/(len-pi[len])就是S的最大周期,且P[len-pi[len]]就是最小重复子串,若条件中式子不能整除则S周期为1。
- 证明:
- 根据条件设len = k*(len-pi[len]),则我们将S分成k个长度均为len-pi[len]子串:s1s2...sk。
- 根据KMP算法的推导,易知P[pi[len]] = s1s2...s(k-1)既是S的前缀也是S的后缀。也即是我们将S向右偏移s1的长度新的S与原来的S重合部分相等,于是我们有s1 = s2 = s3 = ...=sk。即s1是S的一个重复子串。
- s1是最短的证明可以用反证法,若还有更短的重复子串,则易知pi[len]将会增大,得证。
#include<iostream>#include<cstdio>#include<cstring>using namespace std;#define maxN 1000005class solve{private: char* s; int len; int* pi;public: solve(char* a):s(a) { len = strlen(s+1); pi = new int[len+5]; calc_prefix(); if(len%(len-pi[len]) == 0) printf("%d\n",len/(len-pi[len])); else printf("1\n"); } ~solve() { delete[] pi; } int calc_prefix();};int solve::calc_prefix(){ pi[1] = 0; int k = 0; for(int q = 2;q <= len;q++) { while(k > 0&&s[k+1] != s[q]) { k = pi[k]; } if(s[k+1] == s[q]) { k++; } pi[q] = k; } return 0;}int main(){ char s[maxN]; while(gets(s+1)&&s[1] != '.') { solve poj_2406(s); } return 0;}
附上字符串索引从0开始的KMP匹配代码:
#include<iostream>#include<cstdio>#include<cstring>using namespace std;int calcPrefix(char* p, int* pi) {pi[1] = 0;//pi[k]代表p的长度为k的前缀子串中,不仅是其前缀也是其后缀的最大子串长度 int q = 0;for(int i = 1;i < strlen(p);i++) {while(q && p[q] != p[i]) {q = pi[q];}if(p[q] == p[i]) {q++;}pi[i+1] = q;}return 0;}int kmpMatch(char* pattern, char* text) {int* pi;int pLen = strlen(pattern);int tLen = strlen(text);pi = new int[pLen+1];calcPrefix(pattern, pi);int q = 0;int matchNum = 0;for(int i = 0;i < tLen;i++) {while(q && pattern[q] != text[i]) {q = pi[q];}if(pattern[q] == text[i]) {q++;}if(q == pLen) {cout<<"match!"<<endl;matchNum++;q = pi[q];}}return matchNum;}int main() {cout<<kmpMatch("af", "afdfdg")<<endl; return 0;}
0 0
- 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算法)
- 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
- POJ 2406 Power Strings KMP
- poj-2406-Power Strings-kmp
- POJ-2406 Power Strings KMP
- Power Strings - POJ 2406 KMP
- poj 2406 Power Strings kmp
- POJ 2406-Power Strings (KMP)
- 【KMP】 POJ 2406 Power Strings
- poj 2406 Power Strings KMP
- android 判断登录是否邮箱或手机号
- Problem A Forever 0.5(推理构造)
- Review of Codeforces 6B and 6C
- 第十二周上机项目三
- HDU 5095 Linearization of the kernel functions in SVM(模拟)
- POJ 2406--Power Strings(kmp算法)
- 实验四
- hdu 5104 Primes Problem(数学)
- Objective-C基础笔记(4)Category
- JAVA的反射机制与动态代理
- LeetCode - Text Justification 题解
- ASP.NET动态网站开发培训-24.论文管理系统(四、制作Login页面和后台论文管理主页)
- 2014.11.15
- 学习资料(一)