#include <bits/stdc++.h>
#define hmod (long long)1223334444555553
using namespace std;
int n,len,mx,indx,cnt,now,tmp;
string s[25005];
char node[500005];
bool build,gg;
vector <int> edge[500005];
bool p[500005];
vector <char> ans;
void dfs(int x){
if (x != 0) ans.push_back(node[x]);
if (p[x]) ans.push_back('P');
for (int i=0;i<edge[x].size();i++){
dfs(edge[x][i]);
}
ans.push_back('-');
}
int main(){
ios::sync_with_stdio(false);
//freopen("typ7b.in","r",stdin);
cin >> n;
for (int i=1;i<=n;i++){
now=0;
cin >> s[i]; len=s[i].length();
if (len > mx || (len == mx && s[i] > s[indx])){
mx=len;
indx=i;
}
for (int j=0;j<len;j++){
build=true;
for (int k=0;k<edge[now].size();k++){
if (node[edge[now][k]] == s[i][j]){
now=edge[now][k];
build=false;
break;
}
}
if (build){
edge[now].push_back(++cnt);
node[cnt]=s[i][j];
now=cnt;
}
}
p[now]=true;
}
len=s[indx].length(); now=0;
for (int i=0;i<=cnt;i++){
for (int j=0;j<edge[i].size();j++){
for (int k=1;k<edge[i].size();k++){
if (node[edge[i][k]] < node[edge[i][k-1]])
swap(edge[i][k],edge[i][k-1]);
}
}
}
for (int i=0;i<len;i++){
for (int j=0;j<edge[now].size();j++){
if (node[edge[now][j]] == s[indx][i]){
tmp=edge[now][j];
edge[now].erase(edge[now].begin()+j); edge[now].push_back(tmp);
now=tmp;
break;
}
}
}
dfs(0);
cout << (int)ans.size()-mx-1 << "\n";
for (int i=0;i<(int)ans.size()-mx-1;i++){
cout << ans[i] << "\n";
}
return 0;
}
/*
8
xxvebmc
ixhvdhcjxon
hsspmkly
labfaryosskugbkiuffd
yerx
mhgpafawzhnt
eejzatjmnqxctn
n
*/
Compilation message
printer.cpp: In function 'void dfs(int)':
printer.cpp:15:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
15 | for (int i=0;i<edge[x].size();i++){
| ~^~~~~~~~~~~~~~~
printer.cpp: In function 'int main()':
printer.cpp:33:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
33 | for (int k=0;k<edge[now].size();k++){
| ~^~~~~~~~~~~~~~~~~
printer.cpp:50:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
50 | for (int j=0;j<edge[i].size();j++){
| ~^~~~~~~~~~~~~~~
printer.cpp:51:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
51 | for (int k=1;k<edge[i].size();k++){
| ~^~~~~~~~~~~~~~~
printer.cpp:58:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
58 | for (int j=0;j<edge[now].size();j++){
| ~^~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
12748 KB |
Output is correct |
2 |
Correct |
9 ms |
12724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
12748 KB |
Output is correct |
2 |
Correct |
8 ms |
12740 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
12748 KB |
Output is correct |
2 |
Correct |
9 ms |
12748 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
12748 KB |
Output is correct |
2 |
Correct |
8 ms |
12812 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
12748 KB |
Output is correct |
2 |
Correct |
9 ms |
12984 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
13084 KB |
Output is correct |
2 |
Correct |
10 ms |
13132 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
13772 KB |
Output is correct |
2 |
Correct |
22 ms |
14924 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
15176 KB |
Output is correct |
2 |
Correct |
15 ms |
13516 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
52 ms |
19244 KB |
Output is correct |
2 |
Correct |
118 ms |
27756 KB |
Output is correct |
3 |
Correct |
61 ms |
20804 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
48 ms |
17240 KB |
Output is correct |
2 |
Correct |
132 ms |
30652 KB |
Output is correct |
3 |
Correct |
68 ms |
21824 KB |
Output is correct |
4 |
Correct |
108 ms |
29628 KB |
Output is correct |