답안 #520303

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
520303 2022-01-29T08:35:44 Z new_acc Type Printer (IOI08_printer) C++14
100 / 100
158 ms 69440 KB
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define fi first
#define se second
#define all(a) a.begin(), a.end()
#define pb push_back

const ll maxv = LLONG_MAX;
const ll minv = maxv * (-1LL);
const int maxn = 25*1000+10;
const int maxm = 1000*1000+10;
const int SS = 1024*1024;


string s[maxn];
int trie[maxn*20][26+10];
int l = 1;
bool konczy[maxn*20];
vector <char> res;
int m1,m2;
int g[maxn*20];

void Dfs(int v, int odl) 
{
    if(v == 0) return;
    if(m1 < odl)
        m1 = odl, m2 = v;
    for(int i = 1; i <= 26; i++) 
        Dfs(trie[v][i], odl + 1);
}

bool Dfs2(int v) 
{
    if(v == 0) return false;
    if(v == m2) return true;
    g[v] = 27;
    for(int i = 1; i <= 26; i++)
        if(Dfs2(trie[v][i])) g[v] = i;
    if(g[v] != 27) return true;
    return false;
}

void Dfswyn(int v) 
{
    if(konczy[v]) res.pb('P');
    if(v == m2) return;
    for(int i = 1; i < g[v]; i++) {
        if(trie[v][i] != 0) {
            res.pb(char(i + 96));
            Dfswyn(trie[v][i]);
        }
    }
    for(int i = 26; i >= g[v]; i--) {
        if(trie[v][i] != 0) {
            res.pb(char(i+96));
            Dfswyn(trie[v][i]);
        }
    }
    if(g[v] < 27) return;
    res.pb('-');

}

void add(int ind)
{
    string a = s[ind];
    int curr = 1;
    for(int i = 0; i < a.size(); i++) {
        if(trie[curr][(int(a[i]) - 96)] == 0)
            trie[curr][(int(a[i]) - 96)] = ++l;
        curr = trie[curr][int(a[i]) - 96];
    }
    konczy[curr] = true;
}
void solve()
{
    int n;
    cin >> n;
    for(int i = 1; i <= n; i++) cin >> s[i];
    for(int i = 1; i <= n; i++) add(i);
    Dfs(1,1);
    Dfs2(1);
    Dfswyn(1);
    cout << res.size() << "\n";
    for(auto i : res)
        cout << i << "\n";
}
int main() 
{
    ios_base::sync_with_stdio(0),cin.tie(0);
    solve();
}

Compilation message

printer.cpp: In function 'void add(int)':
printer.cpp:70:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   70 |     for(int i = 0; i < a.size(); i++) {
      |                    ~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1100 KB Output is correct
2 Correct 1 ms 1100 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1100 KB Output is correct
2 Correct 1 ms 1100 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1100 KB Output is correct
2 Correct 1 ms 1112 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1100 KB Output is correct
2 Correct 1 ms 1100 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1100 KB Output is correct
2 Correct 2 ms 1620 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 2124 KB Output is correct
2 Correct 4 ms 2380 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 4940 KB Output is correct
2 Correct 18 ms 9436 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 10848 KB Output is correct
2 Correct 9 ms 3404 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 59 ms 25752 KB Output is correct
2 Correct 135 ms 58620 KB Output is correct
3 Correct 69 ms 31172 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 52 ms 20104 KB Output is correct
2 Correct 158 ms 69440 KB Output is correct
3 Correct 79 ms 35268 KB Output is correct
4 Correct 129 ms 65644 KB Output is correct