結果

問題 No.628 Tagの勢い
ユーザー merom686merom686
提出日時 2018-01-06 15:31:34
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 3 ms / 2,000 ms
コード長 2,181 bytes
コンパイル時間 1,531 ms
コンパイル使用メモリ 96,888 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-16 20:03:04
合計ジャッジ時間 2,010 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 1 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 3 ms
5,376 KB
testcase_14 AC 3 ms
5,376 KB
testcase_15 AC 3 ms
5,376 KB
testcase_16 AC 3 ms
5,376 KB
testcase_17 AC 2 ms
5,376 KB
testcase_18 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;

vector<pair<int, string>> a;

struct Trie {
    static constexpr int M = 5, O = 'a';
    struct Node {
        int children[2];
        int count;
    };
    Trie() : nodes(1) {}
    void insert(const string& s, int c) {
        int k = 0;
        int n = s.size();
        for (int i = 0; i < n; i++) {
            int c = s[i] - O;
            for (int j = M - 1; j >= 0; j--) {
                int v = c >> j & 1;
                int& p = nodes[k].children[v];
                if (p == 0) {
                    k = p = nodes.size();
                    nodes.push_back(Node());
                    nodes[k].count = -1;
                } else {
                    k = p;
                }
            }
        }
        if (nodes[k].count < 0) nodes[k].count = 0;
        nodes[k].count += c;
    }
    void dfs(int k, int j, string& s) {
        int64_t d = s.size();

        if (nodes[k].count >= 0 && d > 0) {
            a.push_back({ nodes[k].count, s });
        }

        if (j == 0) s.push_back(0); else s.back() <<= 1;
        if (++j == M) s.back() += O, j = 0;

        for (int v = 0; v < 2; v++) {
            int k1 = nodes[k].children[v];
            if (k1 == 0) continue;

            if (v) s.back()++;
            int b = s.back(), n = s.size();
            dfs(k1, j, s);
            s.resize(n); s.back() = b;
        }
    }
    vector<Node> nodes;
};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);

    int n;
    cin >> n;

    Trie tr;
    string t;
    for (int i = 0; i < n; i++) {
        int j;
        cin >> j;

        int m, s;
        cin >> m >> s;

        for (int k = 0; k < m; k++) {
            cin >> t;
            tr.insert(t, s);
        }
    }

    string s;
    tr.dfs(0, 0, s);

    stable_sort(a.begin(), a.end(), [](const pair<int, string>& p0, const pair<int, string>& p1) {
        return p0.first > p1.first;
    });

    for (int i = 0; i < min(10, (int)a.size()); i++) {
        cout << a[i].second << ' ' << a[i].first << '\n';
    }

    return 0;
}
0