#include <bits/stdc++.h>
using namespace std;
/*<DEBUG>*/
#define tem template <typename
#define can_shift(_X_, ...) enable_if_t<sizeof test<_X_>(0) __VA_ARGS__ 8, debug&> operator<<(T i)
#define _op debug& operator<<
tem C > auto test(C *x) -> decltype(cerr << *x, 0LL);
tem C > char test(...);
tem C > struct itr{C begin, end; };
tem C > itr<C> get_range(C b, C e) { return itr<C>{b, e}; }
struct debug{
#ifdef _LOCAL
~debug(){ cerr << endl; }
tem T > can_shift(T, ==){ cerr << boolalpha << i; return *this; }
tem T> can_shift(T, !=){ return *this << get_range(begin(i), end(i)); }
tem T, typename U > _op (pair<T, U> i){
return *this << "< " << i.first << " , " << i.second << " >"; }
tem T> _op (itr<T> i){
*this << "{ ";
for(auto it = i.begin; it != i.end; it++){
*this << " , " + (it==i.begin?2:0) << *it;
}
return *this << " }";
}
#else
tem T> _op (const T&) { return *this; }
#endif
};
tem T>
string _ARR_(T* arr, int sz){
string ret = "{ " + to_string(arr[0]);
for(int i = 1; i < sz; i++) ret += " , " + to_string(arr[i]);
ret += " }"; return ret;
}
#define exp(...) " [ " << #__VA_ARGS__ << " : " << (__VA_ARGS__) << " ]"
/*</DEBUG>*/
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef unsigned int uint;
typedef pair<int, int> pii;
//mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
#define pb push_back
#define FAST ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define TC int __TC__; cin >> __TC__; while(__TC__--)
#define ar array
const int INF = 1e9 + 7, N = 1e6;
int trie[N][26], p=0, last[N], n, sum=0, d[N];
string s,ans;
void insert(int i, int rt){
if(i == s.size()){
++last[rt];
return;
}
if(!trie[rt][s[i]-'a']) trie[rt][s[i]-'a'] = ++p;
insert(i + 1, trie[rt][s[i]-'a']);
}
void dfs1(int u){
bool leaf = 1;
for(int i = 0; i < 26; ++i){
int v = trie[u][i];
if(v){
leaf=0;
dfs1(v);
d[u] = max(d[u], d[v]+1);
}
}
if(leaf) d[u] = 1;
}
void dfs(int u){
for(int i = 0; i < last[u]; ++i){
ans += 'P';
}
sum -= last[u];
if(sum==0){
cout << ans.size() <<'\n';
for(char c : ans){
cout << c << '\n';
}
exit(0);
}
int deepest = 0;
for(int i = 0; i < 26; ++i){
if(trie[u][i] && d[trie[u][i]] > d[trie[u][deepest]]){
deepest = i;
}
}
debug() << exp(u) exp(trie[u][deepest]) exp((char)('a'+deepest));
for(int i= 0; i < 26; ++i){
if(trie[u][i] && i != deepest){
debug() << exp(u) exp(i);
ans += (char)('a'+i);
dfs(trie[u][i]);
ans+='-';
}
}
if(trie[u][deepest]){
ans+=(char)('a'+deepest);
dfs(trie[u][deepest]);
ans+='-';
}
}
int main()
{
FAST;
cin >> n;
for(int i = 0; i < n; ++i){
cin >> s;
insert(0, 0);
}
sum = n;
dfs1(0);
d[0] = -INF;
dfs(0);
return 0;
}
Compilation message
printer.cpp: In function 'void insert(int, int)':
printer.cpp:58:7: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
58 | if(i == s.size()){
| ~~^~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
2 ms |
716 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
1100 KB |
Output is correct |
2 |
Correct |
4 ms |
1356 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
3188 KB |
Output is correct |
2 |
Correct |
21 ms |
6604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
26 ms |
7772 KB |
Output is correct |
2 |
Correct |
9 ms |
1868 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
66 ms |
18900 KB |
Output is correct |
2 |
Correct |
142 ms |
43004 KB |
Output is correct |
3 |
Correct |
77 ms |
22232 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
54 ms |
14860 KB |
Output is correct |
2 |
Correct |
173 ms |
51044 KB |
Output is correct |
3 |
Correct |
86 ms |
25176 KB |
Output is correct |
4 |
Correct |
146 ms |
48248 KB |
Output is correct |