답안 #550911

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
550911 2022-04-19T11:27:56 Z Jomnoi Type Printer (IOI08_printer) C++17
20 / 100
55 ms 35416 KB
#include <bits/stdc++.h>
#define DEBUG false
using namespace std;

vector <char> ans;

class Node {
public :
	int len;
	bool finish;
	Node *c[26];
	Node(const int &n) : finish(false), len(n) {
		for(int i = 0; i < 26; i++) {
			c[i] = NULL;
		}
	}
};

void add(Node *node, string s, int idx = 0) {
	if(idx == s.length()) {
		node->finish = true;
		return;
	}

	int v = s[idx] - 'a';
	if(node->c[v] == NULL) {
		node->c[v] = new Node(s.length());
	}

	add(node->c[v], s, idx + 1);

	for(int i = 0; i < 26; i++) {
		if(node->c[i] != NULL) {
			node->len = max(node->len, node->c[i]->len);
		}
	}
}

void solve(Node *node) {
	if(node->finish == true) {
		ans.push_back('P');
		return;
	}

	vector <pair <int, int>> vec;
	for(int v = 0; v < 26; v++) {
		if(node->c[v] != NULL) {
			vec.emplace_back(node->c[v]->len, v);
		}
	}

	sort(vec.begin(), vec.end());

	for(auto [l, v] : vec) {
		ans.push_back(v + 'a');
		solve(node->c[v]);
		ans.push_back('-');
	}
}

int main() {
	cin.tie(nullptr)->sync_with_stdio(false);
	int N;
	cin >> N;
	Node *root = new Node(0);
	for(int i = 1; i <= N; i++) {
		string s;
		cin >> s;
		add(root, s);
	}

	solve(root);

	while(ans.back() == '-') {
		ans.pop_back();
	}

	cout << ans.size() << '\n';
	for(auto c : ans) {
		cout << c << '\n';
	}
	return 0;
}

Compilation message

printer.cpp: In constructor 'Node::Node(const int&)':
printer.cpp:10:7: warning: 'Node::finish' will be initialized after [-Wreorder]
   10 |  bool finish;
      |       ^~~~~~
printer.cpp:9:6: warning:   'int Node::len' [-Wreorder]
    9 |  int len;
      |      ^~~
printer.cpp:12:2: warning:   when initialized here [-Wreorder]
   12 |  Node(const int &n) : finish(false), len(n) {
      |  ^~~~
printer.cpp: In function 'void add(Node*, std::string, int)':
printer.cpp:20:9: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   20 |  if(idx == s.length()) {
      |     ~~~~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 320 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 0 ms 340 KB didn't print every word
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 316 KB Output is correct
2 Incorrect 1 ms 316 KB didn't print every word
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 340 KB didn't print every word
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2 ms 1732 KB didn't print every word
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 6 ms 5708 KB didn't print every word
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 17 ms 14164 KB didn't print every word
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 55 ms 35416 KB didn't print every word
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 33 ms 27752 KB didn't print every word
2 Halted 0 ms 0 KB -