#include <iostream>
using namespace std;
int aux, p = 0;
char sol[1000000];
string s, lw;
struct trie
{
int dp, printez, sterg, printezlit;
trie* fii[27];
trie()
{
dp = printez = sterg = printezlit = 0;
for (aux = 0; aux <= 26; aux++)
fii[aux] = 0;
};
};
trie* t = new trie;
void add (trie* x, int poz, int pmax)
{
if (poz <= pmax)
{
x->printezlit = 1, x->sterg = 1;
int ch = s[poz] - 'a';
if (x->fii[ch] == 0)
x->fii[ch] = new trie;
add (x->fii[ch], poz + 1, pmax);
}
else
x->printez++, x->sterg = 1, x->printezlit = 1;
}
void dfs (trie* x, int poz, int pmax, int ok, char lit)
{
int i, xp = x->printez;
sol[++p] = lit;
x->dp = x->printez + x->sterg + x->printezlit;
while (xp--)
sol[++p] = 'P';
for (i = 0; i <= 26; i++)
if (x->fii[i] != 0 && (i + 'a' != lw[poz] || poz > pmax))
dfs (x->fii[i], poz, pmax, 0, i + 'a'), x->dp += x->fii[i]->dp;
if (ok == 1 && poz <= pmax)
dfs (x->fii[lw[poz] - 'a'], poz + 1, pmax, 1, lw[poz]), x->dp += x->fii[lw[poz] - 'a']->dp;
xp = x->sterg;
while (xp--)
sol[++p] = '-';
}
int main()
{
int n, i, lm = 0;
cin >> n;
for (i = 1; i <= n; i++)
{
cin >> s;
add (t, 0, s.size() - 1);
if (lm < s.size())
lm = s.size(), lw = s;
}
dfs (t, 0, lw.size() - 1, 1, ' ');
cout << (t->dp) - lm - 2 << '\n';
while (sol[p] == '-')
p--;
for (i = 2; i <= p; i++)
cout << sol[i] << '\n';
return 0;
}
Compilation message
printer.cpp: In function 'int main()':
printer.cpp:58:16: 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 (lm < s.size())
| ~~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
didn't print every word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
didn't print every word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
308 KB |
didn't print every word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
340 KB |
didn't print every word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
1848 KB |
didn't print every word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
10 ms |
6228 KB |
didn't print every word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
28 ms |
15604 KB |
didn't print every word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
60 ms |
38768 KB |
didn't print every word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
48 ms |
30252 KB |
didn't print every word |
2 |
Halted |
0 ms |
0 KB |
- |