답안 #659632

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
659632 2022-11-18T20:18:24 Z pakapu Type Printer (IOI08_printer) C++17
20 / 100
45 ms 37248 KB
#include <bits/stdc++.h>

using namespace std;

struct node {
	node* c[26];
	bool end = false;
	bool marked = false;
	
	node() {
		for(int i = 0; i < 26; i++) {
			c[i] = nullptr;
		}
	}
};

vector<char> result;

struct trie {

	node* head;

	trie() {
		head = new node;
	}

	void add(string s, bool m) {
		node* curr = head;

		for(int i = 0; i < s.size(); i++) {
			if(curr -> c[s[i] - 'a'] == nullptr) {
				curr -> c[s[i] - 'a'] = new node;
			}

			curr -> marked = m;
			curr = curr -> c[s[i] - 'a'];
		}

		curr -> end = true;
	}

	void out() {
		out(head, nullptr);
	}

	void out(node* v, node* prev) {
		int mi = -1;

		for(int i = 0; i < 26; i++) {
			if(v -> c[i] != nullptr) {
				if(v -> c[i] -> marked) {
					mi = i;
					continue;
				}

				result.push_back((char)(i + 'a'));
				if(v -> c[i] -> end) {
					result.push_back('P');
				}
				out(v -> c[i], v);
			}
		}

		if(mi != -1) {
			result.push_back((char)(mi + 'a'));
			out(v -> c[mi], v);
		}
		else {
			result.push_back('-');
		}
	}

};

signed main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);

	int n;
	cin >> n;

	vector<string> a(n);
	string mx = "";

	for(int i = 0; i < n; i++) {
		cin >> a[i];
		if(a[i].size() > mx.size()) {
			mx = a[i];
		}
	}

	trie t;

	for(auto c : a) {
		t.add(c, false);
	}
	t.add(mx, true);

	t.out();

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

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

	return 0;
}

Compilation message

printer.cpp: In member function 'void trie::add(std::string, bool)':
printer.cpp:30:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   30 |   for(int i = 0; i < s.size(); i++) {
      |                  ~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 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 0 ms 340 KB Output is correct
2 Incorrect 0 ms 212 KB didn't print every word
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 0 ms 340 KB didn't print every word
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2 ms 1824 KB didn't print every word
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 7 ms 6016 KB didn't print every word
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 19 ms 14932 KB didn't print every word
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 45 ms 37248 KB didn't print every word
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 38 ms 29188 KB didn't print every word
2 Halted 0 ms 0 KB -