#include <bits/stdc++.h>
// author : a1abay
using namespace std;
#define int ll
#define all(v) v.begin(), v.end()
typedef long long ll;
typedef long double ld;
const int inf = 1e9 + 7;
const int inff = (int)1e18 + 7;
const int sz = 25e3 + 5;
int n, k, x, cnt;
vector<vector<int>> t;
vector<int> mx, d;
vector<bool> w;
vector<vector<array<int, 3>>> g;
string s;
void add(int ind, int cur)
{
while(ind < s.size())
{
if(t[cur][s[ind] - 'a']) cur = t[cur][s[ind] - 'a'];
else cur = t[cur][s[ind] - 'a'] = ++x;
ind++;
}
w[cur] = 1;
}
void dfs(int v)
{
bool lf = 1;
for(int i = 0; i < 26; i++)
{
if(!t[v][i]) continue;
lf = 0;
int c = t[v][i];
d[c] = d[v] + 1;
dfs(c);
mx[v] = max(mx[v], mx[c]);
g[v].push_back({mx[c], c, i});
}
sort(all(g[v]));
if(lf) mx[v] = d[v];
}
void calc(int v, char c)
{
cnt++;
cout << c << endl;
if(w[v]) cout << "P" << endl;
for(auto i : g[v])
{
calc(i[1], char(i[2] + 'a'));
}
if(cnt < x)cout << "-" << endl;
}
void solve()
{
cin >> n;
t.resize(n * 26, vector<int>(26, 0));
mx.resize(n * 26, 0);
w.resize(n * 26, 0);
d.resize(n * 26, 0);
g.resize(n * 26);
for(int i = 0; i < n; i++)
{
cin >> s;
add(0, 0);
}
dfs(0);
cout << x * 2 - mx[0] + n << endl;
for(auto i : g[0])
{
calc(i[1], char(i[2] + 'a'));
}
}
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t = 1;
// cin >> t;
while(t--)
{
solve();
}
}
Compilation message
printer.cpp: In function 'void add(ll, ll)':
printer.cpp:25:15: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
25 | while(ind < s.size())
| ~~~~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
1116 KB |
Output is correct |
2 |
Correct |
9 ms |
3356 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
6488 KB |
Output is correct |
2 |
Correct |
29 ms |
8084 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
68 ms |
23132 KB |
Output is correct |
2 |
Correct |
136 ms |
46164 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
175 ms |
68688 KB |
Output is correct |
2 |
Correct |
78 ms |
88908 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
443 ms |
137888 KB |
Output is correct |
2 |
Correct |
829 ms |
167248 KB |
Output is correct |
3 |
Correct |
553 ms |
168276 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
390 ms |
188188 KB |
Output is correct |
2 |
Execution timed out |
1018 ms |
198868 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |