답안 #477859

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
477859 2021-10-04T08:25:03 Z starplat Type Printer (IOI08_printer) C++14
100 / 100
143 ms 56148 KB
#include <bits/stdc++.h>
using namespace std;
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;
		add(s);
	}
	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:12:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   12 |  for (int i=0;i<x.length();i++){
      |               ~^~~~~~~~~~~
printer.cpp: In function 'int main()':
printer.cpp:50:17: warning: comparison of integer expressions of different signedness: 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   50 |   if (s.length()>mxlen) mxlen=s.length(),mxs=s;
      |       ~~~~~~~~~~^~~~~~
printer.cpp:61:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   61 |  for (int i=0;i<ans.size()-mxlen;i++) cout<<ans[i]<<"\n";
      |               ~^~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 2 ms 712 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 1100 KB Output is correct
2 Correct 3 ms 1356 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 3532 KB Output is correct
2 Correct 22 ms 7196 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 23 ms 8520 KB Output is correct
2 Correct 11 ms 1956 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 61 ms 20788 KB Output is correct
2 Correct 120 ms 47284 KB Output is correct
3 Correct 94 ms 24312 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 67 ms 16316 KB Output is correct
2 Correct 143 ms 56148 KB Output is correct
3 Correct 88 ms 27580 KB Output is correct
4 Correct 139 ms 53048 KB Output is correct