
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
using namespace std; bool KMP(const string &s1, const string &s2); vector<int> findMatchInFrontAndBack(const string &s2);
int partPi(const string &amatch); int main()
{
cout << "这个程序能够判断s1内是否包含s2" << endl;
string s1, s2;
cout << "请输入字符串1" << endl;
cin >> s1;
cout << "请输入字符串2" << endl;
cin >> s2; KMP(s1, s2); return ;
} bool KMP(const string &s1, const string &s2)
{
if (s2.size()>s1.size())
{
cout << "字符串1里面不包含字符串2" << endl; return false;
}
else
{
vector<int> nodes = findMatchInFrontAndBack(s2); int histroySite = ; while (s1.size() - s2.size() >= histroySite)
{
int matchNum = ; bool allOverMatch = true;
for (int i = ;i<s2.size();++i)
{
if (s1[histroySite + i] != s2[i])
{
allOverMatch = false; if (matchNum == )
{
++histroySite;
}
else
{
histroySite += (matchNum - nodes[i]);
}
break;
}
else
{
++matchNum;
}
} if (allOverMatch)
{
cout << "s1中包含s2" << endl; cout << s1 << endl; for (int i = ;i<histroySite;++i)
{
cout << " ";
}
cout << s2 << endl; return true;
}
} cout << "字符串1里面不包含字符串2" << endl;
return false;
}
} vector<int> findMatchInFrontAndBack(const string &s2)
{
if (s2.size() == )
{
vector<int> nodeOfMatch = { };
return nodeOfMatch;
}
else
{
vector<int> nodeOfMatch(s2.size() - , ); auto be = s2.begin(); auto en = be + ; for (int i = ;i<s2.size() - ;++i)
{
string amatch; amatch.insert(amatch.end(), be, en); nodeOfMatch[i] = partPi(amatch); ++en;
} nodeOfMatch.push_back(); return nodeOfMatch;
}
} int partPi(const string &amatch)
{
vector<string> frontOfString;
vector<string> backOfString; auto startOne = amatch.begin();
auto nextOne = startOne + ; for (int i = ;i<amatch.size() - ;++i)
{
string fs, bs; fs.insert(fs.end(), startOne, nextOne);
bs.insert(bs.end(), nextOne, amatch.end()); frontOfString.push_back(fs);
backOfString.push_back(bs); ++nextOne;
} sort(frontOfString.begin(), frontOfString.end());
sort(backOfString.begin(), backOfString.end()); int highestNum = ; while (frontOfString.size() != && backOfString.size() != )
{
auto s1 = frontOfString.begin();
auto s2 = backOfString.begin(); if (*s1 == *s2)
{
if (s1->size()>highestNum)
{
highestNum = s1->size();
} frontOfString.erase(s1);
backOfString.erase(s2);
}
else if (*s1<*s2)
{
frontOfString.erase(s1);
}
else
{
backOfString.erase(s2);
}
} return highestNum;
}