#include <bits/stdc++.h>
using namespace std;
/**
Initial costul e suma lungimilor.
Punem fiecare string in trie
Cand coboram in trie daca trb sa afisam, afisam
Cand urcam in trie, dam delete
**/
vector<char>ans;
struct node
{
int cnt;
bool term;
node *f[26];
node()
{
cnt = term = 0;
memset(f, 0, sizeof(f));
}
};
struct Trie
{
private:
node *root = new node;
public:
Trie(){}
node *getRoot ()
{
return root;
}
void add (const string &s)
{
node *curr = root;
for (const auto &ch : s)
{
if (curr -> f[ch - 'a'] == NULL)
curr -> f[ch - 'a'] = new node;
curr = curr -> f[ch - 'a'];
curr -> cnt ++;
}
curr -> term = true;
}
void dfs (node *curr)
{
if (curr -> term == true)
ans.push_back('P');
vector<pair<int, int>>v;
for (int i=0; i<26; i++)
{
if (curr -> f[i] != NULL)
v.push_back({curr -> f[i] -> cnt, i});
}
sort(v.begin(), v.end());
for (int i=0; i<(int)v.size(); i++)
{
ans.push_back(char(v[i].second + 'a'));
dfs(curr -> f[v[i].second]);
ans.push_back('-');
}
}
};
int main()
{
int n;
cin >> n;
Trie T;
for (int i=1; i<=n; i++)
{
string s;
cin >> s;
T.add(s);
}
T.dfs(T.getRoot());
while (!ans.empty() && ans.back() == '-')
ans.pop_back();
cout << (int)ans.size() << '\n';
for (const auto &ch : ans)
cout << ch << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
296 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4 ms |
1816 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
12 ms |
5944 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
29 ms |
14776 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
75 ms |
36592 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
55 ms |
28628 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |