結果

問題 No.3626 Not a Prefix
コンテスト
ユーザー ぽえ
提出日時 2026-08-14 17:54:46
言語 C++23
(gcc 15.2.0 + boost 1.90.0)
コンパイル:
g++-15 -O2 -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 72 ms / 2,000 ms
+ 102µs
コード長 1,715 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 2,278 ms
コンパイル使用メモリ 344,788 KB
実行使用メモリ 79,072 KB
最終ジャッジ日時 2026-08-14 20:55:04
合計ジャッジ時間 4,841 ms
ジャッジサーバーID
(参考情報)
judge2_0 / judge3_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 45
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>
using namespace std;

struct node {
    int cnt=0, end=0, pre=0;
    bool flag = false;
    map<char, node*> to;
};

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

    int n, m; cin >> n >> m;

    node root;
    vector<node*> nodes{&root};

    for (int i=0; i<n; i++) {
        string s; cin >> s;
        node* cur = &root; cur->cnt++;
        for (auto& c : s) {
            if (!cur->to.contains(c)) {
                cur->to[c] = new node;
                nodes.push_back(cur->to[c]);
            }
            cur = cur->to[c];
            cur->cnt++;
        }
        cur->end++;
    }

    for (auto& v : nodes) {
        for (auto [c, u] : v->to) {
            u->pre = v->pre + u->end;
        }
    }

    for (auto it=nodes.rbegin(); it!=nodes.rend(); it++) {
        node* v = *it;
        if (v!=&root && v->cnt+v->pre-v->end<=n-m) v->flag = true;
        if (v->pre<=n-m && v->to.size()<26) v->flag = true;
        for (auto& [c, u] : v->to) v->flag |= u->flag;
    }

    if (!root.flag) {
        cout << "No\n";
        return 0;
    }

    cout << "Yes\n";

    string ans = "";
    node* cur = &root;

    while (true) {
        if (cur!=&root && cur->cnt+cur->pre-cur->end<=n-m) break;
        for (char c='a'; c<='z'; c++) {
            auto it = cur->to.find(c);
            if (it == cur->to.end()) {
                if (cur->pre <= n-m) {
                    ans += c;
                    cout << ans << '\n';
                    return 0;
                }
            } else if (it->second->flag) {
                ans += c;
                cur = it->second;
                break;
            }
        }
    }

    cout << ans << '\n';
}
0