Submission #63240

#TimeUsernameProblemLanguageResultExecution timeMemory
63240TenuunType Printer (IOI08_printer)C++17
100 / 100
198 ms108448 KiB
#include<bits/stdc++.h>
#define pb push_back
using namespace std;

string m;

struct node{
	char c;
	int val=0, mx=0;
	node *l[26];
	node(){
		for(int i=0; i<26; i++) {
			l[i]=NULL;
		}
	}
};

vector<char>res;

node *root=new node;

void insert(string s){
	node *now=root;
	int v;
	for(int i=0; i<s.length(); i++){
		v=s[i]-'a';
		if(now->l[v]==NULL){
			now->l[v]=new node;
			now->l[v]->c=s[i];
			now->l[v]->val++;
		}
		else{
			now->l[v]->val++;
		}
		now->mx++;
		now=now->l[v];
	}
}

void swp(){
	node *now=root;
	int ind=0;
	while(ind<m.length()){
		for(int i=0; i<26; i++){
			if(i==m[ind]-'a'){
				swap(now->l[25], now->l[i]);
				now=now->l[25];
				break;
			}
		}
		ind++;
	}
}

void print(node *now){
	int cnt=0;
	for(int i=0; i<26; i++){
		if(now->l[i]==NULL) continue;
		cnt++;
		res.pb(now->l[i]->c);
		//cout << now->l[i]->mx << " " << now->l[i]->val << endl;
		for(int j=now->l[i]->mx; j<now->l[i]->val; j++) res.pb('P');
		print(now->l[i]);
		res.pb('-');
	}
	if(cnt==0){
		//res.pb('P');
	}
}

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int n, t=0;
	cin >> n;
	string s;
	for(int i=0; i<n; i++){
		cin >> s;
		if(s.length()>t){
			m=s;
			t=s.length();
		}
		insert(s);
	}
	swp();
	print(root);
	while(res[res.size()-1]=='-'){
		res.pop_back();
	}
	cout << res.size() << endl;
	for(int i=0; i<res.size(); i++) if(res[i]!='#')cout << res[i] << '\n';
	return 0;
}

Compilation message (stderr)

printer.cpp: In function 'void insert(std::__cxx11::string)':
printer.cpp:25:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0; i<s.length(); i++){
               ~^~~~~~~~~~~
printer.cpp: In function 'void swp()':
printer.cpp:43:11: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  while(ind<m.length()){
        ~~~^~~~~~~~~~~
printer.cpp: In function 'int main()':
printer.cpp:79:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   if(s.length()>t){
      ~~~~~~~~~~^~
printer.cpp:91:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0; i<res.size(); i++) if(res[i]!='#')cout << res[i] << '\n';
               ~^~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...