匹配算法
Brute Force算法
BF算法也叫朴素模式匹配算法,是由Bruce Force提出来的,算法基本思想就是简单粗暴的一次比较的问题。算法的C代码实现很简单,但是在操作中遇到了一些细节问题。
问题代码
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define LEN 100
int B_FIndex(char S[],char T[])
{
int i = 0,j = 0;
int Len_S = strlen(S); // 使用strlen函数获取字符串长度
int Len_T = strlen(T);
if(S == NULL || T == NULL || Len_S < Len_T) // Error Input
return -1;
while(i < Len_S && j < Len_T)
{
if(S[i] == T[j])
{
i++;
j++;
}// end if
else
{
i = i - j +1;
j = 0;
}// end else
}//end while
if(j >=Len_T)
{
printf("Success patterened at %d",i-j); // 匹配成功位置信息提示
return 0;
}
else
{
printf("Fail patterened");
return -1;
}
}//end BF
int main(int argc,char * argv[])
{
char S[LEN+1],T[LEN+1];
fgets(S,sizeof(S),stdin); // 采用推荐的安全函数fgets
fgets(T,sizeof(T),stdin);
B_FIndex(S,T);
}
这段代码初看没什么问题,与其他博客的代码没什么差别,编译也能顺利通过,但是却无法100%正确匹配,为什么这么说?
case1:子串=主串
hello world
hello world
Success patterened at 0
Process returned 0 (0x0) execution time : 16.536 s
Press any key to continue.
能正确输出期望值。
case2:子串<主串
hello world
hello
Fail patterened
Process returned 0 (0x0) execution time : 9.249 s
Press any key to continue.
这是什么原因造成的?
由于scanf()函数只能接收连续的字符串,遇到空格便结束,对匹配的功能有所影响,所以选择了fgets()函数(比gets()函数更安全、通用)。
如果我们直接换用gets()函数在一般的应用中是没有问题的,上述代码可以正常工作。但是gets()函数的最大问题在于它不检查输入是否超过了数组长度,这将引起内存溢出的安全问题。
真正造成问题的原因在与fgets()与gets()的另一个差别上:
- gets()逐个读入字符,指导读到换行符是停止(丢弃换行符)
- fgets()也是逐个读入字符,直到遇到首个换行符或已经读入了sizeof(str)-1个字符时结束操作。如果读入了换行符,那么它会把换行符和其他字符一起保存。
所以关键就是换行符的问题。
在case1的情况下,主串字符为
h | e | l | l | o | w | o | r | l | d | ‘\n’ | ‘\0’ |
---|
字串也为
h | e | l | l | o | w | o | r | l | d | ‘\n’ | ‘\0’ |
---|
所以匹配成功没问题。
但是在case2下,字串为
h | e | l | l | o | ‘\n’ | ‘\0’ |
---|
所以就造成了去匹配时子串是带上换行符一起去进行匹配。
以上就是问题分析了,解决方法就是在BF函数中求字符串长度时吧这个换行符减掉,修改的关键代码如下
int Len_S = strlen(S) - 1; // 使用strlen函数获取字符串长度,不包含结束符,
int Len_T = strlen(T) - 1; // 减1就去掉了换行符。
当然还有其他方法,这里仅针对该问题提出了针对性修正。
KMP算法
关于算法的介绍这里就不列举了,直接给出c++代码
next函数
这里给出两种next函数的方法,都能正常工作
/* 求模式全的netx[]函数
next[]下标从0开始,初始值定位-1
S:cdbcbabcdbcdbcacabc
T:bcdbcdbca
*/
#include <iostream>
#include <string>
using namespace std;
int get_next1(string T,int next[])
{
next[0] = -1;
int k = -1;
for (int i = 1; i < T.size(); i++)
{
while (k > -1 && T[k + 1] != T[i])///如果匹配到某一个不相等的时候,开始回溯
{
k = next[k];///回到next数组中记录的位置重新匹配
}
if (T[k + 1] == T[i] || k == -1)///如果字符一样,继续向前匹配
{
k++;
}
next[i] = k;///将最大匹配的值赋给next数组
}
}
int get_next2(string T,int next[])
{// 求next函数的值,
int i = 0;
signed int j = -1;
next[0] = -1; // initialization
while(i < T.size()) // 与T[0]字符的ASCII值比较,显然始终成立
{
if(j == -1 || T[i] == T[j])
{
++i;
++j;
next[i] = j;
}
else
j = next[j];
}
}
int main(int argc,char* argv[])
{
string T;
cout << "Please input Substring: ";
cin >> T;
int next[T.size()] = {0};
get_next1(T,next);
cout << "The next[] of substring T:";
for(int i =0; i <T.size(); i++)
cout << next[i] << " ";
cout << endl;
get_next2(T,next);
cout << "The next[] of substring T:";
for(int i =0; i <T.size(); i++)
cout << next[i] << " ";
cout << endl;
}
输出
Please input Substring: bcdbcdbca
The get_next1 of substring T:-1 0 0 0 1 2 3 4 0
The get_next2 of substring T:-1 0 0 0 1 2 3 4 5
Process returned 0 (0x0) execution time : 4.611 s
Press any key to continue.
KMP实现
#include <iostream>
#include <cstring>
using namespace std;
/*
得到next数组
*/
void get_next(string T, int *next)
{
int j, k;
j = 0;
k = -1;
next[0] = -1;
while(j <T.size())
{
if(k == -1 || T[j] == T[k])
{
j++;
k++;
next[j] = k;
}
else
{
k = next[k];
}
}
}
/*
KMP算法
*/
int KMP(string S, string T, int *next)
{
int i = 0, j = 0;
get_next(T, next);
int Len_S = S.size();
int Len_T = T.size();
while (i < Len_S && j < Len_T)
{
if (j == -1 || S[i] == T[j])
{
i++;
j++;
}
else
{
j = next[j];
}
}
if (j == Len_T)
{
cout << "Success patterned with shift " << i-j <<endl;
return i - j;
}
else
{
cout << "Fail patterned" << endl;
return -1;
}
}
int main()
{
string S,T;
cin >> S ;
cin >> T;
int next[T.size()];
int i;
Get_Next(T, next);
cout << "模式串的next的值为:";
for (i = 0; i < T.size(); i++)
cout << next[i] << " ";
cout << endl;
KMP(S, T, next);
}
输出
cdbcbabcdbcdbcacabc
bcdbcdbca
模式串的next的值为:-1 0 0 0 1 2 3 4 5
Success patterned with shift 6
Process returned 0 (0x0) execution time : 19.706 s
Press any key to continue.