BF算法采取穷举的思路 BF算法的思想就是将目标串S的第一个字符与模式串T的第一个字符进行匹配,若相等,则继续比较S的第二个字符和 T的第二个字符;若不相等,则比较S的第二个字符和T的第一个字符,依次比较下去,直到得出最后的匹配结果。
#include <iostream>
using namespace std
;
int index(string s
,string t
)
{
int i
=0,j
=0;
while(i
<s
.length()&&j
<t
.length())
{
if(s
[i
]==t
[j
])
{
i
++;
j
++;
}
else
{
i
= i
- j
+ 1;
j
= 0;
}
}
if(j
==t
.length())
return (i
-t
.length());
else return -1;
}
int main()
{
string s
= "aaabba";
string t
= "ab";
cout
<<index(s
,t
)<<" ";
}
转载请注明原文地址: https://mac.8miu.com/read-507937.html