#include <iostream>
#include <vector>
#include <cstring>
using namespace std;
const int L = 26;
struct nod
{
int cnt, sons, marked, endofword;
nod *son[L];
nod()
{
cnt = sons = marked = endofword = 0;
for (int i = 0; i < L; i++)
son[i] = 0;
}
};
int ans = 0;
vector <char> sol;
nod *trie = new nod;
void update(nod *trie, char *s, int val, int lit, int tip)
{
int next;
trie->marked += tip;
if (!lit)
{
trie->endofword = 1;
trie->cnt += val;
return;
}
if (trie->son[*s - 'a'] == 0)
{
trie->son[*s - 'a'] = new nod;
trie->sons++;
}
update((trie->son[*s - 'a']), s + 1, val, lit - 1, tip);
}
int stop = 0;
void dfs(nod *trie)
{
if (trie->endofword)
sol.push_back('P');
if (trie->sons == 0)
{
if (trie->marked == 1)
stop = 1;
return;
}
int dani = -1;
for (int i = 0; i < L; i++)
{
if (trie->son[i] == 0)
continue;
if (trie->son[i]->marked == 1)
dani = i;
else
{
char x = i + 'a';
sol.push_back(x);
/// cout << x << '\n';
dfs(trie->son[i]);
sol.push_back('-');
/// cout << "-" << '\n';
}
}
if (dani == -1)
return;
char x = dani + 'a';
sol.push_back(x);
/// cout << x << '\n';
dfs(trie->son[dani]);
}
/**
11
hjxgqk
ppmqpceairhdont
pwfxlmwfirlgbdevjd
ppgqvqovp
wdmcpctdc
wxcfafyveyuj
vbjxavqcmxzbfiel
xrmqggqby
rhpqtslc
pw
iupqiq
**/
char s[25005][L];
int main()
{
int n, i, maxim = -1;
cin >> n;
ans = n;
trie->cnt = 1;
for (i = 1; i <= n; i++)
{
cin >> s[i];
int aw = strlen(s[i]);
maxim = max(maxim, aw);
}
for (i = 1; i <= n; i++)
{
int op = 0, lit = strlen(s[i]);
if(maxim == lit)
op = 1, maxim = -1;
update(trie, s[i], 1, lit, op);
}
dfs(trie);
cout << sol.size() << '\n';
for (i = 0; i < sol.size(); i++)
cout << sol[i] << '\n';
return 0;
}
Compilation message
printer.cpp: In function 'void update(nod*, char*, int, int, int)':
printer.cpp:23:9: warning: unused variable 'next' [-Wunused-variable]
23 | int next;
| ^~~~
printer.cpp: In function 'int main()':
printer.cpp:109:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
109 | for (i = 0; i < sol.size(); i++)
| ~~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
2 ms |
1100 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
1868 KB |
Output is correct |
2 |
Correct |
5 ms |
2380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
6348 KB |
Output is correct |
2 |
Correct |
24 ms |
13516 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
29 ms |
15816 KB |
Output is correct |
2 |
Correct |
14 ms |
3788 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
70 ms |
39360 KB |
Output is correct |
2 |
Correct |
150 ms |
89924 KB |
Output is correct |
3 |
Correct |
89 ms |
46696 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
58 ms |
31040 KB |
Output is correct |
2 |
Correct |
181 ms |
107028 KB |
Output is correct |
3 |
Correct |
104 ms |
52916 KB |
Output is correct |
4 |
Correct |
163 ms |
101100 KB |
Output is correct |