#include <bits/stdc++.h>
#define fu(i, a, b) for (int i = a; i <= b; i++)
#define fd(i, a, b) for (int i = a; i >= b; i--)
using namespace std;
const int N = 25e3 + 10;
const int M = 5e5 + 10;
int n;
string a[N], res;
int trie[M][27], sz = 0;
bool tail[M];
string longest;
void Insert(const string &a)
{
int node = 0;
for (char c : a)
{
int x = int(c) - 96;
if (!trie[node][x]) trie[node][x] = ++sz;
node = trie[node][x];
}
tail[node] = true;
}
void DFS(int u, int depth, bool still)
{
int c = int(longest[depth]) - 96;
// cout << u << " " << depth << "\n";
fu(i, 1, 26)
{
if (trie[u][i] == 0) continue;
if (i == c && still) continue;
// cout << char(i + 96) << "\n";
res.push_back(char(i + 96));
if (tail[trie[u][i]])
{
// cout << "P\n";
res.push_back('P');
}
DFS(trie[u][i], depth + 1, false);
// cout << "-\n";
res.push_back('-');
}
if (still)
{
// cout << longest[depth] << "\n";
res.push_back(longest[depth]);
if (depth < (int)longest.size() - 1 && trie[u][c]) DFS(trie[u][c], depth + 1, still); else /*cout << "P";*/ res.push_back('P');
}
}
int main()
{
cin.tie(NULL);
cout.tie(NULL);
ios_base::sync_with_stdio(false);
cin >> n;
fu(i, 1, n)
{
cin >> a[i];
}
fu(i, 1, n)
{
Insert(a[i]);
}
longest = a[1];
fu(i, 2, n)
{
if (longest.size() < a[i].size()) longest = a[i];
}
DFS(0, 0, 1);
cout << (int)res.size() << "\n";
fu(i, 0, (int)res.size() - 1)
{
cout << res[i] << "\n";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1104 KB |
Output is correct |
2 |
Correct |
1 ms |
1100 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1100 KB |
Output is correct |
2 |
Correct |
1 ms |
1100 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
1100 KB |
didn't print every word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1100 KB |
Output is correct |
2 |
Incorrect |
1 ms |
1100 KB |
didn't print every word |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
1100 KB |
didn't print every word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
1868 KB |
didn't print every word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
8 ms |
4000 KB |
didn't print every word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
22 ms |
8396 KB |
didn't print every word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
50 ms |
19412 KB |
didn't print every word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
41 ms |
15204 KB |
didn't print every word |
2 |
Halted |
0 ms |
0 KB |
- |