社区讨论
这题是不是有问题呀!!!!
P1308[NOIP 2011 普及组] 统计单词数参与者 1已保存回复 0
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 0 条
- 当前快照
- 1 份
- 快照标识符
- @lqx4erui
- 此快照首次捕获于
- 2024/01/03 09:49 2 年前
- 此快照最后确认于
- 2024/01/03 10:04 2 年前
我下载了第三个测试点的数据,P1308_3.in:
u
tIXHUguyz PZYAJL BIv NAPoemaJ aTF LOvhV m s LSa n xDn mQnO T ettIq T AL fG B Xme t fct U tQ d
P1308_3.out:
1 92
怎么数都不可能有92呀????
下面是我的代码,大佬们帮忙看下是我的代码有问题吗?
CPP#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int main()
{
string word, text;
getline(cin, word); // 读取单词
getline(cin, text); // 读取文章
// 将单词和文章都转换为小写,以便进行不区分大小写的匹配
transform(word.begin(), word.end(), word.begin(), ::tolower);
transform(text.begin(), text.end(), text.begin(), ::tolower);
int count = 0; // 单词在文章中出现的次数
size_t pos = text.find(word); // 单词在文章中第一次出现的位置
size_t firstPos = pos; // 记录第一次出现的位置
while (pos != string::npos)
{
// 检查匹配的单词是否是独立的单词
if ((pos == 0 || text[pos - 1] == ' ') && (pos + word.length() == text.length() || text[pos + word.length()] == ' '))
{
count++;
}
pos = text.find(word, pos + 1); // 查找下一个匹配
}
if (count > 0)
{ // 如果单词在文章中出现,就输出数量和第一次出现的位置
cout << count << " " << firstPos << endl;
}
else
{ // 如果单词在文章中没有出现,就输出 -1
cout << -1 << endl;
}
return 0;
}
回复
共 0 条回复,欢迎继续交流。
正在加载回复...