#include <bits/stdc++.h>
using namespace std;
#define ld long double
#define ll long long
//#define int ll
#define FF first.first
#define FS first.second
#define SF second.first
#define SS second.second
#define PB push_back
#define MP make_pair
#define all(cont) cont.begin(),cont.end()
#define rall(cont) cont.rbegin(), cont.rend()
#define FOR(i, j) for(int i=0;i<j;i++)
#define RFOR(i, j) for (int i=j;i>=0;i--)
#define GO cin.tie(NULL);
#define FAST ios_base::sync_with_stdio(false);
#define prec(x) cout << fixed << setprecision(x)
#define sz(x) (int)x.size()
typedef pair<int,int> pii;
typedef vector<int> VI;
typedef vector<pii> VPII;
typedef vector<VI> VVI;
typedef priority_queue<int> max_heap;
typedef priority_queue<pii> max_heapii;
typedef priority_queue<int,VI,greater<int>> min_heap;
typedef priority_queue<pii,VPII,greater<pii>> min_heapii;
const long double PI = 3.14159265359;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
ll randint(ll a, ll b){return uniform_int_distribution<ll>(a, b)(rng);}
#define deb(...) logger(#__VA_ARGS__, __VA_ARGS__)
template<typename ...Args>
void logger(string vars, Args&&... values) {
cout << vars << " = ";
string delim = "";
(..., (cout << delim << values, delim = ", "));
cout<<endl;
}
void print(vector<int>v){
cout<<"[";
FOR(i,v.size()){
cout<<v[i];
if(i+1!=v.size())cout<<", ";
}
cout<<"]"<<endl;
}
void print(pii p){
cout<<"{"<<p.first<<", "<<p.second<<"}"<<endl;
}
string sm;
//You will have to augment this in most of the problems
struct Node{
map<char,pair<int,int>> nxt;
};
struct Trie{
vector<Node> nodes;
Trie(){
nodes.push_back(Node());
}
void add(string &s,int i,int ind,int p){
if (i==s.length()) return;
if (nodes[ind].nxt.find(s[i])==nodes[ind].nxt.end()){
nodes[ind].nxt[s[i]]={nodes.size(),0};
nodes.push_back(Node());
}
nodes[ind].nxt[s[i]]={nodes[ind].nxt[s[i]].first,nodes[ind].nxt[s[i]].second | (1<<p)};
add(s,i+1,nodes[ind].nxt[s[i]].first,p);
}
void add(string &s,int quem){
s+='$';
add(s,0,0,quem);
}
};
Trie T;
vector<char> ans;
void dfs(int u,char letra,bool tonopath,int dd){
if (T.nodes[u].nxt.size()==0){
ans.PB('P');
return;
}
if (u!=0){
ans.PB(letra);
}
if (tonopath){
for(auto k:T.nodes[u].nxt){
if (k.first!=sm[dd])
dfs(k.second.first,k.first,0,dd+1);
}
dfs(T.nodes[u].nxt[sm[dd]].first,sm[dd],1,dd+1);
}
else{
for(auto k:T.nodes[u].nxt){
dfs(k.second.first,k.first,0,0);
}
}
ans.PB('-');
}
signed main(){
int n;cin>>n;
int maior=0;
FOR(i,n){
string s;cin>>s;
T.add(s,0);
if (sz(s)>maior){
maior=sz(s);
sm=s;
}
}
dfs(0,'a',1,0);
while(ans.back()=='-')ans.pop_back();
cout<<sz(ans)<<'\n';
for(auto k:ans){
cout<<k<<"\n";
}
}
Compilation message
printer.cpp: In function 'void print(std::vector<int>)':
printer.cpp:16:32: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
16 | #define FOR(i, j) for(int i=0;i<j;i++)
......
47 | FOR(i,v.size()){
| ~~~~~~~~~~
printer.cpp:47:2: note: in expansion of macro 'FOR'
47 | FOR(i,v.size()){
| ^~~
printer.cpp:49:9: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
49 | if(i+1!=v.size())cout<<", ";
| ~~~^~~~~~~~~~
printer.cpp: In member function 'void Trie::add(std::string&, int, int, int)':
printer.cpp:71:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
71 | if (i==s.length()) return;
| ~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
208 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
208 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
208 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
208 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
2 ms |
720 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1160 KB |
Output is correct |
2 |
Correct |
4 ms |
1516 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
3552 KB |
Output is correct |
2 |
Correct |
22 ms |
7288 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
30 ms |
10568 KB |
Output is correct |
2 |
Correct |
15 ms |
3332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
68 ms |
21288 KB |
Output is correct |
2 |
Correct |
135 ms |
46524 KB |
Output is correct |
3 |
Correct |
84 ms |
25332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
65 ms |
20712 KB |
Output is correct |
2 |
Correct |
165 ms |
55228 KB |
Output is correct |
3 |
Correct |
108 ms |
28656 KB |
Output is correct |
4 |
Correct |
128 ms |
52364 KB |
Output is correct |