#include <bits/stdc++.h>
using namespace std;
const int MAX_LOG = 20;
const int MAX_SIZE = 15;
struct Node{
char val;
int depth;
vector<Node*> figli;
Node* father[MAX_LOG];
Node(char val, int depth) : val(val), depth(depth){
figli.resize(MAX_SIZE, nullptr);
for(int i=0; i<MAX_LOG; i++)
father[i] = nullptr;
}
Node(char val, int depth, Node* N) : val(val), depth(depth){
figli.resize(MAX_SIZE, nullptr);
father[0] = N;
for(int i=1; i<MAX_LOG; i++)
if(father[i-1] != nullptr)
father[i] = father[i-1]->father[i-1];
else
father[i] = nullptr;
}
};
struct Trie{
Node *root = nullptr;
vector<Node *> version;
int curr = 0;
//costruttore
Trie(){
root = new Node('\0', -1);
version.push_back(root);
}
char getKth(int k){
Node* node = version[curr];
int L = node->depth - k;
for(int i=0; (1<<i)<=L; i++)if(L&(1<<i)){
node = node->father[i];
}
return node->val;
}
//Devo aggiungere nell'ultima versione il carattere(New Node se non esiste) e aggiungere una versione
void insert(char c){
int val = c - 'a';
int h = version[curr]->depth+1;
//Non esiste, lo creo
if(version[curr]->figli[val] == nullptr){
version[curr]->figli[val] = new Node(c, h, version[curr]);
}
//aggiungo la nuova versione
version.push_back(version[curr]->figli[val]);
curr ++;
}
//Per tornare indietro di N versioni mi basta andare all'indice curr - k del vettore
void goBack(int k){
version.push_back(version[curr-k]);
curr++;
}
};
Trie t;
void Init() {}
void TypeLetter(char L) {
t.insert(L);
}
void UndoCommands(int U) {
t.goBack(U);
}
char GetLetter(int P) {
return t.getKth(P);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
256 KB |
Output is correct |
2 |
Runtime error |
3 ms |
504 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
2 ms |
556 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
2 ms |
708 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
3 ms |
972 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
2 ms |
1060 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |