#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;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
296 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
296 KB |
Output is correct |
2 |
Incorrect |
1 ms |
292 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
300 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
1748 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
10 ms |
5864 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
25 ms |
14664 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
64 ms |
36684 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
57 ms |
28640 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |