字符串匹配和KMP模式匹配(没太学懂,暂时不写)
作者:互联网
//字符串匹配
int stringMatching(string longstr, string smallstr){
int lLen = longstr.size(), sLen = smallstr.size();
for (int i = 0; i < lLen - sLen; ++i) {
for (int j = 0; j < sLen; ++j) {
if (longstr[i + j] != smallstr[j]) {
break;
}
if (j == sLen - 1) //代表所有字符都匹配上了
return i; //返回长字符串中的匹配起始位置
}
}
return -1; //代表没找到
}
int main()
{
string longstr, smallstr;
cin >> longstr;
fflush(stdin); //清空缓冲区,主要是清除输入第一个字符串后的回车符号
cin>> smallstr;
int position = stringMatching(longstr, smallstr);
if (position != -1) {
cout << "成功匹配:" << position << endl;
}
else
cout << "匹配失败" << endl;
return 0;
}
标签:sLen,string,int,longstr,smallstr,KMP,字符串,不写,模式匹配 来源: https://www.cnblogs.com/2944014083-zhiyu/p/14877757.html