답안 #554212

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
554212 2022-04-28T02:50:09 Z happypotato Type Printer (IOI08_printer) C++17
100 / 100
109 ms 5280 KB
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define debug(n) cout << "DEBUG1 " << n << "\n";
#define debug2(n, m) cout << "DEBUG2 " << n << ' ' << m << "\n";
#define debug3(n, m, x) cout << "DEBUG3 " << n << ' ' << m << ' ' << x << "\n";
#define debug4(n, m, x, y) cout << "DEBUG3 " << n << ' ' << m << ' ' << x << ' ' << y << "\n";
#define ss_def stringstream ss // stringstream, do ss_def(str), then ss >> n (n is int)
#define upzero(n) rrn < 0) n = 0
#define alive(n) cout << "Alive here: checkpoint " << n << endl
#pragma GCC optimize("Ofast")
using namespace std;
void yes() {
	cout << "YES\n";
	return;
}
void no() {
	cout << "NO\n";
	return;
}
bool cmp1(string a, string b) {
	if (a.length() != b.length()) return a.length() < b.length();
	for (int i = 0; i < min(a.length(), b.length()); i++) {
		if (a[i] != b[i]) return a[i] < b[i];
	}
	return false;
}
string last;
bool cmp2(string a, string b) {
	int cnt1 = a.length(), cnt2 = b.length();
	for (int i = 0; i < a.length(); i++) {
		if (a[i] != last[i]) {
			cnt1 = i;
			break;
		}
	}
	for (int i = 0; i < b.length(); i++) {
		if (b[i] != last[i]) {
			cnt2 = i;
			break;
		}
	}
	if (cnt1 != cnt2) return cnt1 < cnt2;
	return a < b;
}
vector<char> ans;
vector<string> v;
void solve(int case_no) {
    // implementation
    int n;
    cin >> n;
    for (int i = 0; i < n; i++) {
    	string str;
    	cin >> str;
    	v.push_back(str);
	}
    sort(v.begin(), v.end(), cmp1);
    last = v[v.size() - 1];
    sort(v.begin(), v.end() - 1, cmp2);
    string str = "";
    int strl = 0;
    for (int i = 0; i < n; i++) {
    	int vl = v[i].length();
        int ptr = 0;
        while (ptr < min(vl, strl)) {
            if (str[ptr] == v[i][ptr]) ptr++;
            else break;
        }
    	while (str.length() > ptr) {
    		ans.push_back('-');
    		str.erase(strl - 1, 1);
    		strl--;
		}
		for (int j = strl; j < vl; j++) {
			ans.push_back(v[i][j]);
			str += v[i][j];
			strl++;
		}
		ans.push_back('P');
	}
    printf("%d\n", ans.size());
	for (char c : ans) printf("%c\n", c);
}
int main() {
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    srand(time(NULL));
    int t = 1;
//  cin >> t;
    for (int i = 1;i <= t;i++) solve(i);
}
/*
 
*/

Compilation message

printer.cpp: In function 'bool cmp1(std::string, std::string)':
printer.cpp:23:20: warning: comparison of integer expressions of different signedness: 'int' and 'const long unsigned int' [-Wsign-compare]
   23 |  for (int i = 0; i < min(a.length(), b.length()); i++) {
      |                  ~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
printer.cpp: In function 'bool cmp2(std::string, std::string)':
printer.cpp:31:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   31 |  for (int i = 0; i < a.length(); i++) {
      |                  ~~^~~~~~~~~~~~
printer.cpp:37:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   37 |  for (int i = 0; i < b.length(); i++) {
      |                  ~~^~~~~~~~~~~~
printer.cpp: In function 'void solve(int)':
printer.cpp:69:26: warning: comparison of integer expressions of different signedness: 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   69 |      while (str.length() > ptr) {
      |             ~~~~~~~~~~~~~^~~~~
printer.cpp:81:14: warning: format '%d' expects argument of type 'int', but argument 2 has type 'std::vector<char>::size_type' {aka 'long unsigned int'} [-Wformat=]
   81 |     printf("%d\n", ans.size());
      |             ~^     ~~~~~~~~~~
      |              |             |
      |              int           std::vector<char>::size_type {aka long unsigned int}
      |             %ld
# 결과 실행 시간 메모리 Grader output
1 Correct 0 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 0 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 320 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 340 KB Output is correct
2 Correct 3 ms 468 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 668 KB Output is correct
2 Correct 15 ms 1008 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 1268 KB Output is correct
2 Correct 23 ms 1144 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 44 ms 2456 KB Output is correct
2 Correct 97 ms 3940 KB Output is correct
3 Correct 79 ms 3368 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 44 ms 2392 KB Output is correct
2 Correct 109 ms 5280 KB Output is correct
3 Correct 90 ms 4196 KB Output is correct
4 Correct 100 ms 4936 KB Output is correct