给定文本txt [0..n-1]和模式pat [0..m-1] ,编写一个函数search(char pat [],char txt []) ,将所有出现的pat []都打印在txt中[] 。您可以假设n> m。
例子:
输入:txt [] =“这是一个测试文本” pat [] =“ TEST”输出:在索引10处找到的模式输入:txt [] =“ AABAACAADAABAABA” pat [] =“ AABA”输出:在索引0处找到的模式在索引9处找到的模式在索引12处找到的模式
幼稚的字符串匹配算法可将模式一一滑动。各滑动之后,在当前的换档,并且如果所有字符它逐一检查字符匹配,则打印该匹配。
像朴素算法一样,Rabin-Karp算法也可以使图案一个接一个地滑动。但是与Naive算法不同,Rabin Karp算法将模式的哈希值与文本的当前子字符串的哈希值进行匹配,如果哈希值匹配,则仅开始匹配各个字符。因此Rabin Karp算法需要计算以下字符串的哈希值。
1)模式本身。
2)所有长度为m的文本的子字符串。
C/C++
/* Following program is a C implementation of Rabin Karp
Algorithm given in the CLRS book */
#include
#include
// d is the number of characters in the input alphabet
#define d 256
/* pat -> pattern
txt -> text
q -> A prime number
*/
void search(char pat[], char txt[], int q)
{
int M = strlen(pat);
int N = strlen(txt);
int i, j;
int p = 0; // hash value for pattern
int t = 0; // hash value for txt
int h = 1;
// The value of h would be "pow(d, M-1)%q"
for (i = 0; i < M - 1; i++)
h = (h * d) % q;
// Calculate the hash value of pattern and first
// window of text
for (i = 0; i < M; i++) {
p = (d * p + pat[i]) % q;
t = (d * t + txt[i]) % q;
}
// Slide the pattern over text one by one
for (i = 0; i <= N - M; i++) {
// Check the hash values of current window of text
// and pattern. If the hash values match then only
// check for characters on by one
if (p == t) {
/* Check for characters one by one */
for (j = 0; j < M; j++) {
if (txt[i + j] != pat[j])
break;
}
// if p == t and pat[0...M-1] = txt[i, i+1, ...i+M-1]
if (j == M)
printf("Pattern found at index %d \n", i);
}
// Calculate hash value for next window of text: Remove
// leading digit, add trailing digit
if (i < N - M) {
t = (d * (t - txt[i] * h) + txt[i + M]) % q;
// We might get negative value of t, converting it
// to positive
if (t < 0)
t = (t + q);
}
}
}
/* Driver program to test above function */
int main()
{
char txt[] = "GEEKS FOR GEEKS";
char pat[] = "GEEK";
int q = 101; // A prime number
search(pat, txt, q);
return 0;
}
输出:
Pattern found at index 0
Pattern found at index 10
请参阅有关Rabin-Karp模式搜索算法的完整文章,以了解更多详细信息!
想要从精选的最佳视频中学习和练习问题,请查看《基础知识到高级C的C基础课程》。