Submission #804545

# Submission time Handle Problem Language Result Execution time Memory
804545 2023-08-03T09:42:21 Z KaleemRazaSyed Type Printer (IOI08_printer) C++17
100 / 100
104 ms 64228 KB
#include<bits/stdc++.h>

using namespace std;
int ans;
struct node
{
  char c;
  int cnt, depth;
  map<char, node*> ch;
  void print(bool mx = true)
  {
    for(int i=0;i<cnt;i++)
      cout << "P\n";
    char b = '_';
    if(ch.size() and mx)
      {
	b = ch.begin()->first;
	for(auto i:ch)
	  if(ch[b]->depth < i.second->depth)
	    b = i.first;

      }
    for(auto i:ch)
      {
	if(i.first==b) continue;
	cout << i.first << '\n';
	i.second->print(false);
	cout << "-\n";
      }
    if(b!='_')
      {
	cout << b << '\n';
	ch[b]->print(true);
      }
  }
};
node *root = new node();

void add(string &s)
{
  node *r = root;
  for(int i=0;i<s.size(); i++)
    {
      char n = s[i];
      if(r->ch.find(n)==r->ch.end())
	{
	  ans++;
	  // create a new node
	  r->ch[n] = new node();
	  r->ch[n]->c = n;
	}
      r->depth = max(r->depth , int(s.size()) - i -1);
      r = r->ch[n];
    }
  r->cnt++;
}

int main()
{
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  int mx = 0;
  for(int i=0;i<n;i++)
    {
      string s;
      cin >> s;
      add(s);
      mx = max(mx , int(s.size()));
    }
  ans *= 2;
  cout << ans + n - mx << endl;
  root->print();
  return 0;
}

Compilation message

printer.cpp: In function 'void add(std::string&)':
printer.cpp:42:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   42 |   for(int i=0;i<s.size(); i++)
      |               ~^~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 316 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 320 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 316 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 2 ms 836 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 1228 KB Output is correct
2 Correct 2 ms 1492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 3860 KB Output is correct
2 Correct 16 ms 8088 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 19 ms 9556 KB Output is correct
2 Correct 8 ms 2268 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 38 ms 23708 KB Output is correct
2 Correct 83 ms 54000 KB Output is correct
3 Correct 46 ms 27964 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 34 ms 18508 KB Output is correct
2 Correct 104 ms 64228 KB Output is correct
3 Correct 53 ms 31652 KB Output is correct
4 Correct 72 ms 60692 KB Output is correct