如何解决拉宾-卡普Rabin-Karp错误回答,功率提高至p
我已经开始学习字符串处理算法,并想在C ++中实现rabin-Karp算法。我服用了:
p =大于字符集的质数:31 m = mod操作的质数:1e9 + 9
根据我发现的情况,有多种方法可以实现该算法,并且我已经测试了其中两种:当我们使用更高的索引时增加功率,当我们使用更高的索引时降低功率。
例如关于字符串S [0..n-1]
让M1 = S [0] * p ^ 0 + S [1] * p ^ 1 + S [2] * p ^ 2 + ... + S [n-1] * p ^ n-1
让M2 = S [0] * p ^ n-1 + S [1] * p ^ n-2 + S [2] * p ^ n-3 ... + S [n-1] * p ^ 0
我已经实现了这两种方法,只能使用M2获得成功的结果。
M1的代码:
int rabinKarpM1(string s,string t) {
int a = (int)s.size(),b = (int)t.size();
long long p = 31,m = 1e9+9;
long long powTable[a] = {0};
powTable[0] = 1;
for (int i = 1; i < a; i++) {
powTable[i] = powTable[i-1] * p % m;
}
long long hashS = 0,hashT = 0;
for (int i = 0; i < a; i++) {
hashS = (hashS + (s[i] - 'a' + 1)*powTable[i] % m) % m;
hashT = (hashT + (t[i] - 'a' + 1)*powTable[i] % m) % m;
}
if (hashS == hashT) {
bool match = true;
for (int i = 0; i < a; i++) {
if (s[i] != t[i]) {
match = false;
break;
}
}
if (match) {
return 0;
}
}
for (int i = 0; i+a-1 < b; i++) {
hashT = (hashT - (t[i] - 'a' + 1)) / p;
hashT = hashT + (t[i+a] - 'a' + 1)*powTable[a-1] % m;
hashT = hashT % m;
if (hashS == hashT) {
bool match = true;
for (int j = i+1; j < a+i+1; j++) {
if (s[j-i-1] != t[j]) {
match = false;
break;
}
}
if (match) {
return i+1;
}
}
}
return -1;
}
M2代码:
int rabinKarpM2(string s,hashT = 0;
for (int i = 0; i < a; i++) {
hashS = (hashS + (s[i] - 'a' + 1)*powTable[a-i-1] % m) % m;
hashT = (hashT + (t[i] - 'a' + 1)*powTable[a-i-1] % m) % m;
}
if (hashS == hashT) {
bool match = true;
for (int i = 0; i < a; i++) {
if (s[i] != t[i]) {
match = false;
break;
}
}
if (match) {
return 0;
}
}
for (int i = 0; i+a-1 < b; i++) {
hashT = (hashT + m - (t[i] - 'a' + 1)*powTable[a-1] % m) % m * p;
hashT = hashT + (t[i+a] - 'a' + 1) % m;
hashT = hashT % m;
if (hashS == hashT) {
bool match = true;
for (int j = i+1; j < a+i+1; j++) {
if (s[j-i-1] != t[j]) {
match = false;
break;
}
}
if (match) {
return i+1;
}
}
}
return -1;
}
测试输入: 字符串s = foobarfoo 字符串t = barfoobarfoobarfoobarfoobarfoobarfoobarfoo
我在M2上有正确的结果,但在M1上没有。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。