求大佬帮忙看下为什么re
查看原帖
求大佬帮忙看下为什么re
1101971
DrDDs楼主2024/3/2 10:43

以下是代码

#include <bits/stdc++.h>

using namespace std;

string word, article;

int ans, ansxy;
bool flag = false;

int main() {
	getline(cin, word);
	getline(cin, article);
	for (int i = 0; i <= word.size(); i++)
		if (word[i] >= 'A' && word[i] <= 'Z')
			word[i] += 'a' - 'A';

	for (int i = 0; i <= article.size(); i++)
		if (article[i] >= 'A' && article[i] <= 'Z')
			article[i] += 'a' - 'A';

	int st = -1, ed = -1;
	for (int i = 0; i <= article.size(); i++) {
		st = ed;
		while (article[i] != ' ' && article[i] != '\n')
			i++;
		ed = i;
		string temp = article.substr(st + 1, ed - st - 1);
		if (temp == word) {
			ans++;
			if (!flag) {
				ansxy = st + 1;
				flag = true;
			}
		}
	}
	if (flag)
		cout << ans << " " << ansxy;
	else
		cout << "-1";
	return 0;
}
2024/3/2 10:43
加载中...