题解
2025-07-15 11:20:36
发布于:广东
13阅读
0回复
0点赞
#include <bits/stdc++.h>
using namespace() {
string word, text;
getline(cin, word);
getline(cin, text);
// 转换目标单词为小写
transform(word.begin(), word.end(), word.begin(), ::tolower);
int count = 0_pos = -1;
int n = text.length();
for (int i = 0; i < n;) {
// 跳过空格
while (i < n && text[i] == ' ')
++i;
if (i >= n)
break;
int start = i;
// 找到单词结尾
while (i < n && text[i] != ' ')
++i;
int end = i;
string current = text.substr(start, end - start);
// 转换为小写进行比较
string lower_current = current;
transform(lower_current.begin(), lower_current.end(), lower_current.begin(), ::tolower);
if (lower_current == word) {
// 判断前后是否为单词边界
bool left = (start == 0 || !isalpha(text[start - 1]));
bool right = (end == n || !isalpha(text[end]));
if (left && right) {
if (count == 0)
first_pos = start;
count++;
}
}
}
if (count > 0)
cout << count << " " << first_pos << endl;
else
cout << -1 << endl;
return 0;
}
这里空空如也
有帮助,赞一个