POJ 3461 Oulipo(KMP)
2012-07-20 10:12
357 查看
题目链接
第一个KMP,纪念纪念。。。虽然比着模版打的,理解模版,真的好短。
第一个KMP,纪念纪念。。。虽然比着模版打的,理解模版,真的好短。
#include <stdio.h> #include <string.h> char str1[10001],str2[1000001]; int next[10001]; int main() { int t,len1,len2,num,i,j; scanf("%d",&t); while(t--) { scanf("%s%s",str1,str2); len1 = strlen(str1); len2 = strlen(str2); next[0] = -1; num = 0; j = -1; for(i = 1;i <= len1-1;i ++)//预处理 { while(j >= 0&&str1[j+1] != str1[i])//如果已经匹配了一部分,但是j+1的与i不匹配,就退回到next值 j = next[j];//直到找到str[j+1] = str1[i]为止,很明显如果找不到j会变为-1,停止循环 if(str1[j+1] == str1[i])j++; next[i] = j; } j = -1; for(i = 0;i <= len2-1;i ++)//和预处理神似。。 { while(j >= 0&&str1[j+1] != str2[i]) j = next[j]; if(str1[j+1] == str2[i]) j ++; if(j == len1-1)//如果找到了,继续查找。 { num ++; j = next[j]; } } printf("%d\n",num); } return 0; }
相关文章推荐
- POJ---3461-Oulipo(KMP)
- POJ 3461 Oulipo KMP练习
- POj 3461 Oulipo ,HDU 1686 Oulipo(KMP)
- poj 3461_Oulipo_KMP
- poj 3461 Oulipo(KMP)
- poj-3461 Oulipo 【kmp】
- Oulipo - POJ 3461 KMP
- 【KMP】 POJ 3461 Oulipo
- poj 3461 Oulipo(KMP模板题)
- POJ 3461 Oulipo(简单KMP)
- poj 3461 Oulipo (KMP)
- poj 3461 Oulipo(KMP)
- poj 3461 Oulipo KMP
- Poj 3461 Oulipo(Kmp)
- POJ 3461 Oulipo(KMP模板题)
- 【kmp】POJ-3461 Oulipo
- poj 3461 Oulipo(KMP)
- poj3461——Oulipo(KMP)
- poj 3461:Oulipo(KMP模版)
- POJ 3461 Oulipo (KMP)