关于KMP算法的理解我是参考阮先生的解释
http://www.ruanyifeng.com/blog/2013/05/Knuth–Morris–Pratt_algorithm.html
vector<int> pattern;//这是要小的整形串
vector<int> str;//这是大的整形串
if (str.size()<pattern.size())
return false;
//构建next数组 下标为1~patten.size,下标0不用
vector<int> next(pattern.size() + 1, 0);
for (int i = 1; i<pattern.size(); ++i)
{
int j = next [i];
while (j>0 && pattern[i] != pattern[j])
j = next[j];
if (pattern[i] == pattern[j])
next[i + 1] = j + 1;//求next[i+1]的值,所以比较的是p[i]和p[j]
}
//使用KMP
int j = 0;
for (int i = 0; i<str.size(); ++i)
{
while (j>0 && str[i] != pattern[j])
j = next[j];
if (str[i] == pattern[j])
j++;//比较下一个值
if (j == pattern.size())
{
//匹配到字串了,返回真
return true;
}
}