字符串搜索的RK算法,谁研究过,我有问题啊

yang113200 2003-01-05 11:27:28
算法中的d和Q值应该是多少?
...全文
100 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
林仪明 2003-01-17
  • 打赏
  • 举报
回复
hehe!
<<algrothm desgin manuals>>
yang113200 2003-01-17
  • 打赏
  • 举报
回复
wlfjck(忧愁的):对中文好像不好使。你有没有试过。
yang113200 2003-01-17
  • 打赏
  • 举报
回复
lins(*有为青年*) :哪里找啊?
wlfjck 2003-01-11
  • 打赏
  • 举报
回复

/* Rabin Karp string search algorithm */
#include <iostream.h>
#include <string.h>

unsigned int q = 65521; /* largest prime < 2^16 */
unsigned int d = 257; /* base for numeric interpretation of strings */
unsigned int dM; /* d^(M-1), where M is the length of the pattern */

void hash_init(int M){
dM = 1;
for (int i = 1; i < M; i++) dM = (dM*d)%q ;
}
unsigned int hash(char* t, int n){
unsigned h = t[0];
for (int i = 1; i < n; i++)
h = (d*h + t[i])%q;
return h;
}
unsigned int hash_next(char* t, int M, int h){
unsigned int oldest = (dM*t[0])%q;
unsigned int oldest_removed = ((h + q) - oldest)%q;
return (d*oldest_removed + t[M])%q;
}
int equal_string(char* p, char* q, int n){
/* returns "true" if first n characters of p and q are identical */
return n == 0 || ( *p == *q && equal_string(p+1, q+1, n-1));
}

int rksearch(char* p, char* a)
/*
Rabin-Karp string search.
returns index of first occurrence of string p in a.
returns length of a if there is no occurrence of p in a.
*/
{int M = strlen(p);
int N = strlen(a);
hash_init(M);
unsigned int h1 = hash(p, M);
cout << h1 << "\n";
unsigned int h2 = hash(a, M);
cout << h2 << "\n";
for (char* b = a; b < a + N - M; b++)
{
cout << h1 << " " << h2 << " " << equal_string(p, b, M) << " " << b << "\n";
if (h2 == h1 && equal_string(p, b, M)) return b-a;
h2 = hash_next(b, M, h2);
}
return N;
}

int main()
/*
tests rksearch
*/
{
int index;
char* p = "hello";
char* a = "All test programs contain the word hello, bye.";
cout << "\nGiven pattern \n\"" << p << "\" and text \n\"" << a << "\"\n";
index = rksearch(p,a);
cout << "rksearch returns " << index << "\n";

a = "hello, world";
cout << " Given pattern \n\"" << p << "\" and text \n\"" << a << "\"\n";
index = rksearch(p,a);
cout << "rksearch returns " << index << "\n";

a = "Goodbye, cruel world";
cout << " Given pattern \n\"" << p << "\" and text \n\"" << a << "\"\n";
index = rksearch(p,a);
cout << "rksearch returns " << index << "\n";
return 0;
}


yang113200 2003-01-07
  • 打赏
  • 举报
回复
有没有人研究过啊?
nabie 2003-01-07
  • 打赏
  • 举报
回复
我只研究过 BM 算法,不过也忘得差不多了。:)
qousan 2003-01-05
  • 打赏
  • 举报
回复
什么叫RK算法呀(我是不是问得很菜呀),能介绍一二吗?谢了!
yang113200 2003-01-05
  • 打赏
  • 举报
回复
各位,有没有知道RK算法的。
可以发程序到我的信箱yang1132@163.com
一定感谢,是要请客的喔

33,007

社区成员

发帖
与我相关
我的任务
社区描述
数据结构与算法相关内容讨论专区
社区管理员
  • 数据结构与算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