#include<bits/stdc++.h>
using namespace std;
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];
map<string,set<int>>brr;
sort(arr.begin(),arr.end(),[&](string a,string b){
return (int)a.length() < (int)b.length();
});
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 |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
3 |
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 |
0 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 |
1620 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
41 ms |
5184 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
127 ms |
13132 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
316 ms |
32356 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
272 ms |
26588 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |