#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MN=25002;
int N,ans=0;
string w[MN];
vector<string> ss;
map<string,int> wpos;
int main() {
ios::sync_with_stdio(0);cin.tie(0);
cin>>N;
for(int i=1;i<=N;++i) cin>>w[i];
sort(w+1,w+1+N);
int mx=0,mxi=0;
for(int i=1;i<=N;++i) {
wpos[w[i]]=i;
if((int)w[i].size()>mx) {
mx=w[i].size();
mxi=i;
}
}
int endp=N;
for(int i=1;i<=(int)w[mxi].size();++i)
for(int j=1;j<=N;++j) {
if((int)w[j].size()<i) continue;
if(w[j].substr(0,i)==w[mxi].substr(0,i)) wpos[w[j]]=++endp;
}
sort(w+1,w+1+N,[](string a,string b) {return wpos[a]<wpos[b];});
string ct="";
for(int i=1;i<=N;++i) {
string cw=w[i];
while(ct.size()>cw.size()) {
ct.pop_back();
++ans;
ss.push_back("-");
}
while(ct.size()<cw.size()) cw.pop_back();
while(ct!=cw) {
ct.pop_back();
cw.pop_back();
++ans;
ss.push_back("-");
}
while(ct.size()<w[i].size()) {
++ans;
ss.push_back(string(1,w[i][ct.size()]));
ct.push_back(w[i][ct.size()]);
}
++ans;
ss.push_back("P");
}
cout<<ans<<'\n';
for(string s:ss) cout<<s<<'\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
980 KB |
Output is correct |
2 |
Correct |
1 ms |
1108 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1108 KB |
Output is correct |
2 |
Correct |
1 ms |
1108 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1108 KB |
Output is correct |
2 |
Correct |
1 ms |
1108 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1108 KB |
Output is correct |
2 |
Correct |
1 ms |
1108 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1108 KB |
Output is correct |
2 |
Correct |
3 ms |
1492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
1744 KB |
Output is correct |
2 |
Correct |
6 ms |
2380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
3552 KB |
Output is correct |
2 |
Correct |
66 ms |
5964 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
58 ms |
10156 KB |
Output is correct |
2 |
Correct |
93 ms |
4612 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
181 ms |
19356 KB |
Output is correct |
2 |
Correct |
271 ms |
37840 KB |
Output is correct |
3 |
Correct |
213 ms |
21460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
178 ms |
19536 KB |
Output is correct |
2 |
Correct |
313 ms |
38440 KB |
Output is correct |
3 |
Correct |
259 ms |
21980 KB |
Output is correct |
4 |
Correct |
281 ms |
38484 KB |
Output is correct |