这题是不是有问题呀!!!!
查看原帖
这题是不是有问题呀!!!!
1030831
JadeSpring楼主2024/1/3 09:48

我下载了第三个测试点的数据,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呀???? 下面是我的代码,大佬们帮忙看下是我的代码有问题吗?

#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;
}

2024/1/3 09:48
加载中...