답안 #936842

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
936842 2024-03-02T20:02:20 Z Irate Type Printer (IOI08_printer) C++14
100 / 100
83 ms 55624 KB
#include<bits/stdc++.h>
using namespace std;
struct Trie{
    vector<vector<int>>trie;
    vector<bool>isWord;
    int nxt = 1, mxN = 5e5 + 5;
    string S = "";
    Trie(){
        trie.resize(26);
        for(int i = 0;i < 26;++i){
            trie[i].resize(mxN);
        }
        isWord.resize(mxN);
    }
    void Insert(string &s){
        int cur = 0;
        for(int i = 0;i < s.size();++i){
            if(trie[s[i] - 'a'][cur]){
                cur = trie[s[i] - 'a'][cur];
            }
            else{
                trie[s[i] - 'a'][cur] = nxt;
                cur = nxt;
                nxt++;
            }
        }
        isWord[cur] = 1;
    }
    void Traverse(int cur, bool check, int indx, string &longest){
        if(isWord[cur]){
            S += 'P';
            S += '\n';
        }
        for(int i = 0;i < 26;++i){
            if(!check && indx < longest.size() && longest[indx] == char('a' + i))continue;
            if(trie[i][cur]){
                S += char('a' + i);
                S += '\n';
                Traverse(trie[i][cur], 1, 0, longest);
            }
        }
        if(!check && indx < longest.size()){
            S += longest[indx];
            S += '\n';
            Traverse(trie[longest[indx] - 'a'][cur], 0, indx + 1, longest);
        }
        S += '-';
        S += '\n';
    }
};
int main(){
    // ios_base::sync_with_stdio(0);
    // cin.tie(0);
    int n;
    cin >> n;
    Trie tr;
    int mx = 0;
    string longest = "";
    for(int i = 0;i < n;++i){
        string s;
        cin >> s;
        if(s.size() > mx){
            mx = s.size();
            longest = s;
        }
        tr.Insert(s);
    }
    tr.Traverse(0, 0, 0, longest);
    while(tr.S.back() == '-' || tr.S.back() == '\n')tr.S.pop_back();

    cout << (tr.S.size() + 1) / 2 << "\n" << tr.S;
}

Compilation message

printer.cpp: In member function 'void Trie::Insert(std::string&)':
printer.cpp:17:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   17 |         for(int i = 0;i < s.size();++i){
      |                       ~~^~~~~~~~~~
printer.cpp: In member function 'void Trie::Traverse(int, bool, int, std::string&)':
printer.cpp:35:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   35 |             if(!check && indx < longest.size() && longest[indx] == char('a' + i))continue;
      |                          ~~~~~^~~~~~~~~~~~~~~~
printer.cpp:42:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   42 |         if(!check && indx < longest.size()){
      |                      ~~~~~^~~~~~~~~~~~~~~~
printer.cpp: In function 'int main()':
printer.cpp:62:21: warning: comparison of integer expressions of different signedness: 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   62 |         if(s.size() > mx){
      |            ~~~~~~~~~^~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 51292 KB Output is correct
2 Correct 17 ms 51292 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 51292 KB Output is correct
2 Correct 17 ms 51288 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 51288 KB Output is correct
2 Correct 18 ms 51292 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 51292 KB Output is correct
2 Correct 19 ms 51352 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 51292 KB Output is correct
2 Correct 18 ms 51256 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 51292 KB Output is correct
2 Correct 19 ms 51548 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 51544 KB Output is correct
2 Correct 25 ms 51948 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 26 ms 51956 KB Output is correct
2 Correct 23 ms 51804 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 43 ms 52748 KB Output is correct
2 Correct 72 ms 54856 KB Output is correct
3 Correct 59 ms 53520 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 37 ms 52492 KB Output is correct
2 Correct 83 ms 55624 KB Output is correct
3 Correct 55 ms 53588 KB Output is correct
4 Correct 66 ms 55216 KB Output is correct