1 条题解

  • 0
    @ 2025-10-14 9:16:39
    #include <bits/stdc++.h>
    using namespace std;
    int n;
    int tot = 2;
    int t[100000][30];
    int cnt[100000];
    string str[1020];
    void add(string s) {
        int h = 1;
        cnt[h]++;
        for (char c : s) {
            int si = c - 'a';
            if (t[h][si] <= 0) {
                t[h][si] = tot;
                tot++;
            }
            h = t[h][si];
            cnt[h]++;
        }
        return;
    }
    string query(string s) {
        int h = 1;
        string ans = "";
        for (char c : s) {
            int si = c - 'a';
            ans += c;
            h = t[h][si];
            if (cnt[h] <= 1) {
                break;
            }
        }
        return ans;
    }
    int main() {
        while (cin >> str[n]) {
            add(str[n]);
            n++;
        }
        for (int i = 0; i < n; i++) {
            cout << str[i] << ' ' << query(str[i]) << endl;
        }
        return 0;
    }
    
    • 1

    信息

    ID
    1186
    时间
    1000ms
    内存
    256MiB
    难度
    (无)
    标签
    (无)
    递交数
    0
    已通过
    0
    上传者