Visitors hit counter dreamweaver

POJ 3461 还是两种方法


    上午我用了Rabin-Karp算法做的。基本的数据可以测试通过,但是一提交就WA。偶滴天啊,我不知道错在哪啊。。我是非专业的。。呜呜。找了半天找不出。算了。看人家都是用KMP做的,那我下午就用KMP写一个吧。一定把它拿下!!哼哼

   Rabin-Karp:

#include <iostream>
#include <fstream>
#include <math.h>
#include <string.h>

using namespace std;

#define M 16381*4733+1
int nCount;

void Rabin_Karp(char*T,char* W,int d,int q )
{
//搜索W在T中的位置
//参数d:字母表的进制,即字母表的元素个数
//参数q:一个比较大的素数,只需d*q<字长
int n=strlen(T);
int m=strlen(W);
if(n<m) return;
int i;
__int64 h=1;
for(i=1; i<=m-1; i++) //计算h
h=(h*d)%q;
__int64 w=0,t=0;
//预处理,计算P,t0
for(i=0; i<m; i++)
{
w=((d*w+W[i])%q);
t=((d*t+T[i])%q);
}
int s;
for(s=0; s<n-m+1; s++) //匹配
{
if(w==t)
{
for(i=0; i<m; i++) //进一步验证
{
if(W[i]!=T[s+i])
break;
}
if(i==m)
{
nCount++;
}
}
if(s<n-m)
t=(d*(t-T[s]*h)+T[s+m])%q; //计算ts+1
}
}

int main()
{
int n;
char W[10001];
char T[1000001];
freopen("acm.txt","r",stdin);
scanf("%d",&n);
getchar();
while(n--)
{
gets(W);
gets(T);
nCount=0;
Rabin_Karp(T,W,27,M);
printf("%d\n",nCount);
}
return 0;

}

 

KMP: AC的。

#include <iostream>
#include <fstream>
#include <string.h>

using namespace std;
#define N 10001
#define M 1000001
int next[N];
int nCount;

void get_next(char* str)
{
int i=0,j=-1,len=strlen(str);
next[i]=j;
while(i<len)
{
while(j>=0 && str[i]!=str[j]) j=next[j];
i++;j++;
next[i]=j;
}
}

void kmp_search(char *T,char *W)
{
int i=0,j=0,len1=strlen(T),len2=strlen(W);
while(i<len1)
{
while(j>=0 && T[i]!=W[j]) j=next[j];
i++;j++;
if(j==len2)
{
nCount++;
j=next[j];
}
}
}

int main()
{
int n;
char W[N];
char T[M];
freopen("acm.txt","r",stdin);
scanf("%d",&n);
getchar();
while(n--)
{
scanf("%s",W);
scanf("%s",T);
memset(next,0,sizeof(next));
nCount=0;
get_next(W);
kmp_search(T,W);
printf("%d\n",nCount);
}

return 0;
}

 

posted @ 2012-04-03 12:32  Jason Damon  阅读(217)  评论(0编辑  收藏  举报