답안 #717174

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
717174 2023-04-01T10:47:16 Z aedmhsn Type Printer (IOI08_printer) C++17
100 / 100
168 ms 68160 KB
#include <bits/stdc++.h>
using namespace std;
 
#define A first
#define B second
#define MP make_pair
#define ms(a, x) memset(a, x, sizeof(a)) 
 
 
#define boost() ios_base::sync_with_stdio(false); cin.tie(0)
 
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<long double, long double> pld;
const int INF = 0x3f3f3f3f;
const ll LLINF = 0x3f3f3f3f3f3f3f3f;
const double PI = acos(-1);


struct Vertex{
    vector <int> nxt;
    bool leaf=false;
    int longest=0;
    Vertex(){
        nxt.resize(26, -1);
    }
};

vector <Vertex> trie(1);
void add_string(string &s){
    int v=0;
    for(int i=0; i<s.size(); i++){
        int c = s[i]-'a';
        if(trie[v].nxt[c] == -1){
            trie[v].nxt[c] = trie.size();
            trie.emplace_back();
        }
        trie[v].longest = max(trie[v].longest, (int)s.size()-i-1);
        v=trie[v].nxt[c];
    }
    trie[v].leaf=true;
}
vector <char> ans;



// so i think there is a couple of ideas
// we can maybe sort them by size then do the print function such that it marks the strings visited ?
// well this is wierd
// 
void solve(int v){
    if(trie[v].leaf)
        ans.push_back('P'); 
    vector <pii> nodes;
    for(int i=0; i<26; i++){
        if(trie[v].nxt[i] != -1){
            nodes.push_back({trie[trie[v].nxt[i]].longest, i});
        }
    }
    sort(nodes.begin(), nodes.end());
    for(auto x:nodes){
        ans.push_back(x.B+'a');
        solve(trie[v].nxt[x.B]);
        ans.push_back('-');
    }
}
int main(){
    int n;
    cin >> n;
    vector <string> a(n);
    for(int i=0; i<n; i++){
        cin >> a[i];
        add_string(a[i]);
    }
    solve(0);
    while(ans.back() == '-'){
        ans.pop_back();
    }
    cout << ans.size() << '\n';
    for(auto x:ans) cout << x << '\n';
}

Compilation message

printer.cpp: In function 'void add_string(std::string&)':
printer.cpp:34:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   34 |     for(int i=0; i<s.size(); i++){
      |                  ~^~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 296 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 2 ms 852 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 1284 KB Output is correct
2 Correct 4 ms 1744 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 4112 KB Output is correct
2 Correct 22 ms 8616 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 26 ms 10180 KB Output is correct
2 Correct 18 ms 2892 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 67 ms 25288 KB Output is correct
2 Correct 148 ms 57404 KB Output is correct
3 Correct 84 ms 30504 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 58 ms 20020 KB Output is correct
2 Correct 168 ms 68160 KB Output is correct
3 Correct 98 ms 34600 KB Output is correct
4 Correct 155 ms 64472 KB Output is correct