poj1961
2017-02-09 09:12
127 查看
题目大意:
对于有N个字符的字符串S的前缀,我们想知道前缀是否是一个周期串。输入一个字符串,输出截止到i个字符为止,前缀重复K次
解题思路:
KMP算法代码如下:
#include<stdio.h> #define N 1000010 char s ; int nextval ; int len; void getnext(const char *s) { int i=0,j=-1; nextval[0]=-1; while(i!=len) { if(j==-1||s[i]==s[j]) nextval[++i]=++j; else j=nextval[j]; } } int main() { int T=1; int length,add; int i; while(scanf("%d",&len)&&len) { scanf("%s",s); getnext(s); printf("Test case #%d\n",T++); for(i=1;i<=len;++i) { length=i-nextval[i]; if(i!=length&&i%length==0) printf("%d %d\n",i,i/length); } printf("\n"); } return 0; }
相关文章推荐
- 初学ACM - 组合数学基础题目PKU 1833
- POJ ACM 1001
- POJ ACM 1002
- 1611:The Suspects
- POJ1089 区间合并
- POJ 2159 Ancient Cipher
- POJ 2635 The Embarrassed Cryptographe
- POJ 3292 Semi-prime H-numbers
- POJ 2773 HAPPY 2006
- POJ 3090 Visible Lattice Points
- POJ-2409-Let it Bead&&NYOJ-280-LK的项链
- POJ-1695-Magazine Delivery-dp
- POJ1523 SPF dfs
- POJ-1001 求高精度幂-大数乘法系列
- POJ-1003 Hangover
- POJ-1004 Financial Management
- poj 1753 Flip Game
- poj 1328 Radar Installation
- poj 2528 Y2K Accounting Bug
- poj 3295 Tautology