#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], ch[sz * 26];
bool w[sz * 26];
vector<int> g[sz * 26];
string s;
bool cmp(int a, int b)
{
return mx[a] < mx[b];
}
void add(int ind, int cur)
{
if(ind == s.size())
{
w[cur] = 1;
return;
}
int prv = cur;
if(t[cur][s[ind] - 'a'])
{
cur = t[cur][s[ind] - 'a'];
}
else
{
d[++x] = d[cur] + 1;
mx[x] = d[x];
ch[x] = s[ind];
cur = t[cur][s[ind] - 'a'] = x;
}
add(ind + 1, cur);
mx[prv] = max(mx[prv], mx[cur]);
g[prv].push_back(cur);
}
void calc(int v, char c)
{
cnt++;
cout << c << endl;
if(w[v]) cout << "P" << endl;
for(auto i : g[v])
{
calc(i, ch[i]);
}
if(cnt < x)cout << "-" << endl;
}
void solve()
{
cin >> n;
for(int i = 0; i < n; i++)
{
cin >> s;
add(0, 0);
}
for(int i = 0; i <= x; i++)
{
sort(all(g[i]));
g[i].erase(unique(all(g[i])), g[i].end());
sort(all(g[i]), cmp);
}
cout << x * 2 - mx[0] + n << endl;
for(auto i : g[0])
{
calc(i, ch[i]);
}
}
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:29:12: 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]
29 | if(ind == s.size())
| ~~~~^~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
19036 KB |
Output is correct |
2 |
Correct |
3 ms |
19036 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
19036 KB |
Output is correct |
2 |
Correct |
3 ms |
19036 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
19036 KB |
Output is correct |
2 |
Correct |
3 ms |
19036 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
19032 KB |
Output is correct |
2 |
Correct |
3 ms |
19036 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
19036 KB |
Output is correct |
2 |
Correct |
12 ms |
19804 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
20572 KB |
Output is correct |
2 |
Correct |
21 ms |
21084 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
75 ms |
25176 KB |
Output is correct |
2 |
Correct |
125 ms |
32088 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
150 ms |
34632 KB |
Output is correct |
2 |
Correct |
47 ms |
23640 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
386 ms |
62544 KB |
Output is correct |
2 |
Correct |
747 ms |
113980 KB |
Output is correct |
3 |
Correct |
396 ms |
70996 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
268 ms |
53960 KB |
Output is correct |
2 |
Correct |
973 ms |
132496 KB |
Output is correct |
3 |
Correct |
530 ms |
76492 KB |
Output is correct |
4 |
Correct |
913 ms |
125876 KB |
Output is correct |