【KMP思想求循环节】hdu 1358 hust 1010 poj 2406
2017-07-22 21:15
489 查看
字符串的循环节为 字符串长度减去字符串最长公共前后缀的长度。
hdu 1358
其他两题没什么变化。
hdu 1358
#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<stack> #include<queue> #include<deque> #include<map> #include<algorithm> using namespace std; typedef long long LL; //#pragma comment(linker, "/STACK:102400000,102400000") /* HDU - 3746 给出一个字符串,问还需要在后面添加多少个字符才能使它变成由一个前缀循环多次而成 无非求一个next */ const double PI = acos(-1.0); const double eps = 1e-6; const int INF=0x3f3f3f3f; const int mod = 1e9; const int N = 100010; char s ; int nex ; void getnext(char *p) { int len = strlen(p); int k = -1; nex[0] = k; for(int i = 1; i < len; i++) { while(k!=-1 && p[i]!=p[k+1]) k = nex[k]; if(p[i]==p[k+1]) k++; nex[i] = k; } } int main() { int T; scanf("%d",&T); while(T--) { scanf("%s",s); getnext(s); int len = strlen(s); int x = nex[len-1]+1; x = len-x; if(x == len) { printf("%d\n",x); continue; } if(len%x == 0) puts("0"); else printf("%d\n",x-len%x); } return 0; }
其他两题没什么变化。
相关文章推荐
- KMP入门题 Hdu 1711 2594 3746 HUST 1010 Poj 3461 2752 2406 1961 FZU 1901
- hdu 1358 & hdu 3746 & poj 2406 & uva 12012 循环节与kmp
- 【KMP(循环节)】poj 2406 Power Strings(外:hdu 1358 Period)
- HDU 1358 && HDU 3746 && POJ 2406 最小循环节 (KMP)
- Period (poj 1961&&hdu 1358)KMP
- poj2406 Power Strings(kmp求循环节)
- POJ 2406 kmp求循环节个数
- Period (poj 1961&&hdu 1358)KMP
- POJ 1961 && HDU 1358 Period(kmp)
- POJ 2406 Power Strings (kmp求循环节)
- hust 1010 - The Minimum Length KMP求循环节
- (KMP 1.5)hdu 1358 Period(使用next数组来求最小循环节——求到第i个字符的循环节数)
- POJ 2406 依然是KMP求循环节
- hdu1358 kmp的next数组求循环节
- poj 2406 KMP求循环节
- 【POJ 2406】 Power Strings(KMP求循环节)
- hdu 1358 period 求循环节 KMP
- HDU 1358(KMP思想求周期)
- HDU-1358 Period(KMP求循环节)
- HDU 1358 Period (kmp求循环节)