P3879 [TJOI2010] 阅读理解


(题目大骗子,最后一个序号后面明明可以有空格),我原本想的是将map中的两个值都定义成string类型
,但发现不对,然后发现题目说序号能重复,也就是说一个单词多次出现在一个句子时,只用统计一次,然后我就修改,但还是不对,也不知道为什么,我就按照答案的写法将两个值定义为string 和set就ac了

#include<iostream>
#include<set>
#include<map>
#include<algorithm>
#define int long long
const int N = 1e6;
using namespace std;
int a[N];
int ans = 0;
int n,c;
map<string, set<int>>m;
signed main() {
	int n;
	cin >> n;
	for (int i = 1; i <= n; i++) {
		int l;
		cin >> l;
		for (int j = 1; j <= l; j++) {
			string str;
			cin >> str;
			auto it = m.find(str);
			if (it == m.end()) {
				set<int>s;
				s.insert(i);
				m.insert(pair<string, set<int>>(str, s));
			}
			else { 
				it->second.insert(i);
			}
		}
	}
	int k;
	cin >> k;
	for (int i = 1; i <= k; i++) {
		string str;
		cin >> str;
		auto it = m.find(str);
		if (it == m.end())cout << endl;
		else {
			for (auto is = it->second.begin(); is != it->second.end(); is++) {
				cout << *is << " ";
			}
			cout << endl;
			
		}
	}
	return 0;
}
posted @ 2025-02-08 17:27  郭轩均  阅读(17)  评论(0)    收藏  举报