답안 #396267

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
396267 2021-04-29T15:59:58 Z silverfish Type Printer (IOI08_printer) C++14
100 / 100
171 ms 51068 KB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef unsigned int uint;
typedef pair<int, int> pii;
//mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());

#define pb push_back
#define FAST ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define TC int __TC__; cin >> __TC__; while(__TC__--)
#define ar array

const int INF = 1e9 + 7, N = 1e6;
int trie[N][26], p=0, last[N], n, sum=0, d[N];
string s,ans;

void insert(int i, int rt){
	if(i == s.size()){
		++last[rt];
		return;
	}
	if(!trie[rt][s[i]-'a']) trie[rt][s[i]-'a'] = ++p;
	insert(i + 1, trie[rt][s[i]-'a']);
}

void dfs1(int u){
	bool leaf = 1;
	for(int i = 0; i < 26; ++i){
		int v = trie[u][i];
		if(v){
			leaf=0;
			dfs1(v);
			d[u] = max(d[u], d[v]+1);
		}
	}

	if(leaf) d[u] = 1;
}

void dfs(int u){
	for(int i = 0; i < last[u]; ++i){
		ans += 'P';
	}

	sum -= last[u];

	if(sum==0){
		cout << ans.size() <<'\n';
		for(char c : ans){
			cout << c << '\n';
		}
		exit(0);
	}

	int deepest = 0;

	for(int i = 0; i < 26; ++i){
		if(trie[u][i] && d[trie[u][i]] > d[trie[u][deepest]]){
			deepest = i;
		}
	}

	for(int i= 0; i < 26; ++i){
		if(trie[u][i] && i != deepest){
			ans += (char)('a'+i);
			dfs(trie[u][i]);
			ans+='-';
		}
	}
	
	if(trie[u][deepest]){
		ans+=(char)('a'+deepest);
		dfs(trie[u][deepest]);
		ans+='-';
	}
}

int main()
{
	FAST;
	cin >> n;
	for(int i = 0; i < n; ++i){
		cin >> s;
		insert(0, 0);
	}

	sum = n;
	dfs1(0);
	d[0] = -INF;

	dfs(0);

	return 0;
}

Compilation message

printer.cpp: In function 'void insert(int, int)':
printer.cpp:21:7: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   21 |  if(i == s.size()){
      |     ~~^~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 2 ms 716 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 1132 KB Output is correct
2 Correct 4 ms 1356 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 3232 KB Output is correct
2 Correct 21 ms 6604 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 26 ms 7768 KB Output is correct
2 Correct 8 ms 1868 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 76 ms 18980 KB Output is correct
2 Correct 139 ms 43004 KB Output is correct
3 Correct 78 ms 22224 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 57 ms 14820 KB Output is correct
2 Correct 171 ms 51068 KB Output is correct
3 Correct 86 ms 25124 KB Output is correct
4 Correct 146 ms 48248 KB Output is correct