后缀树应用程序5–最长公共子字符串

给定两个字符串X和Y,找到 最长公共子串 关于X和Y。 天真的[O(N*M] 2. )]动态规划[O(N*M)]方法已经讨论过 在这里 . 在本文中,我们将讨论使用后缀树(5 th 后缀树应用程序)。 在这里,我们将为两个字符串X和Y构建通用后缀树,如前所述: 广义后缀树1 让我们以我们在中看到的相同示例(X=xabxa,Y=babxba)为例 广义后缀树1 . 我们为X和Y构建了以下后缀树:

null

Longest Common Substring

这是xabxa#babxba的广义后缀树$ 在上面的例子中,[0,4]中带有后缀索引的叶子是字符串xabxa的后缀,[6,11]中带有后缀索引的叶子是字符串babxa的后缀。为什么? 因为在串联字符串xabxa#babxba$中,字符串xabxa的索引是0,长度是5,所以它的后缀的索引是0、1、2、3和4。类似地,字符串babxba的索引是6,它的长度是6,所以它的后缀的索引是6,7,8,9,10和11。 有了这个,我们可以看到,在上面的广义后缀树图中,有一些内部节点的叶子在它下面

  • 字符串X和Y(即[0,4]中至少有一个叶具有后缀索引,[6,11]中至少有一个叶具有后缀索引)
  • 仅限字符串X(即所有叶节点在[0,4]中都有后缀索引)
  • 仅字符串Y(即所有叶节点在[6,11]中都有后缀索引)

下图显示了标记为“XY”、“X”或“Y”的内部节点,具体取决于叶子所属的字符串,它们位于叶子下面。

Longest Common Substring

这些“XY”、“X”或“Y”标记是什么意思? 从根到内部节点的路径标签给出了X或Y或两者的子字符串。 对于标记为XY的节点,从根到该节点的子字符串同时属于字符串X和Y。 对于标记为X的节点,从根到该节点的子字符串仅属于字符串X。 对于标记为Y的节点,从根到该节点的子字符串仅属于字符串Y。 通过看上图,你能看到如何得到X和Y的LCS吗? 到目前为止,至少应该清楚如何得到X和Y的公共子串。 如果我们遍历从根到标记为XY的节点的路径,我们将得到X和Y的公共子串。 现在我们需要在所有常见的子串中找到最长的一个。 你能想想现在怎么拿到LCS吗?还记得我们是怎么做到的吗 最长重复子串 已经在给定字符串中使用后缀树了。 从根节点到标记为XY的最深节点的路径标签将给出X和Y的LCS。最深节点在上图中高亮显示,从根节点到该节点的路径标签“abx”是X和Y的LCS。

C

