#include<bits/stdc++.h>
using namespace std;
string mx;
bool compare(string &a,string &b){
for(int i = 0 ;i < (int)a.size() && i < (int)b.size();i++){
if(a[i] == b[i]) continue;
if(a[i] == mx[i])
return false;
if(b[i] == mx[i])
return true;
return (a[i] < b[i]);
}
return (int)a.size() < (int)b.size();
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
int n;cin>>n;
vector<string>arr(n);
for (int i = 0;i<n;++i){cin>>arr[i];if (arr[i].length() > mx.length())mx = arr[i];}
map<string,set<int>>brr;
sort(arr.begin(),arr.end(),compare);
for (int i = 0;i<n;++i){
string s;
for (auto x:arr[i]){
s+=x;
brr[s].insert(i);
}
}
vector<char>ans;
vector<int>taken(n,false);
for (int i = 0;i<n;++i){
if (!taken[i]){
string cur;
for (int j = 0;j<(int)arr[i].length();++j){
ans.push_back(arr[i][j]);
cur+=arr[i][j];
}
ans.push_back('P');
taken[i] = true;
while(!cur.empty()){
while(!brr[cur].empty() && taken[*brr[cur].begin()]){
brr[cur].erase(*brr[cur].begin());
}
if (!brr[cur].empty()){
int x = *brr[cur].begin();
//cout<<cur<<" "<<x<<'\n';
taken[x] = true;
while(cur.length() < arr[x].length()){
ans.push_back(arr[x][cur.length()]);
cur+=ans.back();
}
ans.push_back('P');
continue;
}
ans.push_back('-');
cur.pop_back();
}
}
}
while(!ans.empty() && ans.back() =='-')ans.pop_back();
cout<<ans.size()<<'\n';
for (auto x:ans)cout<<x<<'\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
320 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
340 KB |
Output is correct |
2 |
Correct |
7 ms |
980 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
1620 KB |
Output is correct |
2 |
Correct |
15 ms |
1956 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
42 ms |
5324 KB |
Output is correct |
2 |
Correct |
92 ms |
11068 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
125 ms |
13060 KB |
Output is correct |
2 |
Correct |
110 ms |
9292 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
307 ms |
32552 KB |
Output is correct |
2 |
Correct |
683 ms |
73972 KB |
Output is correct |
3 |
Correct |
515 ms |
48020 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
251 ms |
26568 KB |
Output is correct |
2 |
Correct |
841 ms |
87916 KB |
Output is correct |
3 |
Correct |
583 ms |
54264 KB |
Output is correct |
4 |
Correct |
732 ms |
82692 KB |
Output is correct |