답안 #927941

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
927941 2024-02-15T14:25:35 Z TAhmed33 Type Printer (IOI08_printer) C++
100 / 100
66 ms 5484 KB
#include <bits/stdc++.h>
using namespace std;
vector <string> arr;
string ans;
int main () {
	int n;
	cin >> n;
	arr.resize(n);
    string p;
	for (auto &i : arr) {
		cin >> i;
		if (i.length() > p.length()) {
			p = i;
		}
	}	
	sort(arr.begin(), arr.end(), [p] (string &a, string &b) {
		int cnt1 = 0, cnt2 = 0;
		for (int i = 0; i < (int)a.length() && i < (int)p.length(); i++) {
			if (a[i] != p[i]) break;
			cnt1++;
		}
		for (int i = 0; i < (int)b.length() && i < (int)p.length(); i++) {
			if (b[i] != p[i]) break;
			cnt2++;
		}
		if (cnt1 == cnt2) return a < b;
		return cnt1 < cnt2;
	});
	ans = arr[0];
	ans += 'P';
	for (int i = 1; i < n; i++) {
		if (arr[i - 1].length() > arr[i].length()) {
			int x = arr[i - 1].length() - arr[i].length();
			for (int j = 0; j < x; j++) {
				arr[i - 1].pop_back();
				ans += '-';
			}
			string dd = arr[i];
			while (!arr[i - 1].empty() && arr[i - 1] != arr[i]) {
				arr[i - 1].pop_back();
				arr[i].pop_back();
				ans += '-';
			}
			for (int j = (int)arr[i - 1].length(); j < dd.length(); j++) {
				ans += dd[j];
			}
			ans += 'P';
			arr[i] = dd;
			continue;
		}
		if (arr[i - 1].length() == arr[i].length()) {
			string dd = arr[i];
			while (!arr[i - 1].empty() && arr[i - 1] != arr[i]) {
				arr[i - 1].pop_back();
				arr[i].pop_back();
				ans += '-';
			}
			for (int j = (int)arr[i - 1].length(); j < dd.length(); j++) {
				ans += dd[j];
			}
			ans += 'P';
			arr[i] = dd;
			continue;
		}
		string dd = arr[i];
		while (arr[i].length() > arr[i - 1].length()) {
			arr[i].pop_back();
		}
		while (!arr[i - 1].empty() && arr[i - 1] != arr[i]) {
			arr[i - 1].pop_back();
			arr[i].pop_back();
			ans += '-';
		}
		for (int j = (int)arr[i - 1].length(); j < dd.length(); j++) {
			ans += dd[j];
		}
		ans += 'P';
		arr[i] = dd;
		continue;
	}
	cout << ans.length() << '\n';
	for (auto i : ans) cout << i << '\n';
}

Compilation message

printer.cpp: In function 'int main()':
printer.cpp:44:45: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   44 |    for (int j = (int)arr[i - 1].length(); j < dd.length(); j++) {
      |                                           ~~^~~~~~~~~~~~~
printer.cpp:58:45: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   58 |    for (int j = (int)arr[i - 1].length(); j < dd.length(); j++) {
      |                                           ~~^~~~~~~~~~~~~
printer.cpp:74:44: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   74 |   for (int j = (int)arr[i - 1].length(); j < dd.length(); j++) {
      |                                          ~~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 428 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 444 KB Output is correct
2 Correct 2 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 568 KB Output is correct
2 Correct 10 ms 1108 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 12 ms 1140 KB Output is correct
2 Correct 10 ms 1112 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 29 ms 2548 KB Output is correct
2 Correct 52 ms 4816 KB Output is correct
3 Correct 35 ms 3772 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 26 ms 2296 KB Output is correct
2 Correct 66 ms 5484 KB Output is correct
3 Correct 40 ms 4592 KB Output is correct
4 Correct 56 ms 5328 KB Output is correct