top1编程
← 返回题目
题解

[TJOI2010] 阅读理解

1 条题解

  • 0
    @ 2026-10-21 1:10:24

    1. 解题思路

    为每个单词保存出现的文章编号。文章从小到大处理,同篇单词只保存一次,查询时顺序输出编号。

    2. 坑点和注意事项

    N≤1000、M≤10^4,每篇≤5000字符,单词≤20字符。不存在的单词输出空行,不能重复输出同一篇。map用字符串作键,普通数组无法直接寻址。

    3. 复杂度

    • 时间复杂度:O(WL log W+输出量)。
    • 空间复杂度:O(WL+出现篇次)。
    /*
    思路:为每个单词保存出现的文章编号。文章从小到大处理,同篇单词只保存一次,查询时顺序输出编号。
    关键规则与坑点:N≤1000、M≤10^4,每篇≤5000字符,单词≤20字符。不存在的单词输出空行,不能重复输出同一篇。map用字符串作键,普通数组无法直接寻址。
    时间复杂度:O(WL log W+输出量)。空间复杂度:O(WL+出现篇次)。
    */
    #include <bits/stdc++.h>
    using namespace std;
    map<string, set<int>> idx;
    int main()
    {
        int n;
        cin >> n;
        for (int i = 1; i <= n; i++)
        {
            int k;
            cin >> k;
            while (k--)
            {
                string s;
                cin >> s;
                idx[s].insert(i);
            }
        }
        int m;
        cin >> m;
        while (m--)
        {
            string s;
            cin >> s;
            map<string, set<int>>::iterator it = idx.find(s);
            if (it != idx.end())
            {
                bool first = true;
                for (set<int>::iterator p = it->second.begin(); p != it->second.end(); ++p)
                {
                    if (!first)
                        cout << ' ';
                    cout << *p;
                    first = false;
                }
            }
            cout << endl;
        }
    }
    
    • 1