#include <iostream>
#include <vector>
#include <string>
using namespace std;
struct Trie {
int nex[26]; // index of next trie for each letter
int prev; // parent
bool end; // end of a word
Trie() {
fill(nex, nex + 26, -1);
prev = -1;
end = false;
}
};
Trie tries[500000];
int N;
char letters[200]; // store letter for each ascii value
int index = 1;
int wc = 0; //word count
void traverse(int curr) {
for (int c = 0; c < 26; c++) { // loop through each letter
if (tries[curr].nex[c] != -1) {
cout << letters[c] << endl;
traverse(tries[curr].nex[c]);
if (wc == N) return;
cout << "-" << endl;
}
}
if (tries[curr].end) {
cout << "P" << endl;
wc++;
}
return;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> N;
for (int i = 0; i < N; i++) {
string str;
cin >> str;
int curr = 0;
for (int j = 0; j < (int)str.length(); j++) {
int c = (int)str[j] - 97;
letters[c] = str[j];
if (tries[curr].nex[c] != -1) { // prefix recorded previously
curr = tries[curr].nex[c];
}
else {
tries[curr].nex[c] = index;
tries[index].prev = curr;
curr = index;
index++;
}
cout << curr << endl;
}
tries[curr].end = true;
cout << endl;
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
21 ms |
55020 KB |
Line "2" doesn't correspond to pattern "[a-z\-P]{1}" |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
21 ms |
54992 KB |
Line "2" doesn't correspond to pattern "[a-z\-P]{1}" |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
22 ms |
55024 KB |
Line "2" doesn't correspond to pattern "[a-z\-P]{1}" |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
21 ms |
55012 KB |
Line "2" doesn't correspond to pattern "[a-z\-P]{1}" |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
23 ms |
55036 KB |
Line "2" doesn't correspond to pattern "[a-z\-P]{1}" |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
31 ms |
55124 KB |
Line "2" doesn't correspond to pattern "[a-z\-P]{1}" |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
60 ms |
55240 KB |
Line "2" doesn't correspond to pattern "[a-z\-P]{1}" |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
138 ms |
55504 KB |
Line "2" doesn't correspond to pattern "[a-z\-P]{1}" |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
288 ms |
56308 KB |
Line "2" doesn't correspond to pattern "[a-z\-P]{1}" |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
271 ms |
56192 KB |
Line "2" doesn't correspond to pattern "[a-z\-P]{1}" |
2 |
Halted |
0 ms |
0 KB |
- |