# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
639516 | tade | Crayfish scrivener (IOI12_scrivener) | C++17 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define int long long
using namespace std;
struct trie_node
{
int child[26];
char letter;
int depth;
trie_node()
{
memset(child, -1, sizeof(child));
depth = 0;
}
};
int st[100005][22];
vector<trie_node> trie;
vector<int> cmds;
int node = 0;
void Init()
{
trie.push_back(trie_node());
trie[0].letter = '#';
trie[0].depth = -1;
cmds.push_back(0);
}
void TypeLetter(char L)
{
int nxt = L - 'a';
if(trie[node].child[nxt] == -1)
{
trie[node].child[nxt] = trie.size();
trie.push_back(trie_node());
}
int parent = node;
node = trie[node].child[nxt];
trie[node].letter = L;
trie[node].depth = trie[parent].depth + 1;
st[node][0] = parent;
for(int i = 1; i < 22; ++ i)
st[node][i] = st[st[node][i - 1]][i - 1];
cmds.push_back(node);
}
void UndoCommands(int U)
{
node = cmds[cmds.size() - U - 1];
cmds.push_back(node);
}
char GetLetter(int P)
{
int jump_dist = trie[node].depth - P;
int jump_node = node;
for(int i = 0; i < 22 && jump_dist > 0; ++ i)
{
if(jump_dist & 1) jump_node = st[jump_node][i];
jump_dist >>= 1;
}
return trie[jump_node].letter;
}