#include <bits/stdc++.h>
#define forn(i, n) for (int i = 0; i < int(n); i++)
#define forsn(i, s, n) for (int i = int(s); i < int(n); i++)
#define all(x) x.begin(), x.end()
#define LSB(x) x & -x
typedef long long ll;
using namespace std;
/**
Trie
traverse in sorted order
**/
int trie[500020][27], d[500020], nxt = 1, rta = 0;
bool isWord[500020], dont[500020];
string longest = "", ans = "";
void insert(string &s)
{
int i = 0, v = 0;
while (i < s.size())
{
if (trie[v][(s[i] - 'a')] == -1)
{
trie[v][(s[i] - 'a')] = nxt++;
}
v = trie[v][(s[i++] - 'a')];
}
isWord[v] = true;
}
void depths(int node, int depth)
{
d[node] = depth;
forn(i, 26)
{
if (trie[node][i] != -1)
{
depths(trie[node][i], depth + 1);
}
}
}
void dfs(int node)
{
int d_it = -1;
forn(i, 26)
{
if (trie[node][i] != -1)
{
if (!dont[trie[node][i]])
{
rta++;
ans += (char)(i + 'a');
if (isWord[trie[node][i]])
{
ans += 'P';
rta++;
}
dfs(trie[node][i]);
rta++;
ans += '-';
}
else
d_it = i;
}
}
if (d_it != -1)
{
rta++;
ans += (char)(d_it + 'a');
if (isWord[trie[node][d_it]])
{
ans += 'P';
rta++;
}
dfs(trie[node][d_it]);
}
}
int main()
{
iostream::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin >> n;
memset(trie, -1, sizeof(trie));
memset(isWord, false, sizeof(isWord));
memset(dont, false, sizeof(dont));
forn(i, n)
{
string s;
cin >> s;
insert(s);
if (s.size() > longest.size())
{
longest = s;
}
}
int v = 0;
forn(i, longest.size())
{
v = trie[v][longest[i] - 'a'];
dont[v] = true;
}
// declare depths
depths(0, 0);
// dfs
dfs(0);
// display
cout << rta;
for (auto x : ans)
cout << x << "\n";
return 0;
}
/*
*/
Compilation message
printer.cpp: In function 'void insert(std::string&)':
printer.cpp:21:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
21 | while (i < s.size())
| ~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
20 ms |
54120 KB |
Expected integer, but "20t" found |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
20 ms |
54092 KB |
Expected integer, but "142e" found |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
22 ms |
54092 KB |
Expected integer, but "201h" found |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
20 ms |
54100 KB |
Expected integer, but "290b" found |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
21 ms |
54088 KB |
Expected integer, but "1193a" found |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
29 ms |
54280 KB |
Expected integer, but "14145a" found |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
26 ms |
54592 KB |
Expected integer, but "52541a" found |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
38 ms |
54920 KB |
Expected integer, but "135477a" found |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
62 ms |
56032 KB |
Expected integer, but "337528a" found |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
53 ms |
55784 KB |
Expected integer, but "273585a" found |
2 |
Halted |
0 ms |
0 KB |
- |