Submission #659638

# Submission time Handle Problem Language Result Execution time Memory
659638 2022-11-18T20:33:51 Z pakapu Type Printer (IOI08_printer) C++17
100 / 100
125 ms 101104 KB
#include <bits/stdc++.h>

using namespace std;

#define int short

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;
		head -> marked = true;
	}

	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 = curr -> c[s[i] - 'a'];
			curr -> marked = m;
		}

		curr -> end = true;
	}

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

	void out(node* v, node* prev) {
		if(v -> end) {
			result.push_back('P');
		}

		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'));
				out(v -> c[i], v);
			}
		}

		if(mi != -1) {
			result.push_back((char)(mi + 'a'));
			out(v -> c[mi], v);
		}
		
		if(!v -> marked) {
			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();

	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:33:20: warning: comparison of integer expressions of different signedness: 'short int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   33 |   for(int i = 0; i < s.size(); i++) {
      |                  ~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 1108 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 1764 KB Output is correct
2 Correct 3 ms 2260 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 6024 KB Output is correct
2 Correct 16 ms 12772 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 20 ms 14968 KB Output is correct
2 Correct 7 ms 3796 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 51 ms 37204 KB Output is correct
2 Correct 116 ms 84888 KB Output is correct
3 Correct 53 ms 44488 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 47 ms 29248 KB Output is correct
2 Correct 125 ms 101104 KB Output is correct
3 Correct 63 ms 50500 KB Output is correct
4 Correct 98 ms 95428 KB Output is correct