soj 2307 String Matching(KMP)
来源:互联网 发布:阿里云短信api 编辑:程序博客网 时间:2024/04/29 04:31
@(K ACMer)
KMP裸题一道…
#include <iostream>#include <cstdio>#include <cstring>#include <set>#include <map>#include <stack>#include <vector>#include <string>#include <queue>#include <cstdlib>#include <cmath>#include <algorithm>using namespace std;typedef pair<int, int> pii;typedef long long ll;typedef vector<int> vi;#define xx first#define yy secondconst int mod = int(1e9) + 7, INF = 0x3fffffff, maxn = 1e6 + 40;char s[maxn], p[maxn];int nexts[maxn], plen;void getnexts(char* p){ int k = -1, j = 0; nexts[0] = -1; while (j < plen - 1) { if (k == -1 || p[j] == p[k]) { k++, j++; if (p[j] != p[k]) nexts[j] = k; else nexts[j] = nexts[k]; } else { k = nexts[k]; } }}int kmp(char* s, char* p){ int ans = 0, i = 0, j = 0, slen = strlen(s); while (i < slen) { if (j == -1 || p[j] == s[i]) i++, j++; else j = nexts[j]; if (j == plen - 1) ans++; } return ans;}int main(void) { int T; cin >> T; while (T--) { scanf("%s%s", s, p); plen = strlen(p); p[plen++] = '$'; p[plen] = '\0'; getnexts(p); printf("%d\n", kmp(s, p)); } return 0;}
0 0
- soj 2307 String Matching(KMP)
- KMP Matching: String Matching
- KMP string matching algorithm
- 5 Binary String Matching【kmp】
- nyoj5 Binary String Matching(KMP)
- Binary String Matching---KMP算法,再次复习
- NYoj-Binary String Matching-KMP算法
- String Matching -- Brute Force + Rabin-Karp + KMP
- nyoj 5 Binary String Matching 【裸kmp】
- NYOJ 5 Binary String Matching (KMP)
- nyoj -5 Binary String Matching 【kmp】
- NYOJ 5 Binary String Matching (KMP)
- NYOJ-----5---Binary String Matching(KMP)
- nyoj 5 Binary String Matching (KMP)
- SOJ 1035 DNA matching
- KMP matching
- HDU--3407[String-Matching Automata] AC自动机或kmp
- NYOJ 5 Binary String Matching (kmp 字符串匹配)
- Android经典例子收藏笔记1
- 114 linux 完全卸载lnmp
- Android ListView setSelection()方法的介绍
- 【Java集合源码剖析】ArrayList源码剖析
- MYSQL简介和存储引擎的选择
- soj 2307 String Matching(KMP)
- 安装inotify-tools,用inotifywait命令监听文件或目录的访问信息
- Kettle执行transform时报错:错误打开转换
- 数组与链表的优缺点分析
- Sublime Text3插件安装
- 【uml】-总结
- C语言 scanf函数的机理和常规的格式字符串
- 有关android的property机制 (property_set() & property_get())
- AutoLayout总结