#include <iostream>
#include <queue>
#include <string>
#include <algorithm>
#include <vector>
#include <cmath>
#include <iomanip>
#include <map>
#include <cstring>
#include <set>
#include <stack>
#include <bitset>
#define ll long long
#define INF (1e9)
#define MAX (int)(26 * (2e5 + 5))
#define MOD 1000000007
#define par pair<int, int>
#define all(v) v.begin(), v.end()
#define sz(x) (int) ((x).size())
#define esq(x) (x<<1)
#define dir(x) ((x<<1)|1)
#define lsb(x) (x & -x)
#define W(x) cout << #x << ": " << x << endl
#define Wii(x) cout << x.first << ' ' << x.second << endl
#define _ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
using namespace std;
int n, t[MAX], trie[MAX][26], qnt, tot, resp, maxn[MAX];
string s;
void add(string s)
{
int u = 0;
for (char c : s)
{
c -= 'a';
if (!trie[u][c]) trie[u][c] = ++qnt;
u = trie[u][c];
}
t[u] = 1;
}
int a(int u)
{
int nivel = 0;
for (int i = 0; i < 26; i++)
{
if (!trie[u][i]) continue;
if (nivel < 1 + a(trie[u][i]))
{
nivel = 1 + a(trie[u][i]);
maxn[u] = i;
}
}
return nivel;
}
void b(int u)
{
++resp;
if (t[u])
{
++tot;
++resp;
}
for (int i = 0; i < 26; i++)
{
if (!trie[u][i] || maxn[u] == i) continue;
b(trie[u][i]);
if (tot != n)
{
++resp;
}
}
if (trie[u][maxn[u]])
{
b(trie[u][maxn[u]]);
if (tot != n) resp++;
}
}
void solve(int u, char c)
{
if (u) printf("%c\n", c);
if (t[u])
{
printf("P\n");
++tot;
}
for (int i = 0; i < 26; i++)
{
if (!trie[u][i] || maxn[u] == i) continue;
solve(trie[u][i], i+'a');
if (tot != n) printf("-\n");
}
if (trie[u][maxn[u]])
{
solve(trie[u][maxn[u]], maxn[u]+'a');
if (tot != n) printf("-\n");
}
}
int main()
{_
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> s;
add(s);
}
a(0);
b(0);
cout << --resp << endl;
tot = 0;
solve(0, ' ');
}
Compilation message
printer.cpp: In function 'void add(std::string)':
printer.cpp:38:16: warning: array subscript has type 'char' [-Wchar-subscripts]
38 | if (!trie[u][c]) trie[u][c] = ++qnt;
| ^
printer.cpp:38:28: warning: array subscript has type 'char' [-Wchar-subscripts]
38 | if (!trie[u][c]) trie[u][c] = ++qnt;
| ^
printer.cpp:39:15: warning: array subscript has type 'char' [-Wchar-subscripts]
39 | u = trie[u][c];
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
30 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
64 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
332 KB |
Output is correct |
2 |
Correct |
16 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
332 KB |
Output is correct |
2 |
Execution timed out |
1047 ms |
716 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
478 ms |
1092 KB |
Output is correct |
2 |
Correct |
593 ms |
1272 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1090 ms |
3024 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
974 ms |
7584 KB |
Output is correct |
2 |
Execution timed out |
1096 ms |
1740 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1098 ms |
17824 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
224 ms |
14444 KB |
Output is correct |
2 |
Execution timed out |
1098 ms |
47056 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |