Submission #384743

# Submission time Handle Problem Language Result Execution time Memory
384743 2021-04-02T07:07:04 Z hivakarami Type Printer (IOI08_printer) C++14
80 / 100
299 ms 262148 KB
#include<bits/stdc++.h>
 
using namespace std;
 
typedef long long int ll;
typedef long double ld;
#define f first
#define s second
 
const int N = 1e5 + 100;
const ll mod = 998244353;
const ll inf = 1e9 + 10;

int node = 1;
bool mark[N], last[N];
deque<char> ans;
vector<pair<int, int>> adj[N];
int h[N], c[N][30], par[N];


void dfs(int u)
{
	if(mark[u])
		ans.push_back('P');
		
	int x2 = -1, t2 = -1;	
	for(auto it : adj[u])
	{
		int x = it.f, t = it.s;
		if(last[x])
		{
			x2 = x;
			t2 = t;
			continue;
		}
		
		ans.push_back(char(t+'a'));
		dfs(x);
	}
	
	if(x2 != -1)
	{
		ans.push_back(char(t2+'a'));
		dfs(x2);
	}
	
	ans.push_back('-');
	
}



void add(int u, string s)
{
	if(h[u] == s.size())
	{
		mark[u] = true;
		return;
	}
	
	int t = s[h[u]]-'a';
	
	if(c[u][t] == 0)
	{
		c[u][t] = node;
		adj[u].push_back({node, t});
		h[node] = h[u] + 1;
		par[node] = u;
		node++;
	}
	add(c[u][t], s);
}


 
int main()
{
    ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);	
	
	int n;
	cin >> n;
	for(int i = 0; i < n; i++)
	{
		string s;
		cin >> s;
		add(0, s);
	}
	
	int mx = 0;
	for(int i = 0; i < node; i++)
	{
		if(h[i] > h[mx])
			mx = i;
	}
	
	while(mx != 0)
	{
		last[mx] = true;
		mx = par[mx];
	}
	
	dfs(0);
	
	
	while(ans.size() && ans.back() == '-')
		ans.pop_back();
	
	cout << ans.size() << endl;
	for(auto t : ans)
		cout << t << endl;
	
	
		
	return 0;
}
 
/*
3
print
the
poem
*/

 

Compilation message

printer.cpp: In function 'void add(int, std::string)':
printer.cpp:55:10: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   55 |  if(h[u] == s.size())
      |     ~~~~~^~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 2668 KB Output is correct
2 Correct 2 ms 2668 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 2668 KB Output is correct
2 Correct 3 ms 2668 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 2796 KB Output is correct
2 Correct 3 ms 2668 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 2796 KB Output is correct
2 Correct 3 ms 2668 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 2796 KB Output is correct
2 Correct 19 ms 3308 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 36 ms 3948 KB Output is correct
2 Correct 43 ms 4076 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 118 ms 6764 KB Output is correct
2 Correct 257 ms 11564 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 299 ms 13164 KB Output is correct
2 Correct 90 ms 4972 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 228 ms 262144 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 190 ms 262148 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -