#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;
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 |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
10 ms |
1608 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
49 ms |
5232 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
124 ms |
13080 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
298 ms |
32408 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
263 ms |
26564 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |