Submission #477888

# Submission time Handle Problem Language Result Execution time Memory
477888 2021-10-04T11:40:11 Z starplat Type Printer (IOI08_printer) C++14
100 / 100
164 ms 57752 KB
#include <bits/stdc++.h>
using namespace std;
vector<string> tmp,v,u;
vector<char> order,ans;
int n,mxlen,ct,vis[550005];
string s,mxs;
struct node{
	int word,ac[30];
}trie[550005];
void add(string x)
{
	int curr=0;
	for (int i=0;i<x.length();i++){
		if (!trie[curr].ac[s[i]-'a']) trie[curr].ac[s[i]-'a']=++ct;
		curr=trie[curr].ac[s[i]-'a'];
	}
	trie[curr].word=1;
}
void dfs(int curr,char x)
{
	ans.push_back(x);
	if (trie[curr].word) ans.push_back('P');
	for (int i=0;i<26;i++){
		if (!trie[curr].ac[i]) continue;
		dfs(trie[curr].ac[i],char(i+'a'));
	}
	ans.push_back('-');
}
void final(int curr,char x,int lvl,int ok)
{
	ans.push_back(x);
	if (trie[curr].word) ans.push_back('P');
	for (int i=0;i<26;i++){
		if (!trie[curr].ac[i]) continue;
		if (ok) final(trie[curr].ac[i],char(i+'a'),lvl+1,ok);
		else {
			if (!ok&&mxs[lvl]==char(i+'a')) continue;
			final(trie[curr].ac[i],char(i+'a'),lvl+1,1);
		}
	}
	if (mxs[lvl]!=' '&&!ok){
		final(trie[curr].ac[mxs[lvl]-'a'],mxs[lvl],lvl+1,0);
	}
	ans.push_back('-');
}
int main()
{
	cin>>n;
	for (int i=0;i<n;i++){
		cin>>s;
		if (s.length()>mxlen) mxlen=s.length(),mxs=s,tmp.clear();
		else if (s.length()==mxlen){
			tmp.push_back(s);
		}
		add(s);
	
	}
	tmp.push_back(mxs);
	sort(tmp.begin(),tmp.end());
	mxs=tmp[tmp.size()-1];
	mxs=mxs+' ';
	for (int i=0;i<26;i++){
		if (char(i+'a')==mxs[0]) continue;
		if (!trie[0].ac[i]) continue;
		dfs(trie[0].ac[i],char(i+'a'));
	}
	final(trie[0].ac[mxs[0]-'a'],mxs[0],1,0);
	cout<<int(ans.size())-mxlen<<"\n";
	for (int i=0;i<ans.size()-mxlen;i++) cout<<ans[i]<<"\n";
}

Compilation message

printer.cpp: In function 'void add(std::string)':
printer.cpp:13:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   13 |  for (int i=0;i<x.length();i++){
      |               ~^~~~~~~~~~~
printer.cpp: In function 'int main()':
printer.cpp:51:17: warning: comparison of integer expressions of different signedness: 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   51 |   if (s.length()>mxlen) mxlen=s.length(),mxs=s,tmp.clear();
      |       ~~~~~~~~~~^~~~~~
printer.cpp:52:22: warning: comparison of integer expressions of different signedness: 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   52 |   else if (s.length()==mxlen){
      |            ~~~~~~~~~~^~~~~~~
printer.cpp:69:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   69 |  for (int i=0;i<ans.size()-mxlen;i++) cout<<ans[i]<<"\n";
      |               ~^~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 2 ms 716 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 1100 KB Output is correct
2 Correct 3 ms 1356 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 3532 KB Output is correct
2 Correct 19 ms 7236 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 23 ms 8520 KB Output is correct
2 Correct 12 ms 1996 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 73 ms 20828 KB Output is correct
2 Correct 128 ms 48592 KB Output is correct
3 Correct 81 ms 24792 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 61 ms 16208 KB Output is correct
2 Correct 164 ms 57752 KB Output is correct
3 Correct 81 ms 27712 KB Output is correct
4 Correct 128 ms 55048 KB Output is correct