您的位置:首页 > 其它

HDU 1686 Oulipo kmp模板

2016-07-23 16:24 288 查看
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1686

题意:给一个模式串,一个目标串,问目标串中有几个模式串

思路:kmp模板题,留个模板。。。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

const int N = 10010;
char s1
, s2[N*100];
int nt
;

void getnt()
{
int i = 0, j = -1;
nt[0] = -1;
while(s1[i])
{
if(j == -1 || s1[i] == s1[j])
{
i++, j++;
if(s1[i] != s1[j]) nt[i] = j;
else nt[i] = nt[j];
}
else j = nt[j];
}
}
int kmp()
{
int i = 0, j = 0, cnt = 0;
getnt();
while(s2[i])
{
if(j == -1 || s2[i] == s1[j])
i++, j++;
else j = nt[j];
if(j != -1 && ! s1[j])
cnt++, j = nt[j];
}
return cnt;
}
int main()
{
int t;
scanf("%d", &t);
while(t--)
{
scanf(" %s %s", s1, s2);
printf("%d\n", kmp());
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: