题解 A27.统计单词数
2026-03-21 15:45:50
发布于:广东
2阅读
0回复
0点赞
#include <bits/stdc++.h>
using namespace std;
int main() {
string s1, s2;
getline(cin, s1); // 读取单词
getline(cin, s2); // 读取文章(可能包含空格)
// 将单词转为小写
for (char &c : s1) c = tolower(c);
int n = s2.size();
int cnt = 0, firstPos = -1;
int i = 0;
while (i < n) {
// 跳过非字母
while (i < n && !isalpha(s2[i])) i++;
if (i >= n) break;
int start = i;
// 提取单词
string cur;
while (i < n && isalpha(s2[i])) {
cur += tolower(s2[i]);
i++;
}
// 比较
if (cur == s1) {
cnt++;
if (firstPos == -1) firstPos = start;
}
}
if (cnt == 0) cout << -1 << endl;
else cout << cnt << " " << firstPos << endl;
return 0;
}
这里空空如也




有帮助,赞一个