Submission #384752

# Submission time Handle Problem Language Result Execution time Memory
384752 2021-04-02T07:15:09 Z hivakarami Type Printer (IOI08_printer) C++14
100 / 100
179 ms 83180 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 = 5e5 + 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('-');
	
}



inline void add(string s)
{
	int u = 0;
	
	while(h[u] != s.size())
	{
		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++;
		}
		
		u = c[u][t];
	}
	
	mark[u] = true;
	return;
	
}

 
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(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() << '\n';
	for(auto t : ans)
		cout << t << '\n';
	
	
		
	return 0;
}
 
/*
3
print
the
poem
*/

 

Compilation message

printer.cpp: In function 'void add(std::string)':
printer.cpp:57:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   57 |  while(h[u] != s.size())
      |        ~~~~~^~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 9 ms 12140 KB Output is correct
2 Correct 11 ms 12140 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 12164 KB Output is correct
2 Correct 9 ms 12140 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 12140 KB Output is correct
2 Correct 11 ms 12140 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 12140 KB Output is correct
2 Correct 9 ms 12140 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 12140 KB Output is correct
2 Correct 12 ms 12652 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 13164 KB Output is correct
2 Correct 12 ms 13548 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 21 ms 16108 KB Output is correct
2 Correct 30 ms 20844 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 34 ms 22380 KB Output is correct
2 Correct 18 ms 14188 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 78 ms 37996 KB Output is correct
2 Correct 154 ms 71916 KB Output is correct
3 Correct 84 ms 42988 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 66 ms 32108 KB Output is correct
2 Correct 179 ms 83180 KB Output is correct
3 Correct 93 ms 47084 KB Output is correct
4 Correct 154 ms 79724 KB Output is correct