답안 #486380

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
486380 2021-11-11T13:36:03 Z PoPularPlusPlus Type Printer (IOI08_printer) C++17
100 / 100
55 ms 7600 KB
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long 
#define pb(e) push_back(e)
#define sv(a) sort(a.begin(),a.end())
#define sa(a,n) sort(a,a+n)
#define mp(a,b) make_pair(a,b)
#define vf first
#define vs second
#define ar array
#define all(x) x.begin(),x.end()
const int inf = 0x3f3f3f3f;
const int mod = 1000000007; 
const double PI=3.14159265358979323846264338327950288419716939937510582097494459230;
bool remender(ll a , ll b){return a%b;}

bool check(string& a , string& b){
	for(int i = 0; i < (int)a.size(); i++){
		if(a[i]!=b[i])return 1;
	}
	return 0;
}

void solve(){
	int n;
	cin >> n;
	string arr[n];
	for(int i = 0; i < n; i++)cin >> arr[i];
	sort(arr,arr + n);
	int mx = 0 , pos = -1;
	for(int i = 0; i < n-1 ;i++){
		if(mx < (int)arr[i].size()){
			mx = arr[i].size();
			pos = i;
		}
	}
	if(mx < (int)arr[n-1].size()){
		mx = arr[n-1].size();
		pos = n-1;
	}
	vector<string> t;
	int l = pos , r = pos;
	while(l >= 0 && arr[l][0] == arr[pos][0])l--;
	while(r < n  && arr[r][0] == arr[pos][0])r++;
	vector<string> x;
	for(int i = l + 1; i < r; i++){
		x.pb(arr[i]);
	}
	int cur = 1;
	while(x.size() > 1){
		vector<string> y;
		for(int i = 0; i < (int)x.size(); i++){
			if(x[i][cur]!=arr[pos][cur]){
				t.pb(x[i]);
			}
			else y.pb(x[i]);
		}
		x = y;
		cur++;
	}
	t.pb(arr[pos]);
	string a[n];
	cur = 0;
	for(int i = 0; i <= l; i++){
		a[cur++] = arr[i];
	}
	for(int i = r; i < n; i++){
		a[cur++] = arr[i];
	}
	for(int i = 0; i < (int)t.size(); i++){
		a[cur++] = t[i];
	}
	for(int i = 0; i < n; i++)arr[i] = a[i];
	vector<char> v;
	string s = "";
	for(int i = 0; i < n; i++){
		while(check(s,arr[i])){
			v.pb('-');
			s.pop_back();
		}
		for(int j = s.size(); j < (int)arr[i].size(); j++){
			s += arr[i][j];
			v.pb(arr[i][j]);
		}
		v.pb('P');
	}
	cout << v.size() << '\n';
	for(char c : v)cout << c << '\n';
}

int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
	//int t;cin >> t;while(t--)
	solve();
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 2 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 3 ms 460 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 716 KB Output is correct
2 Correct 8 ms 1356 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 1480 KB Output is correct
2 Correct 9 ms 1748 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 25 ms 3148 KB Output is correct
2 Correct 47 ms 6072 KB Output is correct
3 Correct 29 ms 5060 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 23 ms 2912 KB Output is correct
2 Correct 55 ms 7600 KB Output is correct
3 Correct 33 ms 6204 KB Output is correct
4 Correct 49 ms 7464 KB Output is correct