結果

問題 No.3626 Not a Prefix
コンテスト
ユーザー yaaya
提出日時 2026-08-14 22:22:33
言語 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
結果
WA  
実行時間 -
コード長 5,919 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 3,042 ms
コンパイル使用メモリ 348,264 KB
実行使用メモリ 213,508 KB
最終ジャッジ日時 2026-08-14 22:22:40
合計ジャッジ時間 6,381 ms
ジャッジサーバーID
(参考情報)
judge3_1 / judge2_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 43 WA * 2
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(ll i=a;i<b;i++)
#define rrep(i,a,b) for(ll i=a-1;i>=b;i--)
#define ll long long
#define ull unsigned ll
#define ld long double
#define bl __int128_t
#define fi first
#define se second
#define vel vector<ll>
#define vvel vector<vel>
#define pll pair<ll,ll>
#define vepll vector<pll>
#define vvepll vector<vepll>
#define ves vector<string>
#define vem vector<mint>
#define vvem vector<vem>
#define pmm pair<mint,mint>
#define cleout(i) cout<<fixed<<setprecision(i)
template<class T>using PQ=priority_queue<T,vector<T>,greater<T>>;
//               上  右 下 左
vector<int> di={-1, 0, 1, 0};
vector<int> dj={ 0, 1, 0,-1};

vector<int> dx={ 0, 1, 0,-1};
vector<int> dy={ 1, 0,-1, 0};


vector<int> ddx={ 1, 1, 1, 0, -1, -1, -1, 0 };
vector<int> ddy={ 1, 0, -1, -1, -1, 0, 1, 1 };

ll inf=1000000000000000000;//1e18
// LLONG_MAX

mt19937_64 rng((ull)chrono::steady_clock::now().time_since_epoch().count());

//[x^M]1/(1-x)^N=comb(N-1+M,M)

struct hoge{
    vel next;//aho_corasickの遷移先
    ll failure;//遷移先がない時にどこまで戻るか(trie木に含まれる文字列=suffixとなる最長の場所のindex)
    ll output;//自分未満で初めてヒットするやつ
    ll deep;//この頂点の深さ
    ll P;//親のindex
    vel ids;
    ll cnt;
    hoge(ll dp,ll var){
        next.assign(var,-1);
        deep=dp;
        P=0;
        failure=0;
        output=0;
        cnt=0;
    }
};


struct trie{
    vector<hoge> node;
    char base='a';
    ll var;
    vel id;
    trie(ll v=26):var(v){//この頂点の子供の数
        hoge x(0,var);
        node.push_back(x);
    }
    ll insert(string &S,ll num){
        ll now=0;
        rep(i,0,S.size()){
            node[now].cnt++;
            if(node[now].next[S[i]-base]==-1){
                hoge x(i+1,var);
                node[now].next[S[i]-base]=node.size();
                node.push_back(x);
            }
            ll next=node[now].next[S[i]-base];
            node[next].P=now;
            now=node[now].next[S[i]-base];
            if(i+1==S.size()){
                node[now].ids.push_back(num);
                id.push_back(now);
                //node[now].ids|=(1ll<<num);//bit用
            }
        }
        return now;
    }

    void build(){
        queue<ll> qu;
        rep(i,0,var){
            if(node[0].next[i]!=-1){
                ll child=node[0].next[i];
                node[child].failure=0;
                qu.push(node[0].next[i]);
            }else{
                node[0].next[i]=0;
            }
        }
        while(!qu.empty()){
            ll now=qu.front();
            qu.pop();
            rep(i,0,var){
                if(node[now].next[i]!=-1){
                    ll next=node[now].next[i];
                    node[next].failure=node[node[now].failure].next[i];//子供の行き先がないなら自分のところの行き先にする
                    ll f=node[next].failure;
                    //node[next].ids|=node[f].ids;//bit用
                    if(node[f].ids.size()){//子供のoutputは行き先が終端ならそこに、そうじゃないなら行き先に合わせる
                        node[next].output=f;
                    }else{
                        node[next].output=node[f].output;
                    }
                    qu.push(next);
                }else{
                    node[now].next[i]=node[node[now].failure].next[i];
                }
            }
        }
    }

    ll f(ll now,ll c){
        return node[now].next[c];
    }

    void s_list(ll now,vel &v){
        for(auto x:node[now].ids)v.push_back(x);
        while(node[now].output!=0){
            now=node[now].output;
            for(auto x:node[now].ids)v.push_back(x);
        }
        return ;
    }
    

    vel cnt(string &S){
        ll now=0;
        rep(i,0,S.size()){
            ll n=S[i]-base;
            now=f(now,n);
            node[now].cnt++;
        }
        rrep(i,node.size(),1){
            ll now=node[i].failure;
            node[now].cnt+=node[i].cnt;
        }
        vel res(id.size());
        rep(i,0,id.size())res[i]=node[id[i]].cnt;
        return res;
    }

    vvel failure_tree(){//id[i],id[j]のlcaがiとjの共通最長suffix
        ll n=node.size();
        vvel G(n);
        rep(i,1,n) {
            G[node[i].failure].push_back(i);
        }
        return G;
    }
    bool any_match(string &S){
        ll now=0;
        for(char c:S) {
            now=f(now,c-base);
            if(node[now].ids.size()||node[now].output!=0)return true;
        }
        return false;
    }

    void solve(string &ans,ll now,ll K,bool &ok){
        rep(i,0,26){
            if(node[now].ids.size()>K){
                continue;
            }else{
                if(K>=node[now].cnt){
                    cout<<"Yes\n";
                    cout<<ans<<"\n";
                    ok=1;
                    return ;
                }else{
                    ans+=(char)('a'+i);
                    if(node[now].next[i]==-1){
                        cout<<"Yes\n";
                        cout<<ans<<"\n";
                        ok=1;
                        return ;
                    }else{
                        solve(ans,node[now].next[i],K-node[now].ids.size(),ok);
                        if(ok)return ;
                    }
                    ans.pop_back();
                }
            }
        }
    }
};

void _solve(){
    ll N,K;
    cin>>N>>K;
    trie tree;
    rep(i,0,N){
        string S;
        cin>>S;
        tree.insert(S,i);
    }
    string ans="";
    bool ok=0;
    tree.solve(ans,0,N-K,ok);
    if(!ok){
        cout<<"No\n";
    }
}


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

    
    ll _;
    bool multitest=0;
    if(multitest)cin>>_;
    else _=1;
    rep(__,0,_){
        _solve();
    }
}
0