結果

問題 No.3626 Not a Prefix
コンテスト
ユーザー yuunegi
提出日時 2026-08-14 23:01:40
言語 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  
実行時間 64 ms / 2,000 ms
+ 903µs
コード長 2,746 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 5,387 ms
コンパイル使用メモリ 349,784 KB
実行使用メモリ 72,236 KB
最終ジャッジ日時 2026-08-14 23:01:49
合計ジャッジ時間 5,585 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 45
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>
using namespace std;
//参考
//https://qiita.com/butsurizuki/items/7c1dd4916b9495beacea
struct node{
  int child[26]; // その頂点の子の情報
  long long weight; // その頂点が表す文字列の重み
  long long endw;
};
int main(void){
    int n,m;
    cin>>n>>m;
    vector<string>s(n);
    for(int i=0;i<n;i++)cin>>s[i];
    node init;
    for(int i=0;i<26;i++){init.child[i]=-1;}
    init.weight=0;
    init.endw=0;
    vector<node> trie={init};
    for(int i=0;i<n;i++){
        int vertex=0; // 頂点 0 は根とする
        for(int j=0;j<s[i].size();j++){
            if(trie[vertex].child[s[i][j]-'a']==-1){ // 今から行きたい子が無い場合
                trie[vertex].child[s[i][j]-'a']=trie.size(); // 次に作られる頂点の添え字は trie.size() なので、その頂点を子として登録する
                trie.push_back(init); // 新たに頂点を作成する
            }
            vertex=trie[vertex].child[s[i][j]-'a']; // 子に移動する
            (s[i].size()-1==j?trie[vertex].endw:trie[vertex].weight)++;
        }
    }
    int tmp=n-m;
    stack<array<int,3>>st;
    stack<int>ans;
    st.push({0,0,1});
    while(!st.empty()){
        int nownode=st.top()[0],nxval=st.top()[1],op=st.top()[2];
        //cout<<nownode<<" "<<nxval<<" "<<op<<endl;
        st.pop();
        if(op==1){
            tmp-=trie[nownode].endw;
            st.push({nownode,nxval,-1});
            if(tmp<0){
                continue;
            }
            if(tmp-trie[nownode].weight<0||nownode==0){
                if(trie[nownode].child[nxval]==-1){
                    ans.push(nxval);
                    cout<<"Yes"<<endl;
                    string ans2;
                    while(!ans.empty()){
                        ans2+=(char)(ans.top()+'a');
                        ans.pop();
                    }
                    reverse(ans2.begin(),ans2.end());
                    cout<<ans2<<endl;
                    return 0;
                }else{
                    st.push({trie[nownode].child[nxval],0,1});
                    ans.push(nxval);
                }
            }else{
                cout<<"Yes"<<endl;
                string ans2;
                while(!ans.empty()){
                    ans2+=(char)(ans.top()+'a');
                    ans.pop();
                }
                reverse(ans2.begin(),ans2.end());
                cout<<ans2<<endl;
                return 0;
            }
        }else if(op==-1){
            tmp+=trie[nownode].endw;
            if(nxval+1!=26){
                st.push({nownode,nxval+1,1});
            }else{
                ans.pop();
            }
        }
    }
    cout<<"No"<<endl;
    return 0;
}
0