Submission #519035

# Submission time Handle Problem Language Result Execution time Memory
519035 2022-01-25T12:01:50 Z AdamGS Type Printer (IOI08_printer) C++17
100 / 100
65 ms 7512 KB
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define rep(a, b) for(int a = 0; a < (b); ++a)
#define st first
#define nd second
#define pb push_back
#define all(a) a.begin(), a.end()
const int LIM=25e3+7, INF=1e9+7;
string T[LIM], P[LIM];
int lcp(string a, string b) {
	int ans=0;
	while(ans<a.size() && ans<b.size() && a[ans]==b[ans]) ++ans;
	return ans;
}
int main() {
	ios_base::sync_with_stdio(0); cin.tie(0);
	int n;
	cin >> n;
	rep(i, n) cin >> T[i];
	sort(T, T+n);
	pair<int,int>ma={-1, -1};
	rep(i, n) {
		pair<int,int>p={T[i].size(), i};
		ma=max(ma, p);
	}
	P[0]=T[ma.nd];
	int mi1=0, mi2=0, p1=ma.nd-1, p2=ma.nd+1;
	if(p1>=0) mi1=lcp(P[0], T[p1]);
	else mi1=-1;
	if(p2<n) mi2=lcp(P[0], T[p2]);
	else mi2=-1;
	rep(i, n-1) {
		if(mi1>=mi2) {
			P[i+1]=T[p1];
			--p1;
			if(p1>=0) mi1=lcp(P[0], T[p1]);
			else mi1=-1;
		} else {
			P[i+1]=T[p2];
			++p2;
			if(p2<n) mi2=lcp(P[0], T[p2]);
			else mi2=-1;
		}
	}
	reverse(P, P+n);
	string x="";
	vector<char>ans;
	rep(i, n) {
		int p=lcp(x, P[i]);
		while(x.size()>p) {
			ans.pb('-');
			x.pop_back();
		}
		while(x.size()<P[i].size()) {
			ans.pb(P[i][x.size()]);
			x+=P[i][x.size()];
		}
		ans.pb('P');
	}
	cout << ans.size() << '\n';
	for(auto i : ans) cout << i << '\n';
}

Compilation message

printer.cpp: In function 'int lcp(std::string, std::string)':
printer.cpp:14:11: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   14 |  while(ans<a.size() && ans<b.size() && a[ans]==b[ans]) ++ans;
      |        ~~~^~~~~~~~~
printer.cpp:14:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   14 |  while(ans<a.size() && ans<b.size() && a[ans]==b[ans]) ++ans;
      |                        ~~~^~~~~~~~~
printer.cpp: In function 'int main()':
printer.cpp:52:17: warning: comparison of integer expressions of different signedness: 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   52 |   while(x.size()>p) {
      |         ~~~~~~~~^~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1868 KB Output is correct
2 Correct 1 ms 1868 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1868 KB Output is correct
2 Correct 1 ms 1868 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1876 KB Output is correct
2 Correct 1 ms 1868 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 1868 KB Output is correct
2 Correct 1 ms 1876 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1868 KB Output is correct
2 Correct 2 ms 1868 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 1868 KB Output is correct
2 Correct 2 ms 1996 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 2124 KB Output is correct
2 Correct 9 ms 2508 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 2476 KB Output is correct
2 Correct 9 ms 2500 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 26 ms 3572 KB Output is correct
2 Correct 47 ms 6636 KB Output is correct
3 Correct 30 ms 5636 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 25 ms 3012 KB Output is correct
2 Correct 65 ms 7512 KB Output is correct
3 Correct 37 ms 6080 KB Output is correct
4 Correct 53 ms 7296 KB Output is correct