// A C program to implement Ukkonen's Suffix Tree Construction
// Here we build generalized suffix tree for two strings
// And then we find longest common substring of the two input strings
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX_CHAR 256
struct SuffixTreeNode {
struct SuffixTreeNode *children[MAX_CHAR];
//pointer to other node via suffix link
struct SuffixTreeNode *suffixLink;
/*(start, end) interval specifies the edge, by which the
node is connected to its parent node. Each edge will
connect two nodes,  one parent and one child, and
(start, end) interval of a given edge  will be stored
in the child node. Lets say there are two nods A and B
connected by an edge with indices (5, 8) then this
indices (5, 8) will be stored in node B. */
int start;
int *end;
/*for leaf nodes, it stores the index of suffix for
the path  from root to leaf*/
int suffixIndex;
};
typedef struct SuffixTreeNode Node;
char text[100]; //Input string
Node *root = NULL; //Pointer to root node
/*lastNewNode will point to newly created internal node,
waiting for it's suffix link to be set, which might get
a new suffix link (other than root) in next extension of
same phase. lastNewNode will be set to NULL when last
newly created internal node (if there is any) got it's
suffix link reset to new internal node created in next
extension of same phase. */
Node *lastNewNode = NULL;
Node *activeNode = NULL;
/*activeEdge is represented as input string character
index (not the character itself)*/
int activeEdge = -1;
int activeLength = 0;
// remainingSuffixCount tells how many suffixes yet to
// be added in tree
int remainingSuffixCount = 0;
int leafEnd = -1;
int *rootEnd = NULL;
int *splitEnd = NULL;
int size = -1; //Length of input string
int size1 = 0; //Size of 1st string
Node *newNode( int start, int *end)
{
Node *node =(Node*) malloc ( sizeof (Node));
int i;
for (i = 0; i < MAX_CHAR; i++)
node->children[i] = NULL;
/*For root node, suffixLink will be set to NULL
For internal nodes, suffixLink will be set to root
by default in  current extension and may change in
next extension*/
node->suffixLink = root;
node->start = start;
node->end = end;
/*suffixIndex will be set to -1 by default and
actual suffix index will be set later for leaves
at the end of all phases*/
node->suffixIndex = -1;
return node;
}
int edgeLength(Node *n) {
if (n == root)
return 0;
return *(n->end) - (n->start) + 1;
}
int walkDown(Node *currNode)
{
/*activePoint change for walk down (APCFWD) using
Skip/Count Trick  (Trick 1). If activeLength is greater
than current edge length, set next  internal node as
activeNode and adjust activeEdge and activeLength
accordingly to represent same activePoint*/
if (activeLength >= edgeLength(currNode))
{
activeEdge += edgeLength(currNode);
activeLength -= edgeLength(currNode);
activeNode = currNode;
return 1;
}
return 0;
}
void extendSuffixTree( int pos)
{
/*Extension Rule 1, this takes care of extending all
leaves created so far in tree*/
leafEnd = pos;
/*Increment remainingSuffixCount indicating that a
new suffix added to the list of suffixes yet to be
added in tree*/
remainingSuffixCount++;
/*set lastNewNode to NULL while starting a new phase,
indicating there is no internal node waiting for
it's suffix link reset in current phase*/
lastNewNode = NULL;
//Add all suffixes (yet to be added) one by one in tree
while (remainingSuffixCount > 0) {
if (activeLength == 0)
activeEdge = pos; //APCFALZ
// There is no outgoing edge starting with
// activeEdge from activeNode
if (activeNode->children] == NULL)
{
//Extension Rule 2 (A new leaf edge gets created)
activeNode->children] =
newNode(pos, &leafEnd);
/*A new leaf edge is created in above line starting
from  an existing node (the current activeNode), and
if there is any internal node waiting for it's suffix
link get reset, point the suffix link from that last
internal node to current activeNode. Then set lastNewNode
to NULL indicating no more node waiting for suffix link
reset.*/
if (lastNewNode != NULL)
{
lastNewNode->suffixLink = activeNode;
lastNewNode = NULL;
}
}
// There is an outgoing edge starting with activeEdge
// from activeNode
else
{
// Get the next node at the end of edge starting
// with activeEdge
Node *next = activeNode->children];
if (walkDown(next)) //Do walkdown
{
//Start from next node (the new activeNode)
continue ;
}
/*Extension Rule 3 (current character being processed
is already on the edge)*/
if (text[next->start + activeLength] == text[pos])
{
//If a newly created node waiting for it's
//suffix link to be set, then set suffix link
//of that waiting node to current active node
if (lastNewNode != NULL && activeNode != root)
{
lastNewNode->suffixLink = activeNode;
lastNewNode = NULL;
}
//APCFER3
activeLength++;
/*STOP all further processing in this phase
and move on to next phase*/
break ;
}
/*We will be here when activePoint is in middle of
the edge being traversed and current character
being processed is not  on the edge (we fall off
the tree). In this case, we add a new internal node
and a new leaf edge going out of that new node. This
is Extension Rule 2, where a new leaf edge and a new
internal node get created*/
splitEnd = ( int *) malloc ( sizeof ( int ));
*splitEnd = next->start + activeLength - 1;
//New internal node
Node *split = newNode(next->start, splitEnd);
activeNode->children] = split;
//New leaf coming out of new internal node
split->children] = newNode(pos, &leafEnd);
next->start += activeLength;
split->children] = next;
/*We got a new internal node here. If there is any
internal node created in last extensions of same
phase which is still waiting for it's suffix link
reset, do it now.*/
if (lastNewNode != NULL)
{
/*suffixLink of lastNewNode points to current newly
created internal node*/
lastNewNode->suffixLink = split;
}
/*Make the current newly created internal node waiting
for it's suffix link reset (which is pointing to root
at present). If we come across any other internal node
(existing or newly created) in next extension of same
phase, when a new leaf edge gets added (i.e. when
Extension Rule 2 applies is any of the next extension
of same phase) at that point, suffixLink of this node
will point to that internal node.*/
lastNewNode = split;
}
/* One suffix got added in tree, decrement the count of
suffixes yet to be added.*/
remainingSuffixCount--;
if (activeNode == root && activeLength > 0) //APCFER2C1
{
activeLength--;
activeEdge = pos - remainingSuffixCount + 1;
}
else if (activeNode != root) //APCFER2C2
{
activeNode = activeNode->suffixLink;
}
}
}
void print( int i, int j)
{
int k;
for (k=i; k<=j && text[k] != '#' ; k++)
printf ( "%c" , text[k]);
if (k<=j)
printf ( "#" );
}
//Print the suffix tree as well along with setting suffix index
//So tree will be printed in DFS manner
//Each edge along with it's suffix index will be printed
void setSuffixIndexByDFS(Node *n, int labelHeight)
{
if (n == NULL) return ;
if (n->start != -1) //A non-root node
{
//Print the label on edge from parent to current node
//Uncomment below line to print suffix tree
//print(n->start, *(n->end));
}
int leaf = 1;
int i;
for (i = 0; i < MAX_CHAR; i++)
{
if (n->children[i] != NULL)
{
//Uncomment below two lines to print suffix index
//   if (leaf == 1 && n->start != -1)
//     printf(" [%d]", n->suffixIndex);
//Current node is not a leaf as it has outgoing
//edges from it.
leaf = 0;
setSuffixIndexByDFS(n->children[i], labelHeight +
edgeLength(n->children[i]));
}
}
if (leaf == 1)
{
for (i= n->start; i<= *(n->end); i++)
{
if (text[i] == '#' )
{
n->end = ( int *) malloc ( sizeof ( int ));
*(n->end) = i;
}
}
n->suffixIndex = size - labelHeight;
//Uncomment below line to print suffix index
// printf(" [%d]", n->suffixIndex);
}
}
void freeSuffixTreeByPostOrder(Node *n)
{
if (n == NULL)
return ;
int i;
for (i = 0; i < MAX_CHAR; i++)
{
if (n->children[i] != NULL)
{
freeSuffixTreeByPostOrder(n->children[i]);
}
}
if (n->suffixIndex == -1)
free (n->end);
free (n);
}
/*Build the suffix tree and print the edge labels along with
suffixIndex. suffixIndex for leaf edges will be >= 0 and
for non-leaf edges will be -1*/
void buildSuffixTree()
{
size = strlen (text);
int i;
rootEnd = ( int *) malloc ( sizeof ( int ));
*rootEnd = - 1;
/*Root is a special node with start and end indices as -1,
as it has no parent from where an edge comes to root*/
root = newNode(-1, rootEnd);
activeNode = root; //First activeNode will be root
for (i=0; i<size; i++)
extendSuffixTree(i);
int labelHeight = 0;
setSuffixIndexByDFS(root, labelHeight);
}
int doTraversal(Node *n, int labelHeight, int * maxHeight,
int * substringStartIndex)
{
if (n == NULL)
{
return ;
}
int i=0;
int ret = -1;
if (n->suffixIndex < 0) //If it is internal node
{
for (i = 0; i < MAX_CHAR; i++)
{
if (n->children[i] != NULL)
{
ret = doTraversal(n->children[i], labelHeight +
edgeLength(n->children[i]),
maxHeight, substringStartIndex);
if (n->suffixIndex == -1)
n->suffixIndex = ret;
else if ((n->suffixIndex == -2 && ret == -3) ||
(n->suffixIndex == -3 && ret == -2) ||
n->suffixIndex == -4)
{
n->suffixIndex = -4; //Mark node as XY
//Keep track of deepest node
if (*maxHeight < labelHeight)
{
*maxHeight = labelHeight;
*substringStartIndex = *(n->end) -
labelHeight + 1;
}
}
}
}
}
else if (n->suffixIndex > -1 && n->suffixIndex < size1) //suffix of X
return -2; //Mark node as X
else if (n->suffixIndex >= size1) //suffix of Y
return -3; //Mark node as Y
return n->suffixIndex;
}
void getLongestCommonSubstring()
{
int maxHeight = 0;
int substringStartIndex = 0;
doTraversal(root, 0, &maxHeight, &substringStartIndex);
int k;
for (k=0; k<maxHeight; k++)
printf ( "%c" , text[k + substringStartIndex]);
if (k == 0)
printf ( "No common substring" );
else
printf ( ", of length: %d" ,maxHeight);
printf ( "" );
}
// driver program to test above functions
int main( int argc, char *argv[])
{
size1 = 7;
printf ( "Longest Common Substring in xabxac and abcabxabcd is: " );
strcpy (text, "xabxac#abcabxabcd$" ); buildSuffixTree();
getLongestCommonSubstring();
//Free the dynamically allocated memory
freeSuffixTreeByPostOrder(root);
size1 = 10;
printf ( "Longest Common Substring in xabxaabxa and babxba is: " );
strcpy (text, "xabxaabxa#babxba$" ); buildSuffixTree();
getLongestCommonSubstring();
//Free the dynamically allocated memory
freeSuffixTreeByPostOrder(root);
size1 = 14;
printf ( "Longest Common Substring in GeeksforGeeks and GeeksQuiz is: " );
strcpy (text, "GeeksforGeeks#GeeksQuiz$" ); buildSuffixTree();
getLongestCommonSubstring();
//Free the dynamically allocated memory
freeSuffixTreeByPostOrder(root);
size1 = 26;
printf ( "Longest Common Substring in OldSite:GeeksforGeeks.org" );
printf ( " and NewSite:GeeksQuiz.com is: " );
strcpy (text, "OldSite:GeeksforGeeks.org#NewSite:GeeksQuiz.com$" );
buildSuffixTree();
getLongestCommonSubstring();
//Free the dynamically allocated memory
freeSuffixTreeByPostOrder(root);
size1 = 6;
printf ( "Longest Common Substring in abcde and fghie is: " );
strcpy (text, "abcde#fghie$" ); buildSuffixTree();
getLongestCommonSubstring();
//Free the dynamically allocated memory
freeSuffixTreeByPostOrder(root);
size1 = 6;
printf ( "Longest Common Substring in pqrst and uvwxyz is: " );
strcpy (text, "pqrst#uvwxyz$" ); buildSuffixTree();
getLongestCommonSubstring();
//Free the dynamically allocated memory
freeSuffixTreeByPostOrder(root);
return 0;
}


输出:

Longest Common Substring in xabxac and abcabxabcd is: abxa, of length: 4Longest Common Substring in xabxaabxa and babxba is: abx, of length: 3Longest Common Substring in GeeksforGeeks and GeeksQuiz is: Geeks, of length: 5Longest Common Substring in OldSite:GeeksforGeeks.org and NewSite:GeeksQuiz.com is: Site:Geeks, of length: 10Longest Common Substring in abcde and fghie is: e, of length: 1Longest Common Substring in pqrst and uvwxyz is: No common substring

如果两个字符串的大小分别为M和N,则广义后缀树的构造采用O(M+N),LCS查找的是树上的DFS,同样是O(M+N)。 所以总体复杂性在时间和空间上是线性的。 后续行动:

  1. 给定一个模式,检查它是否是X或Y的子串,或者两者都是。如果它是一个子字符串,则查找它所属的字符串(X或Y或两者)的所有匹配项。
  2. 扩展实现以查找两个以上字符串的LCS
  3. 解决两个以上字符串的问题1
  4. 给定一个字符串,找到它的 最长回文子串

我们发表了以下更多关于后缀树应用程序的文章:

本文由 导演 。如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请发表评论

© 版权声明
THE END
喜欢就支持一下吧
点赞15 分享