您的位置:首页 > 其它

Oulipo - HDU 1686 (KMP模板题)

2015-08-14 15:04 267 查看
题目大意:题目叙述很多,其实只看输入输出也能明白什么意思,给两个串W,T, 判断T串中包含几个串W。

分析:还是基础的KMP应用.......................
直接上代码。
==================================================================================================================

#include<stdio.h>
#include<string.h>

const int MAXM = 1e4+7;
const int MAXN = 1e6+7;

void GetNext(char s[], int next[], int N)
{
int i=0, j=-1;
next[0] = -1;

while(i < N)
{
if(j==-1 || s[i]==s[j])
next[++i] = ++j;
else
j = next[j];
}
}
int KMP(char W[], char T[], int Nw, int Nt)
{
static int next_w[MAXM];
int i=0, j=0, ans=0;

GetNext(W, next_w, Nw);

while(i < Nt)
{
while(j==-1 || (T[i] == W[j] && i<Nt) )
i++, j++;

if(j == Nw)
ans++;
j = next_w[j];
}

return ans;
}

int main()
{
int ncase;

scanf("%d", &ncase);

while(ncase--)
{
static char W[MAXM], T[MAXN];
int Nw, Nt;

scanf("%s%s", W, T);

Nw = strlen(W);
Nt = strlen(T);

int ans = KMP(W, T, Nw, Nt);

printf("%d\n", ans);
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: