//字符串匹配
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;
}