题意:英语老师留了 N 篇阅读理解作业,但是每篇英文短文都有很多生词需要查字典,为了节约时间,现在要做个统计,算一算某些生词都在哪几篇短文中出现过。

思路:建立一个string映射到set的映射

#include <bits/stdc++.h>
using namespace std;

map<string,set<int> > mp;
set<int>::iterator it;

int main()
{
    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
	int n, m;
	cin>>n;
	for(int i = 1;i <= n; i++)
	{
	    int l;
	    cin>>l;
		string s;
	    for(int j = 0;j < l; j++)
		{
	        cin>>s;
	        mp[s].insert(i);
	    }
	}
	cin>>m;
	while(m--)
	{
	    string s;
	    cin>>s;
	    if(mp.count(s))
	        for(it = mp[s].begin(); it !=mp[s].end(); it++)
	            cout<<*it<<" ";
	    cout<<endl;
	}
	return 0;
}

更多推荐