P3879 [TJOI2010] 阅读理解(水题)

[TJOI2010] 阅读理解

题目描述

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

输入格式

第一行为整数 N ,表示短文篇数,其中每篇短文只含空格和小写字母。

按下来的 N 行,每行描述一篇短文。每行的开头是一个整数 L ,表示这篇短文由 L 个单词组成。接下来是 L 个单词,单词之间用一个空格分隔。

然后为一个整数 M ,表示要做几次询问。后面有 M 行,每行表示一个要统计的生词。

输出格式

对于每个生词输出一行,统计其在哪几篇短文中出现过,并按从小到大输出短文的序号,序号不应有重复,序号之间用一个空格隔开(注意第一个序号的前面和最后一个序号的后面不应有空格)。如果该单词一直没出现过,则输出一个空行。

样例 #1

样例输入 #1

3
9 you are a good boy ha ha o yeah
13 o my god you like bleach naruto one piece and so do i
11 but i do not think you will get all the points
5
you
i
o
all
naruto

样例输出 #1

1 2 3
2 3
1 2
3
2

提示

对于 30% 的数据, 1 ≤ M ≤ 10^3

对于 100% 的数据,$1 ≤ M ≤ 10^41 ≤ N ≤ 10^3

每篇短文长度(含相邻单词之间的空格)≤ 5*10^3 字符,每个单词长度 ≤ 20 字符。

每个测试点时限 2 秒。

感谢@钟梓俊添加的一组数据。

过程

绿题水题,这道题数据应该比较简单,map+vector直接过,如果有大佬有易懂的字典树做法,欢迎解答

AC Code

// Problem: 
//     P3879 [TJOI2010] 阅读理解
//   
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P3879
// Memory Limit: 125 MB
// Time Limit: 2000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include<iostream>
#include<algorithm>
//#include<cstdio>
#include<map>
#include<vector>
#include<cstring>
#define ll long long 
#define endl '\n'
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define per(i,a,b) for(int i=(a);i>(b);i--)
using namespace std;
map<string,vector<int>>mp;
void insert(string str,int index){
	if(mp.find(str)==mp.end()||find(mp[str].begin(),mp[str].end(),index)==mp[str].end()){
		mp[str].push_back(index);
	}
}
int main(){
	//ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
	int n,m;string str;
	cin>>n;
	rep(i,1,n){
		cin>>m;
		while(m--){
			cin>>str;
			insert(str,i);			
		}
	}
	cin>>m;
	while(m--){
		cin>>str;
		if(mp.find(str)==mp.end())cout<<endl;
		else{
			for(auto item:mp[str]){
				cout<<item<<" ";
			}
			cout<<endl;
		}
	}
	return 0; 
} 


本文作者:今心上

本文链接:https://www.cnblogs.com/Illuminated/p/17985191

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   今心上  阅读(22)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
💬
评论
📌
收藏
💗
关注
👍
推荐
🚀
回顶
收起
🔑