#codingexercise
Find the longest repeated substring in a string.
We build a suffix tree to keep track of substrings encountered so far
int suffixesleft = 0;
int leafEnd = -1;
lastNewNode = null;
void extendSuffixTree(int pos)
{
leafEnd = pos;
suffixesleft ++;
lastNewNode = null;
while (suffixesleft > 0){
if (activeLength == 0)
activeLength = pos;
if (activeNode.children[text[activeEdge]] == NULL)
activeNode.children[text[activeEdge]] = newNode(pos, &leafEnd);
if (lastNewNode != null){
lastNewNode.suffixLink = activeNode;
lastNewNode = null;
}
}
else{
var next = activeNode.children[text[activeEdge]];
if (walkDown(next)) continue;
if (text[next.start + activeLength] == text[pos])
{
if (lastNewNode != null && activeNode != root)
{
lastNewNode.suffixLink = activeNode;
lastNewNode = null;
}
activeLength++;
break;
}
var splitEnd = next.start + activeLength -1;
splitEnd = next.start + activeLength -1;
var split = newNode(next.start, splitEnd);
activeNode.children[text[activeEdge]] = split;
split.children[text[pos]] = newNode(pos, ref leafEnd);
next.start += activeLength;
split.children[text[next.start]] = next;
if (lastNewNode != null){
lastNewNode.suffixLink = split;
}
lastNewNode = split;
}
suffixesleft--;
if (activeNode == root && activeLength > 0)
{
activeLength--;
activeEdge = pos - remainingSuffixCount + 1;
}else if (activeNode != root){
activeNode = activeNode.suffixLink;
}
}
}
Build a binary search tree from postorder traversal
node treebuilder(List<int> post, ref int index, int key, int min, int max, int size)
{
if (index < 0)
return null;
node root = null;
if (key > min && key < max)
{
root = newNode(key);
index = index - 1;
if (index > 0)
{
root.right = treebuilder(post, index, post[index], key, max, size);
root.left = treebuilder(post, index, post[index], min, key, size);
}
}
return root;
}
courtesy : geeksforgeeks
Find the longest repeated substring in a string.
We build a suffix tree to keep track of substrings encountered so far
int suffixesleft = 0;
int leafEnd = -1;
lastNewNode = null;
void extendSuffixTree(int pos)
{
leafEnd = pos;
suffixesleft ++;
lastNewNode = null;
while (suffixesleft > 0){
if (activeLength == 0)
activeLength = pos;
if (activeNode.children[text[activeEdge]] == NULL)
activeNode.children[text[activeEdge]] = newNode(pos, &leafEnd);
if (lastNewNode != null){
lastNewNode.suffixLink = activeNode;
lastNewNode = null;
}
}
else{
var next = activeNode.children[text[activeEdge]];
if (walkDown(next)) continue;
if (text[next.start + activeLength] == text[pos])
{
if (lastNewNode != null && activeNode != root)
{
lastNewNode.suffixLink = activeNode;
lastNewNode = null;
}
activeLength++;
break;
}
var splitEnd = next.start + activeLength -1;
splitEnd = next.start + activeLength -1;
var split = newNode(next.start, splitEnd);
activeNode.children[text[activeEdge]] = split;
split.children[text[pos]] = newNode(pos, ref leafEnd);
next.start += activeLength;
split.children[text[next.start]] = next;
if (lastNewNode != null){
lastNewNode.suffixLink = split;
}
lastNewNode = split;
}
suffixesleft--;
if (activeNode == root && activeLength > 0)
{
activeLength--;
activeEdge = pos - remainingSuffixCount + 1;
}else if (activeNode != root){
activeNode = activeNode.suffixLink;
}
}
}
Build a binary search tree from postorder traversal
node treebuilder(List<int> post, ref int index, int key, int min, int max, int size)
{
if (index < 0)
return null;
node root = null;
if (key > min && key < max)
{
root = newNode(key);
index = index - 1;
if (index > 0)
{
root.right = treebuilder(post, index, post[index], key, max, size);
root.left = treebuilder(post, index, post[index], min, key, size);
}
}
return root;
}
courtesy : geeksforgeeks
No comments:
Post a Comment