思路:这道题我一开始并没有什么好思路,所以是看了别人的解答后写的,这里感谢洛谷用户Aimyhtixela,原文链接我也会放在后面
这题主要使用c++中string类的find函数,还有大小写转换tolower(toupper)函数。有不懂这些方法的同学可以自行查阅一下。此外,这里空格处理a = ’ ’ + a + ’ '是避免单词被包含,例如要找to,结果Ottoman里含有to,但这个to并不是我们想要的。
#include<iostream>
#include<string>
using namespace std;
int main() {
string a, b;
getline(cin, a);
getline(cin, b);
for (int i = 0; i < a.length(); i++) {
a[i] = towlower(a[i]);
}
for (int i = 0; i < b.length(); i++) {
b[i] = towlower(b[i]);
}
a = ' ' + a + ' ';
b = ' ' + b + ' ';
if (b.find(a) == string::npos) {
cout << -1;
}
else {
int m = b.find(a);
int n = b.find(a), count = 0;
while (n != string::npos) {
count++;
n = b.find(a, n + 1);
}
cout << count << " " << m << endl;
}
}