답안 #797944

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
797944 2023-07-30T07:39:38 Z acatmeowmeow Type Printer (IOI08_printer) C++11
100 / 100
140 ms 106448 KB
#include <bits/stdc++.h>

using namespace std;

#define int long long 

struct trie {
	bool mark;
	int height = 1;
	trie *next[26];
	
	void extend(int index) { if (!next[index]) next[index] = new trie(); }

	void add(string&s, int index) {
		if (s.size() - 1 == index) mark = true;
		else {
			char nextChar = s[index + 1];
			extend(nextChar - 'a');
			next[nextChar - 'a']->add(s, index + 1);
		}
	}

	void query(vector<char>&ans) {
		if (mark) ans.push_back('P');
		vector<pair<int, int>> arr;
		for (int i = 0; i < 26; i++) {
			if (!next[i]) continue;
			arr.push_back({next[i]->height, i});
		}
		sort(arr.begin(), arr.end());
		for (auto&v : arr) {
			int index = v.second;
			ans.push_back(index + 'a');
			next[index]->query(ans);
			ans.push_back('-');
		}
	}

	void dfs() {
		for (int i = 0; i < 26; i++) {
			if (!next[i]) continue;
			next[i]->dfs();
			height = max(height, next[i]->height + 1);
		}
	}
};

const int N = 25000;
int n;
trie *root[26];

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cin >> n;
	for (int i = 1; i <= n; i++) {
		string s;
		cin >> s;
		if (!root[s[0] - 'a']) root[s[0] - 'a'] = new trie();
		root[s[0] - 'a']->add(s, 0);
	}
	vector<pair<int, int>> arr;
	for (int i = 0; i < 26; i++){
		if (!root[i]) continue;
		root[i]->dfs();
		arr.push_back({root[i]->height, i});
	}
	sort(arr.begin(), arr.end());
	vector<char> ans;
	for (auto&v : arr) {
		int index = v.second;
		ans.push_back(index + 'a');
		root[index]->query(ans);
		ans.push_back('-');
	}
	while (ans.size() && ans.back() == '-') ans.pop_back();
	cout << ans.size() << '\n';
	for (auto&v : ans) cout << v << '\n';
	return 0;
}

Compilation message

printer.cpp: In member function 'void trie::add(std::string&, long long int)':
printer.cpp:15:20: warning: comparison of integer expressions of different signedness: 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
   15 |   if (s.size() - 1 == index) mark = true;
      |       ~~~~~~~~~~~~~^~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 1108 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 1876 KB Output is correct
2 Correct 3 ms 2388 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 6296 KB Output is correct
2 Correct 17 ms 13268 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 15696 KB Output is correct
2 Correct 6 ms 3608 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 54 ms 38872 KB Output is correct
2 Correct 118 ms 89428 KB Output is correct
3 Correct 61 ms 46148 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 47 ms 30404 KB Output is correct
2 Correct 140 ms 106448 KB Output is correct
3 Correct 71 ms 52300 KB Output is correct
4 Correct 117 ms 100488 KB Output is correct