답안 #477885

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
477885 2021-10-04T11:19:16 Z starplat Type Printer (IOI08_printer) C++14
100 / 100
149 ms 59644 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],go;
}trie[550005];
void add(string x,bool ok)
{
	int curr=0;
	for (int i=0;i<x.length();i++){
		if (!trie[curr].ac[x[i]-'a']) trie[curr].ac[x[i]-'a']=++ct;
		curr=trie[curr].ac[x[i]-'a'];
		trie[curr].go=ok;
		//if (trie[curr].go==true) cout<<curr<<"\n";
	}
	trie[curr].word=1;
}
void dfs(int curr)
{
	vis[curr]=1;
	if (trie[curr].word) ans.push_back('P');
	int dumb=-1;
	for (int i=0;i<26;i++){
		int node=trie[curr].ac[i];
		if (node==0) continue;
		if (vis[node]) continue;
		//if (curr==27) cout<<node<<" "<<trie[node].go<<"\n";
		if (trie[node].go==1) {
			dumb=i;
		//	cout<<trie[node].go<<" "<<curr<<" "<<dumb<<"\n" ;
		}
		else {
			ans.push_back(char(i+'a'));
			dfs(node);
			ans.push_back('-');
		}
 	}
 	//cout<<dumb<<"\n";
 	if (dumb>-1){
 		//cout<<trie[curr].ac[dumb]<<"\n";
 		ans.push_back(char(dumb+'a'));
 		dfs(trie[curr].ac[dumb]);
	 }
}
int main()
{
	cin>>n;
	for (int i=0;i<n;i++){
		cin>>s;
		if (s.length()>mxlen) mxlen=s.length(),mxs=s;
		add(s,0);
	}
	add(mxs,1);
	dfs(0);
	//dfs(trie[0].ac[mxs[0]-'a']);
	cout<<ans.size()<<"\n";
	for (char c:ans) cout<<c<<"\n";
}


Compilation message

printer.cpp: In function 'void add(std::string, bool)':
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:52:17: warning: comparison of integer expressions of different signedness: 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   52 |   if (s.length()>mxlen) mxlen=s.length(),mxs=s;
      |       ~~~~~~~~~~^~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 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 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 0 ms 204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 2 ms 800 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 1228 KB Output is correct
2 Correct 4 ms 1492 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 3660 KB Output is correct
2 Correct 20 ms 7628 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 22 ms 8904 KB Output is correct
2 Correct 15 ms 2012 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 60 ms 21948 KB Output is correct
2 Correct 116 ms 50100 KB Output is correct
3 Correct 67 ms 25868 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 47 ms 17188 KB Output is correct
2 Correct 149 ms 59644 KB Output is correct
3 Correct 79 ms 29240 KB Output is correct
4 Correct 122 ms 56308 KB Output is correct