kmp算法 in POJ【POJ3461,POJ2752,POJ2406,POJ1961】
2017-08-29 11:19
369 查看
POJ_3461 Oulipo
简单的kmp使用,找出第一个字符串在第二个字符串中出现的次数
#include<iostream> #include<stdlib.h> #include<cstring> using namespace std; int main(void) { char mother[10001]; char son[1001]; cin >> mother >> son; int next[1001] = {-1}; int i=0; int j=-1; int len_son = strlen(son); int len_mother = strlen(mother); while(i < len_son) { if(j==-1 || son[i]==son[j]) { i++; j++; next[i] = j; } else j = next[j]; } i=j=0; int ans=0; while(i < len_mother) { if(j==-1 || mother[i] == son[j]) { i++; j++; } else j = next[j]; if(j==len_son) ans++; } cout << ans << endl; system("pause"); return 0; }
POJ_2751 Seek the Name, Seek the Fame
依次输出使字符串前缀后缀相同的逻辑位序
#include<iostream> #include<stdlib.h> #include<cstring> using namespace std; const int Max = 4e5+5;//即400005 char str[Max]; int next[Max],ans[Max]; void get_next(char *str) { int i=0,j=-1; next[0] = -1; int len=strlen(str); while(i<len) { if(j==-1 || str[i]==str[j]) next[++i] = ++j; else j = next[j]; } } int main(void) { while(cin >> str) { get_next(str); int len = strlen(str); int k = next[len]; int j=0; while(k>0) { j++; ans[j] = k; k = next[k]; } for(int i=j;i>0;i--) { cout << ans[i] <<" "; } cout << len <<endl; } system("pause"); return 0; }
POJ_2406 Power Strings
求字符串最多是由多少个子串循环组成 ,kmp算法中next数组的使用
/***************** POJ 2406 求字符串最多是由多少个子串循环组成 kmp算法中next数组的使用 ******************/ #include<iostream> #include<cstring> using namespace std; const int Max_Size = 1000005; int next[Max_Size]; void Get_next(char *s) { int l = strlen(s); int i=0; int j=-1; next[0] = -1; while(i<l) { if(j==-1 || s[i]==s[j]) { next[++i] = ++j; } else j = next[j]; } } int main(void) { char str[Max_Size]; while(gets(str)) { if(strcmp(str,".")==0) break; Get_next(str); int ans = 1; int l = strlen(str); if(l%(l-next[l]) == 0) ans = l/(l-next[l]); /*else ans = 1;*/ printf("%d\n",ans); } system("pause"); return 0; }
POJ-1961 Period
类似于2406
#include<iostream> #include<stdlib.h> #include<cstring> using namespace std; const int Max_n = 1000005; int next[Max_n]; void get_next(char *str) { int i=0,j=-1; int len = strlen(str); next[0] = -1; while(i<len) { if(j==-1 || str[i]==str[j]) next[++i] = ++j; else j = next[j]; } } int main(void) { int n; char str[Max_n]; cin >> n; int count = 1; while(n!=0) { cin >> str; cout << "Test case #" << count << endl; count ++; int i=0,j=-1; int len = strlen(str); next[0] = -1; while(i<n) { if(j==-1 || str[i]==str[j]) next[++i] = ++j; else j = next[j]; } for(int k=2;k<=n;k++) { if(k%(k-next[k])==0 && k/(k-next[k])!=1) { cout << k << " " << k/(k-next[k]) << endl; } } cout << endl; cin >> n; } system("pause"); return 0; }
相关文章推荐
- HihoCoder第三周与POJ2406:KMP算法总结
- poj1961_KMP算法中next数组的应用
- POJ3461 Oulipo KMP算法
- KMP算法、next数组与前缀中的周期(相关题目:Power strings, poj2406)
- KMP算法的经典例题(poj 3461、poj 2752、poj 2406、poj1961)
- 数据结构之KMP算法---poj2406---Power Strings
- KMP算法的next、next value数组代码实现及POJ3461
- POJ3461 HDU1686(KMP算法)
- 【POJ3461】KMP算法理解 for 初学者
- Poj1961 Poj2406 最短循环节
- 再谈kmp算法 , 由poj3461
- poj3461 KMP算法(字符串匹配)
- kmp算法巩固 以poj3461为试验
- POJ3461 Oulipo KMP算法应用
- (POJ1961)Period <KMP算法求最小循环节>
- 【kmp算法】poj2406 Power Strings
- (POJ2406)Power Strings <KMP算法求最小循环节>
- POJ3461 Oulipo ——KMP算法——Pku3461
- KMP算法(POJ2406)
- poj1961与poj2406同都是寻找循环节