当前位置:网站首页>字典树简单入门题(居然是蓝题?)
字典树简单入门题(居然是蓝题?)
2022-07-05 20:54:00 【汤键.TJ】
[TJOI2010] 阅读理解
题目描述
英语老师留了 N N N 篇阅读理解作业,但是每篇英文短文都有很多生词需要查字典,为了节约时间,现在要做个统计,算一算某些生词都在哪几篇短文中出现过。
输入格式
第一行为整数 N N N ,表示短文篇数,其中每篇短文只含空格和小写字母。
按下来的 N N N 行,每行描述一篇短文。每行的开头是一个整数 L L L ,表示这篇短文由 L L L 个单词组成。
接下来是 L L L 个单词,单词之间用一个空格分隔。
然后为一个整数 M M M ,表示要做几次询问。后面有 M 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 % 30\% 30% 的数据, 1 ≤ M ≤ 1 0 3 1\le M\le 10^3 1≤M≤103 。
对于 100 % 100\% 100% 的数据, 1 ≤ M ≤ 1 0 4 1\le M\le 10^4 1≤M≤104, 1 ≤ N ≤ 1 0 3 1\le N\le 10^3 1≤N≤103 。
每篇短文长度(含相邻单词之间的空格) ≤ 5 × 1 0 3 \le 5\times 10^3 ≤5×103 字符,每个单词长度 ≤ 20 \le 20 ≤20 字符。
每个测试点时限 2 2 2 秒。
所有过程都在注释里
#include<bits/stdc++.h>
using namespace std;
const int N=5e5+5;
typedef long long ll;
int son[N][30],idx,n,l,m;
char a[N];
bitset<1010> sign[N];
// 用bool会爆空间
// 用bool一个值占1字节,1个字节是8比特
// 用bitset则每个bool值只占用一个比特,大大节省空间
void insert(char str[],int k){
//插入字符串,同时告知是哪行
int p=0; //从根节点开始遍历插入
int l=strlen(str);
for(int i=0;i<l;i++){
int u=str[i]-'a'; // 方便当作下标
if(!son[p][u]) son[p][u]=++idx;
//查看当前节点p的子节点u是否已给予编号,没有则标记给予编号
p=son[p][u];//移至下一节点转换位置
}
sign[p][k]=1;//标记该字符串存在于第k行
}
void find(char str[]){
//查找该字符串
int p=0,signsign=1;//初始化,回到根节点继续查
int l=strlen(str);
for(int i=0;i<l;i++){
//遍历节点进行查询
int u=str[i]-'a';
if(!son[p][u]){
//一旦发现未被标记,则不存在该字符串,退出遍历查询
signsign=0;//同时标记不存在
break;
}
p=son[p][u];//移至下一节点转换位置
}
if(signsign){
//通过标记判断是否查询成功
for(int i=1;i<=n;i++){
if(sign[p][i]){
//通过标记查看此行是否存在此字符串
printf("%d ",i);
}
}printf("\n");
}
else{
puts("");
}
}
int main(){
cin>>n;
for(int i=1;i<=n;i++){
cin>>l;
for(int j=1;j<=l;j++){
scanf("%s",a);
insert(a,i);
}
}
cin>>m;
for(int i=1;i<=m;i++){
scanf("%s",a);
find(a);
}
return 0;
}
边栏推荐
- bazel是否有学习的必要
- 培养机器人教育创造力的前沿科技
- AI 从代码中自动生成注释文档
- Mode - "Richter replacement principle"
- Abnova e (diii) (WNV) recombinant protein Chinese and English instructions
- POJ 3414 pots (bfs+ clues)
- Return to blowing marshland -- travel notes of zhailidong, founder of duanzhitang
- 判断横竖屏的最佳实现
- 表单文本框的使用(二) 输入过滤(合成事件)
- Interpreting the daily application functions of cooperative robots
猜你喜欢

XML建模

How to make ERP inventory accounts of chemical enterprises more accurate

解读协作型机器人的日常应用功能

Abnova丨 MaxPab 小鼠源多克隆抗体解决方案

重上吹麻滩——段芝堂创始人翟立冬游记

Abnova total RNA Purification Kit for cultured cells Chinese and English instructions

实现浏览页面时校验用户是否已经完成登录的功能

基于flask写一个接口

Abnova丨DNA 标记高质量控制测试方案

Open source SPL eliminates tens of thousands of database intermediate tables
随机推荐
2.<tag-哈希表, 字符串>补充: 剑指 Offer 50. 第一个只出现一次的字符 dbc
Duchefa丨MS培养基含维生素说明书
Prosci LAG-3 recombinant protein specification
Selenium element information
Abnova CRISPR spcas9 polyclonal antibody protocol
Abbkine BCA法 蛋白质定量试剂盒说明书
Abbkine trakine F-actin Staining Kit (green fluorescence) scheme
重上吹麻滩——段芝堂创始人翟立冬游记
Simple getting started example of Web Service
表单文本框的使用(二) 输入过滤(合成事件)
Abnova丨荧光染料 620-M 链霉亲和素方案
Écrire une interface basée sur flask
基于AVFoundation实现视频录制的两种方式
PHP反序列化+MD5碰撞
php中explode函数存在的陷阱
CADD course learning (7) -- Simulation of target and small molecule interaction (semi flexible docking autodock)
Abnova丨DNA 标记高质量控制测试方案
How to renew NPDP? Here comes the operation guide!
leetcode:1139. 最大的以 1 为边界的正方形
ODPS 下一个map / reduce 准备