#include <bits/stdc++.h>
#define endl '\n'
#define int long long
using namespace std;
const long long longlongmax=9223372036854775807;
const int modul=998244353;
const long long mod = 1e9 + 7;
int N=25000*20+9;
vector<char> rez(0);
vector<bool> kraj(N,0),drugi_krug(N,0);
vector<vector<int>> trie(N,vector<int>(26,0));
string odabrana;
int ind_c=0;
void ubaci(string&rec){
int node=0;
for(auto&x:rec){
int c=x-'a';
if(!trie[node][c]) trie[node][c]=++ind_c;
node=trie[node][c];
}
kraj[node]=true;
if(rec==odabrana){
node=0;
for(auto&x:rec){
int c=x-'a';
node=trie[node][c];
drugi_krug[node]=true;
}
}
}
void dfs1(int node){
if(kraj[node])
rez.push_back('P');
bool ok=false;
for(auto x:trie[node]){
if(x)
ok=true;
}
if(!ok) {rez.push_back('-');return;}
for(int i=0; i<26; i++){
if(!trie[node][i]) continue;
if(drugi_krug[trie[node][i]]) continue;
rez.push_back('a'+i);
dfs1(trie[node][i]);
}
if(node)
rez.push_back('-');
return;
}
void dfs2(int node,int ind,int odstup){
if(kraj[node])
rez.push_back('P');
bool ok=false;
for(auto x:trie[node]){
if(x)
ok=true;
}
if(!ok){rez.push_back('-');return;}
for(int i=0; i<26; i++){
if(!trie[node][i]) continue;
if(!odstup&&i==(odabrana[ind]-'a')) continue;
rez.push_back('a'+i);
dfs2(trie[node][i],ind,odstup+1);
}
if(!odstup){
rez.push_back(odabrana[ind]);
dfs2(trie[node][odabrana[ind]-'a'],ind+1,odstup);
}
if(node)
rez.push_back('-');
return;
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
//freopen("factory.in","r",stdin);
//freopen("factory.out","w",stdout);
int n;
cin >> n;
int maxsiz=0;
vector<string> niz(0);
for(int i=0; i<n; i++){
string rec;
cin >> rec;
niz.push_back(rec);
if(maxsiz<rec.size()){
odabrana=rec;
maxsiz=rec.size();
}
}
for(auto&x:niz)
ubaci(x);
dfs1(0);
rez.push_back(odabrana[0]);
dfs2(trie[0][odabrana[0]-'a'],1,0);
while(rez.back()=='-')
rez.pop_back();
cout << rez.size() << endl;
for(auto&x:rez)
cout << x << endl;
return 0;
}
Compilation message
printer.cpp: In function 'int main()':
printer.cpp:95:18: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
95 | if(maxsiz<rec.size()){
| ~~~~~~^~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
69 ms |
122000 KB |
Output is correct |
2 |
Correct |
60 ms |
121684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
59 ms |
121768 KB |
Output is correct |
2 |
Correct |
60 ms |
121680 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
59 ms |
121684 KB |
Output is correct |
2 |
Correct |
59 ms |
121684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
59 ms |
121676 KB |
Output is correct |
2 |
Correct |
69 ms |
121684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
63 ms |
121692 KB |
Output is correct |
2 |
Correct |
62 ms |
121936 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
62 ms |
121988 KB |
Output is correct |
2 |
Correct |
62 ms |
121936 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
63 ms |
122196 KB |
Output is correct |
2 |
Correct |
68 ms |
122572 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
69 ms |
122828 KB |
Output is correct |
2 |
Correct |
79 ms |
122504 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
93 ms |
124116 KB |
Output is correct |
2 |
Correct |
125 ms |
125920 KB |
Output is correct |
3 |
Correct |
100 ms |
125248 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
84 ms |
124200 KB |
Output is correct |
2 |
Correct |
135 ms |
126556 KB |
Output is correct |
3 |
Correct |
103 ms |
125720 KB |
Output is correct |
4 |
Correct |
136 ms |
126484 KB |
Output is correct |