这题就离谱啊,用了 《深入浅出》 的方法 A了之后IDE上第一个测试点都过不了,求巨佬解释
代码如下:
#include <bits/stdc++.h>
using namespace std;
int main()
{
string s,w;
getline(cin,w);
getline(cin,s);
int lw = w.length(),ls = s.length();
for (int i = 0; i < lw; i++)
{
w[i] = toupper(w[i]);
}
for (int i = 0; i < ls; i++)
{
s[i] = toupper(s[i]);
}
s = ' ' + s + ' ',w = ' ' + w + ' ';
if (s.find(w) != -1)
{
int cnt = 0,nex = s.find(w),fir = s.find(w);
while(nex != -1)
{
cnt++;
nex = s.find(w, nex + 1);
}
cout << cnt << " " << fir;
}
else
{
cout << -1;
}
return 0;
}
样例一输出给我整个
-1
? ? ?大雾