답안 #396175

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
396175 2021-04-29T14:07:53 Z Berted Type Printer (IOI08_printer) C++14
50 / 100
121 ms 42964 KB
#include <iostream>

using namespace std;

const int INF = 1e8;

int N; string ans;
int adj[500001][26], cnt[500001], DP[500001], sz = 0;

inline int newNode()
{
	for (int i = 0; i < 26; i++) adj[sz][i] = -1;
	return sz++;
}

inline void insert(int u, int i, const string& s)
{
	if (i < s.size())
	{
		int &v = adj[u][s[i] - 'a'];
		if (v == -1) {v = newNode();}
		insert(v, i + 1, s); DP[u] = max(DP[u], DP[v]) + 1;
	}
	else {cnt[u]++; DP[u] = max(1, DP[u]);}
}

inline void solve(int u, bool m)
{
	int hvy = -1;
	for (int i = 0; i < cnt[u]; i++) ans.push_back('P');
	for (int i = 0; i < 26; i++)
	{
		if (adj[u][i] != -1 && m && (hvy == -1 || DP[adj[u][i]] > DP[adj[u][hvy]])) hvy = i;
	}
	for (int i = 0; i < 26; i++)
	{
		if (adj[u][i] != -1 && hvy != i) 
		{
			ans.push_back(i + 'a');
			solve(adj[u][i], 0);
			ans.push_back('-');
		}
	}
	if (hvy != -1) {ans.push_back(hvy + 'a'); solve(adj[u][hvy], 1);}
}

int main()
{
	ios :: sync_with_stdio(0); cin.tie(0); cout.tie(0);
	cin >> N;
	newNode();
	for (int i = 0; i < N; i++) {string S; cin >> S; insert(0, 0, S);}
	solve(0, 1);
	cout << ans.size() << "\n";
	for (const char &c : ans) cout << c << "\n";
	return 0;
}

Compilation message

printer.cpp: In function 'void insert(int, int, const string&)':
printer.cpp:18:8: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   18 |  if (i < s.size())
      |      ~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 352 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 2 ms 716 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 3 ms 1100 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 11 ms 3276 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 26 ms 7800 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 61 ms 18964 KB Output is correct
2 Correct 121 ms 42964 KB Output is correct
3 Incorrect 68 ms 22244 KB Output isn't correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 48 ms 14796 KB Output isn't correct
2 Halted 0 ms 0 KB -