ACM_KMP模板
来源:互联网 发布:c编程思想pdf下载 编辑:程序博客网 时间:2024/05/24 06:31
KMP模板
其实 每道题的区别在于 你的模式串在文本庄中匹配到了 接下来的操作 都只在kmp函数末尾的操作发生变化
1.如果 是类似统计布条 即每次找到的字符串没有重合部分 求出sum
if (j==m) { sum++; j=0; } return sum;
2.如果统计的字符串可以重合 、
if (j==m) { sum++; j=next[j]; } } return sum;
3.统计有无
if (j==m) { return 1; } } return 0;
4确定位置
if (j==m) return i;
下面代码是统计多少个不重合的sum值的模板
#include <string.h>#include <stdio.h>//#include <bits/stdc++.h>#define Max 1000005char a[Max];char b[Max];int next[Max];int m, n;int sum;void getnext(){ int j=0, k=-1; next[0] = -1; while (j<m) { if (k==-1||b[j]==b[k]) { j++; k++; next[j] = k; } else k = next[k]; }}int kmp(){ int i=0, j=0; getnext(); while (i<n) { if (j==-1||a[i]==b[j]) { i++; j++; } else j = next[j]; if (j==m) { sum++; j=0; } } return sum;}int main(){ while (scanf("%s",a)) { if (a[0]=='#') break; scanf("%s",b); sum=0; //int n ,m; m = strlen(b); n = strlen(a); int t = kmp(); printf ("%d\n",sum); } return 0;}
阅读全文
1 0
- ACM_KMP模板
- 模板
- 模板?
- 模板
- 模板
- 模板
- 模板
- 模板
- 模板
- 模板
- 模板
- 模板
- 模板
- 模板
- 模板
- 模板
- 模板
- 模板
- Django初探
- (node:31568) DeprecationWarning: `open()` is deprecated in mongoose >= 4.11.0, use `openUri()` inste
- (18)用std::wifstream读取Unicode文本
- Vim中通过tags跳转,手动输入选择哪条,"找到 tag: 1 / 2 或更多"
- 《Java多线程编程核心技术》-笔记
- ACM_KMP模板
- poi导出百万数据到excel
- (function($){})(jQuery)、jQuery.extend({})、jQuery.fn.extend()的含义与之间的联系运用
- 2018第四届俄罗斯(莫斯科)国际防务与航空航天展
- Python3爬虫学习笔记(5.Selenium简单操作介绍)
- 嵌入式数据库——sqlite
- 实现Binder接口的异步通信
- Windows下的React-Native开发调试环境安装
- test