#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;
int t[sz * 26][26], mx[sz * 26], d[sz * 26];
bool w[sz * 26];
vector<array<int, 3>> g[sz * 26];
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;
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:24: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]
24 | while(ind < s.size())
| ~~~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
15708 KB |
Output is correct |
2 |
Correct |
8 ms |
15708 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
15704 KB |
Output is correct |
2 |
Correct |
6 ms |
15708 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
15708 KB |
Output is correct |
2 |
Correct |
6 ms |
15708 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
15624 KB |
Output is correct |
2 |
Correct |
7 ms |
15704 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
15708 KB |
Output is correct |
2 |
Correct |
15 ms |
16500 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
17244 KB |
Output is correct |
2 |
Correct |
27 ms |
17920 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
61 ms |
22100 KB |
Output is correct |
2 |
Correct |
128 ms |
29312 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
162 ms |
31824 KB |
Output is correct |
2 |
Correct |
59 ms |
19120 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
400 ms |
56656 KB |
Output is correct |
2 |
Correct |
811 ms |
110216 KB |
Output is correct |
3 |
Correct |
449 ms |
64084 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
305 ms |
47716 KB |
Output is correct |
2 |
Correct |
928 ms |
128084 KB |
Output is correct |
3 |
Correct |
503 ms |
70704 KB |
Output is correct |
4 |
Correct |
978 ms |
121980 KB |
Output is correct |