POJ - 3461 Oulipo——kmp

网友投稿 506 2022-11-28

POJ - 3461 Oulipo——kmp

POJ - 3461 Oulipo——kmp

求模式串在字符串中出现的次数。

统计次数时,要注意当完成一组匹配之后令j = next【j】,以便开启另一组匹配

#include #include #include using namespace std;const int maxn = 1000000 + 10;int next[maxn], leno, lenp;char o[maxn], p[maxn];void NEXT() { int i = 0, j = -1; next[0] = -1; while (i < lenp) { if (j == -1 || p[i] == p[j]) { i++, j++; if (p[i] != p[j]) next[i] = j; else next[i] = next[j]; } else j = next[j]; }}void KMP() { int i = 0, j = 0, cnt = 0; while (i < leno && j < lenp) { if (j == -1 || o[i] == p[j]) { i++, j++; } else j = next[j]; if (j == lenp) { j = next[j], cnt++; } } printf("%d\n", cnt);}int main() { int T; scanf("%d", &T); while (T--) { scanf("%s%s", p, o); leno = strlen(o), lenp = strlen(p); NEXT(); KMP(); } return 0;}

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:springboot整合shiro的过程详解
下一篇:SpringMVC中如何获取@PathVariable的值
相关文章

 发表评论

暂时没有评论,来抢沙发吧~