답안 #74684

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
74684 2018-09-06T13:02:45 Z PeppaPig Type Printer (IOI08_printer) C++14
100 / 100
202 ms 65392 KB
#include <bits/stdc++.h>
 
using namespace std;
 
const int N = 5e5+5;
 
int n, ptr;
int t[N][30], sub[N];
bitset<N> leaf;
vector<char> C;
 
int subLen(int u) {
	sub[u] = 0;
	for(int i = 0; i < 26; i++) {
		int v = t[u][i];
		if(v == -1) continue;
		subLen(v);
		sub[u] = max(sub[v]+1, sub[u]);
	}
}
 
void dfs(int u) {
	if(leaf[u]) C.emplace_back('P');
	int mx = 0, hv = -1;
	for(int i = 0; i < 26; i++) {
		int v = t[u][i];
		if(v == -1) continue;
		if(sub[v] > mx) mx = sub[v], hv = i;
	}
	for(int i = 0; i < 26; i++) {
		int v = t[u][i];
		if(v == -1 || i == hv) continue;
		C.emplace_back('a'+i);
		dfs(v);
	}
	if(hv != -1) {
		C.emplace_back('a'+hv);
		dfs(t[u][hv]);
	} 
	C.emplace_back('-');
}
 
int main() {
	memset(t, -1, sizeof t);
	scanf("%d", &n);
	for(int i = 1; i <= n; i++) {
		char A[25];
		scanf(" %s", A+1);
		int cptr = 0;
		for(int i = 1; i <= strlen(A+1); i++) {
			int &pos = t[cptr][A[i]-'a'];
			if(pos == -1) pos = ++ptr;
			cptr = pos;
		}
		leaf[cptr] = 1;
	}
	subLen(0);
	dfs(0);
	while(!C.empty() && C.back() == '-') C.pop_back();
	printf("%d\n", C.size());
	for(char c : C) printf("%c\n", c);
 
	return 0;
}

Compilation message

printer.cpp: In function 'int subLen(int)':
printer.cpp:20:1: warning: no return statement in function returning non-void [-Wreturn-type]
 }
 ^
printer.cpp: In function 'int main()':
printer.cpp:50:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(int i = 1; i <= strlen(A+1); i++) {
                  ~~^~~~~~~~~~~~~~
printer.cpp:60:25: warning: format '%d' expects argument of type 'int', but argument 2 has type 'std::vector<char>::size_type {aka long unsigned int}' [-Wformat=]
  printf("%d\n", C.size());
                 ~~~~~~~~^
printer.cpp:45:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &n);
  ~~~~~^~~~~~~~~~
printer.cpp:48:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf(" %s", A+1);
   ~~~~~^~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 48 ms 59128 KB Output is correct
2 Correct 45 ms 59136 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 47 ms 59136 KB Output is correct
2 Correct 47 ms 59136 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 48 ms 59144 KB Output is correct
2 Correct 47 ms 59160 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 46 ms 59292 KB Output is correct
2 Correct 47 ms 59308 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 48 ms 59336 KB Output is correct
2 Correct 48 ms 59336 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 48 ms 59412 KB Output is correct
2 Correct 48 ms 59424 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 57 ms 59676 KB Output is correct
2 Correct 65 ms 60108 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 70 ms 60128 KB Output is correct
2 Correct 56 ms 60128 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 109 ms 61252 KB Output is correct
2 Correct 177 ms 63356 KB Output is correct
3 Correct 119 ms 63356 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 97 ms 63356 KB Output is correct
2 Correct 202 ms 64632 KB Output is correct
3 Correct 131 ms 64632 KB Output is correct
4 Correct 184 ms 65392 KB Output is correct