//
// Created by adavy on 8/5/2023.
//
//
// Created by adavy on 2/4/2023.
//
//
// Created by adavy on 1/31/2023.
//
// My Header
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using db = double;
using str = string; // yay python!
using ii = pair<int,int>;
using pl = pair<ll,ll>;
using pd = pair<db,db>;
using vi = vector<int>;
using vb = vector<bool>;
using vl = vector<ll>;
using vd = vector<db>;
using vs = vector<str>;
using vii = vector<ii>;
using vpl = vector<pl>;
using vpd = vector<pd>;
#define tcT template<class T
#define tcTU tcT, class U
tcT> using V = vector<T>;
tcT, size_t SZ> using AR = array<T,SZ>;
tcT> using PR = pair<T,T>;
// pairs
#define mp make_pair
#define f first
#define s second
#define FOR(i,a,b) for (int i = (a); i < (b); ++i)
#define F0R(i,a) FOR(i,0,a)
#define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i)
#define R0F(i,a) ROF(i,0,a)
#define trav(a,x) for (auto& a: x)
#define len(x) int((x).size())
#define bg(x) begin(x)
#define all(x) bg(x), end(x)
#define rall(x) rbegin(x), rend(x)
#define sor(x) sort(all(x))
#define rsz resize
#define ins insert
#define ft front()
#define bk back()
#define pb push_back
#define eb emplace_back
#define pf push_front
const int MOD = 1e9+7; // 998244353;
const int MX = 2e5+5;
const ll INF = 1e18; // not too close to LLONG_MAX
const ld PI = acos((ld)-1);
const int dx[4] = {1,0,-1,0}, dy[4] = {0,1,0,-1}; // for every grid problem!!
vector<char> toPrint;
struct Trie{
bool active = 0;
vector<Trie> links;
int numWords=0;
int wordsTot=0;
bool special = 0;
void activate(){
active = 1;
numWords = 0;
wordsTot = 0;
special = 0;
links.rsz(26);
}
void dfs(){
F0R(i, numWords) toPrint.pb('P');
int specialFound = -1;
F0R(i, 26){
if (links[i].active&&links[i].wordsTot>0){
if (links[i].special){
specialFound =i;
continue;
}
toPrint.pb('a'+i);
links[i].dfs();
toPrint.pb('-');
}
}
if (specialFound!=-1){
toPrint.pb('a'+specialFound);
links[specialFound].dfs();
toPrint.pb('-');
}
}
void add_word(string s, bool isSpecial = 0){
special = isSpecial;
wordsTot++;
if (s.size()==0){ numWords++; return;}
int nextChar = s.back()-'a';
if (links[nextChar].active == 0) links[nextChar].activate();
s.pop_back();
links[nextChar].add_word(s,isSpecial);
}
};
int main(){
std::ios::sync_with_stdio(false);
cin.tie(NULL);
int n; cin >> n;
vector<str> words(n);
Trie t;
t.activate();
trav(i, words){ cin >> i; reverse(all(i));}
std::sort(all(words), []
(const std::string& first, const std::string& second){
return first.size() < second.size();
});
F0R(i, words.size()-1) t.add_word(words[i]);
t.add_word(words.back(),1);
t.dfs();
int bs = words.back().size();
cout << toPrint.size()-bs << endl;
//trav(i, toPrint) cout << i << endl;
F0R(i, toPrint.size()-bs) cout << toPrint[i] << endl;
}
Compilation message
printer.cpp: In function 'int main()':
printer.cpp:47:40: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::__cxx11::basic_string<char> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
47 | #define FOR(i,a,b) for (int i = (a); i < (b); ++i)
| ^
printer.cpp:48:18: note: in expansion of macro 'FOR'
48 | #define F0R(i,a) FOR(i,0,a)
| ^~~
printer.cpp:142:5: note: in expansion of macro 'F0R'
142 | F0R(i, words.size()-1) t.add_word(words[i]);
| ^~~
printer.cpp:47:40: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
47 | #define FOR(i,a,b) for (int i = (a); i < (b); ++i)
| ^
printer.cpp:48:18: note: in expansion of macro 'FOR'
48 | #define F0R(i,a) FOR(i,0,a)
| ^~~
printer.cpp:148:5: note: in expansion of macro 'F0R'
148 | F0R(i, toPrint.size()-bs) cout << toPrint[i] << endl;
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
444 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
444 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
980 KB |
Output is correct |
2 |
Correct |
10 ms |
4820 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
8668 KB |
Output is correct |
2 |
Correct |
25 ms |
10964 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
76 ms |
31276 KB |
Output is correct |
2 |
Correct |
162 ms |
67348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
202 ms |
79500 KB |
Output is correct |
2 |
Correct |
51 ms |
16844 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
459 ms |
200160 KB |
Output is correct |
2 |
Runtime error |
106 ms |
262144 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
477 ms |
155928 KB |
Output is correct |
2 |
Runtime error |
106 ms |
262144 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |