后面几页的没看,就看了第一页的,我感觉都太复杂了,转大写明明可以直接写
for (char &c : s) c = toupper(c);
他们却写了一长段,而查找字串完全可以用双参版find,例如:
while ((pos = message.find(word,pos + 1)) != -1)
if (++cnt == 1)
firstpos = pos;
他们却写了一长段。这些还好更离谱的是,有些题解甚至使用getchar来处理含空格的字符串(而且说明就是说不是用来加速的),所以求将我的短小代码加入题解,code:
#include <iostream>
#include <string>
#include <cctype>
int main(){
std::ios::sync_with_stdio(0);
std::cin.tie(0),std::cout.tie(0);
std::string word,message;
int cnt = 0,pos = -1,firstpos;
getline(std::cin,word);
getline(std::cin,message);
for (char &c : word) c = toupper(c);
for (char &c : message) c = toupper(c);
word = ' ' + word + ' ',
message = ' ' + message + ' ';
while ((pos = message.find(word,pos + 1)) != -1)
if (++cnt == 1)
firstpos = pos;
if (cnt) std::cout << cnt << ' ' << firstpos << '\n';
else std::cout << -1 << '\n';
return 0;
}
ps:不要说c不支持getline和find,可以用strstr和fgets(gets)来达到同样的效果