洛谷刷题C++语言 | P3879 阅读理解

​学习C++从娃娃抓起!记录下洛谷C++学习和备考过程中的题目,记录每一个瞬间。

附上汇总贴:洛谷刷题C++语言 | 汇总


【题目描述】

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

【输入】

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

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

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

【输出】

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

【输入样例】

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 2 3
2 3
1 2
3
2

【代码详解】

在这里插入图片描述

#include <bits/stdc++.h>
using namespace std;
int n, m, q;
map<string, set<int> > a;
string s;
int main()
{
    cin >> n;
    for (int i=1; i<=n; i++) {
        cin >> m;
        for (int j=1; j<=m; j++) {
            cin >> s;
            a[s].insert(i);
        }
    }
    cin >> q;
    for (int i=1; i<=q; i++) {
        cin >> s;
        set<int> se = a[s];
        for (set<int>::iterator is=se.begin(); is!=se.end(); is++)
            cout << *is << " ";
        cout << endl;
    }
    // for (map<string, set<int> >::iterator it=a.begin(); it!=a.end(); it++) {
    //     cout << it->first << " ";
    //     set<int> se = it->second;
    //     for (set<int>::iterator is=se.begin(); is!=se.end(); is++) {
    //         cout << *is << " ";
    //     }
    //     cout << endl;
    // }
    return 0;
}

【运行结果】

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
1 2 3
i
2 3
o
1 2
all
3
naruto
2
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值