Submission #924980

# Submission time Handle Problem Language Result Execution time Memory
924980 2024-02-10T09:20:50 Z vjudge1 Type Printer (IOI08_printer) C++17
100 / 100
125 ms 58088 KB
#include <bits/stdc++.h>
#define ll long long
#define F first
#define S second
#define pii pair<int,int>
const int mod = 1e9 + 7;
const int mxN = 6e6 + 5;
using namespace std;
int trie[mxN][26];
int nodeCnt;
char cnt[mxN];
int num[mxN];
int p[mxN];
string chk;
int ed;
int mx;
void insert(int u = 0){
  for(int i = 0;i <chk.size();i++){
    if(!trie[u][chk[i] - 'a']) trie[u][chk[i] - 'a'] = ++nodeCnt;
    u = trie[u][chk[i] - 'a'];
    cnt[u] = chk[i];
  }
  num[u]++;
}
void calcD(int u = 0,int dep = 0){
  if(dep > mx){
    ed = u;
    mx = dep;
  }
  // cout<<u<<' ';
  for(int i = 0;i < 26;i++){
    if(trie[u][i]) {
      p[trie[u][i]] = u;
      calcD(trie[u][i],dep + 1);
    }
  }
}
set<int>path;
vector<char>ans;
void query(int u = 0){
    queue<int> q;
    int lst = -1;
    for(int i = 0;i < 26;i++){
      if(trie[u][i]){
        if(path.find(trie[u][i]) != path.end()) lst = trie[u][i];
        else q.push(trie[u][i]);
      }
    }
    while(num[u]--){
      ans.push_back('P');
    }
    if(lst != -1) q.push(lst);
    while(q.size()){
      ans.push_back(cnt[q.front()]);
      query(q.front());
      ans.push_back('-');
      q.pop();
    }
}
signed main(){
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >>n;
  for(int i = 0;i < n;i++){
    cin >>chk;
    insert();
  }
  calcD();
  while(ed){
    path.insert(ed);
    ed = p[ed];
  }
  query();
  // cout<<"TEST\n";
  while(ans.back() == '-') ans.pop_back();
  cout<<ans.size()<<'\n';
  for(auto x : ans){
    cout<<x<<'\n';
  }
}

Compilation message

printer.cpp: In function 'void insert(int)':
printer.cpp:18:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   18 |   for(int i = 0;i <chk.size();i++){
      |                 ~~^~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4440 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4440 KB Output is correct
2 Correct 2 ms 4956 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 5208 KB Output is correct
2 Correct 3 ms 5468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 7256 KB Output is correct
2 Correct 14 ms 12632 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 23 ms 13660 KB Output is correct
2 Correct 8 ms 5976 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 61 ms 23956 KB Output is correct
2 Correct 103 ms 48588 KB Output is correct
3 Correct 54 ms 27100 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 35 ms 20360 KB Output is correct
2 Correct 125 ms 58088 KB Output is correct
3 Correct 60 ms 29904 KB Output is correct
4 Correct 100 ms 55500 KB Output is correct