HDU 1686 Oulipo

题目:http://acm.hdu.edu.cn/showproblem.php?pid=1686

 

大意:求模式串在主串出现的次数。。(可以重复)

 

#include <iostream>
#include <string>
using namespace std;

const int L1 = 1000010;
const int L2 = 10010;


char s[L1], p[L2];
int nxt[L2], l1, l2;
int res;

void GetNext()
{
int k = -1, j = 0;
nxt[0] = -1;
while (j < l2)
{
if (k == -1 || p[j] == p[k])
{
j++, k++;
if (p[j] != p[k])
{
nxt[j] = k;
}
else
{
nxt[j] = nxt[k];
}
}
else
{
k = nxt[k];
}
}
}


int Kmp(int pos)
{
int i = pos, j = 0;
while (i < l1 && j < l2)
{
if (j == -1 || s[i] == p[j])
{
i++, j++;
}
else
{
j = nxt[j];
}
if (j == l2)
{
res++;
j = nxt[j];
}
}
return res;
}


int main()
{
int t;
int i;
scanf("%d", &t);

while (t--)
{

scanf("%s%s", p, s);
l1 = strlen(s);
l2 = strlen(p);
GetNext();
res = 0;
printf("%d\n", Kmp(0));

}

return 0;
}



posted on 2012-04-01 17:55  [S*I]SImMon_WCG______*  阅读(183)  评论(0编辑  收藏  举报

导航