<html>

<head><script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- MyFirstUnitAd -->
<ins class="adsbygoogle"
     style="display:inline-block;width:970px;height:250px"
     data-ad-client="ca-pub-5778386704669218"
     data-ad-slot="1503492166"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>

<meta http-equiv="Content-Language" content="zh-cn">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="GENERATOR" content="Microsoft FrontPage 5.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>Longest Common Subsequence</title>
</head>

<body>



<p align="left"><font size="6" color="#FF0000"><span lang="en-ca"><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
 
</b></span><b>&nbsp;&nbsp;&nbsp; </b></font><span lang="en-ca">
<font size="6" color="#FF0000"><b>Longest Common Subsequence</b></font></span></p>

<div align="left">
  <pre><b><font color="#ff0000" size="5">A. <span lang="en-ca">First </span>Edition</font></b></pre>
</div>
<div align="left">
  <pre><b>This is <span lang="en-ca">just a simple practice of algorithms of Longest Common Subsequence. </span></b></pre>
</div>
<div align="left">
  <pre><b><font color="#ff0000" size="5"><span lang="en-ca">B</span>.<span lang="en-ca"><a name="problem"></a>The problem</span></font></b></pre>
</div>
<p ALIGN="LEFT"><span lang="en-ca"><font size="2"><b>How to find the longest 
common subsequence? Here the common subsequence is defined to be common string 
such that</b></font></span></p>
<p ALIGN="LEFT"><span lang="en-ca"><font size="2"><b>you can omit one or more 
character within the string. i.e. &quot;string&quot; and &quot;satirainsg&quot; have the common 
subsequence</b></font></span></p>
<p ALIGN="LEFT"><span lang="en-ca"><font size="2"><b>of &quot;string&quot; even the second 
string has so many garbage within it. You just ignore them and continue to move</b></font></span></p>
<p ALIGN="LEFT"><span lang="en-ca"><font size="2"><b>your cursor. </b></font>
</span></p>
<p ALIGN="LEFT"><span lang="en-ca"><font size="2"><b>What I am doing here is try 
to test my idea that I can compare and get the longest common subsequence among
</b></font></span></p>
<p ALIGN="LEFT"><span lang="en-ca"><font size="2"><b>THREE strings by comparing 
the result common subsequence of two with the third string.</b></font></span></p>
<div align="left">
  <b><font color="#ff0000" size="5"><span lang="en-ca"><a name="explain"></a>C</span>.<span lang="en-ca">The
  </span></font></b><span lang="en-ca"><font size="5" color="#FF0000"><b>idea of 
  program</b></font></span></div>
<div align="left">
  กก</div>
<div align="left">
  <p ALIGN="LEFT"><span lang="en-ca"><font size="2"><b>This is the standard 
  algorithm from textbook. The only thing I misunderstand is that when you 
  re-construct the</b></font></span></div>
<div align="left">
  <p ALIGN="LEFT"><span lang="en-ca"><font size="2"><b>common string from the 
  matrix, you need to add the character into your stack unless it is surely not 
  from </b></font></span></div>
<div align="left">
  <p ALIGN="LEFT"><span lang="en-ca"><font size="2"><b>&quot;different&quot; case. (Do you 
  understand? See the algorithm and you know that it will write down the max of 
  its </b></font></span></div>
<div align="left">
  <p ALIGN="LEFT"><span lang="en-ca"><font size="2"><b>neighbour if the two 
  character from two string are different. If same, you write down the 
  upper-left number plus</b></font></span></div>
<div align="left">
  <p ALIGN="LEFT"><span lang="en-ca"><font size="2"><b>one.)</b></font></span></div>
<div align="left">
  <pre><b><font color="#ff0000" size="5">D.<span lang="en-ca"><a name="Method"></a>The </span>major functions</font></b></pre>
</div>
<div align="left">
  <pre>กก</pre>
</div>
<div align="left">
  <pre><b><font color="#ff0000" size="5"><span lang="en-ca">E</span>.</font></b><span lang="en-ca"><font size="5" color="#FF0000"><b>Further improvement</b></font></span></pre>
</div>
<div align="left">
  <pre>กก</pre>
</div>
<div align="left">
  <pre><b><font color="#ff0000" size="5"><span lang="en-ca">F</span>.</font></b><span lang="en-ca"><font size="5" color="#FF0000"><b>File listing</b></font></span></pre>
</div>
<div align="left">
  <pre><font size="3"><b>1<span lang="en-ca">. LCS.h</span></b></font></pre>
</div>
<div align="left">
  <pre><font size="3"><b><span lang="en-ca">2. LCS.cpp</span></b></font></pre>
</div>
<div align="left">
  <pre><font size="3"><b><span lang="en-ca">3. main.cpp</span></b></font></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><font size="3" color="#FF0000"><b>file name: LCS.h</b></font></span></pre>
</div>
<pre>
class LCS
{
private:
	int** matrix;
	char* rowStr;
	char* colStr;
	int row, col;
	int curRow, curCol;
	void initialize(char* str1, char* str2);
	void diffCase(int r, int c);
	void sameCase(int r, int c);
	void printMatrix();
	void uninitialize();
public:
	char* stack;
	int compStr(const char* str1, const char* str2);
	void print();
	LCS();
	~LCS();
};</pre>
<pre><span lang="en-ca"><font size="3" color="#FF0000"><b>file name: LCS.cpp</b></font></span></pre>
<pre>#include &lt;stdlib.h&gt;
#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
#include &quot;LCS.H&quot;

LCS::~LCS()
{
	uninitialize();
}

void LCS::uninitialize()
{
	if (matrix!=NULL)
	{
		for (int i=0; i&lt;row; i++)
		{
			delete [] matrix[i];
		}
		delete [] matrix;
		matrix=NULL;
	}
	if (stack!=NULL)
	{
		delete [] stack;
	}
	stack=NULL;
}

LCS::LCS()
{
	matrix=NULL;
	rowStr=colStr=stack=NULL;
	curRow=curCol=0;
}


void LCS::initialize(char* str1, char* str2)
{
	uninitialize();
	row=strlen(str1);
	col=strlen(str2);
	rowStr=(char*)str1;
	colStr=(char*)str2;
	matrix=new int*[row];
	for (int i=0; i&lt;row; i++)
	{
		matrix[i]=new int[col];
		for (int j=0; j&lt;col; j++)
		{
			//because every row by default has maximum of this number
			matrix[i][j]=i+1;
		}
	}
}

int LCS::compStr(const char* str1, const char* str2)
{
	initialize((char*)str1, (char*)str2);
	for (int r=0; r&lt;row; r++)
	{
		for (int c=0; c&lt;col; c++)
		{
			if (str1[r]!=str2[c])
			{
				diffCase(r, c);				
			}
			else
			{
				sameCase(r, c);
			}
			//if exceeds the limit, no need for further 
			if (matrix[r][c]==r+1)
			{
				break;
			}
		}
	}
	return matrix[row-1][col-1];
}

void LCS::printMatrix()
{
	for (int r=0; r&lt;col; r++)
	{
		printf(&quot;%2d  &quot;, r+1);
	}
	printf(&quot;\n&quot;);
	for ( r=0; r&lt;row; r++)
	{
		for (int c=0; c&lt;col; c++)
		{
			printf(&quot;%2d  &quot;, matrix[r][c]);
		}
		printf(&quot;\n&quot;);
	}
}


void LCS::print()
{
	int r=row-1, c=col-1, len=matrix[row-1][col-1];
	stack=new char[len+1];
	stack[len]='\0';
	printMatrix();
	printf(&quot;str1=%s\nstr2=%s\n&quot;, rowStr, colStr);
	while (r!=0&amp;&amp;c!=0)
	{
		if (matrix[r][c]==matrix[r-1][c])
		{			
			r--;			
		}
		else
		{
			if (matrix[r][c]==matrix[r][c-1])
			{
				c--;
			}
			else
			{
				//it must be same for the last one
				stack[--len]=rowStr[r];
				r--;
				c--;
			}
		}
	}
	if (len&gt;0)
	{
		stack[--len]=rowStr[r];
	}
	printf(&quot;the common subsequence is:%s\n&quot;, stack);
}

void LCS::sameCase(int r, int c)
{
	if (r!=0&amp;&amp;c!=0)
	{
		matrix[r][c]=matrix[r-1][c-1]+1;
	}
	else
	{
		matrix[r][c]=1;
	}
}

void LCS::diffCase(int r, int c)
{
	if (r!=0&amp;&amp;c!=0)
	{
		if (matrix[r][c-1]&gt;matrix[r-1][c])
		{
			matrix[r][c]=matrix[r][c-1];
		}
		else
		{
			matrix[r][c]=matrix[r-1][c];
		}
	}
	else
	{
		matrix[r][c]=0;
	}
}


</pre>
<pre><span lang="en-ca"><font size="3" color="#FF0000"><b>file name: main.cpp</b></font></span></pre>
<pre>#include &quot;LCS.H&quot;
#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;string.h&gt;



char* str1=&quot;010010100100111100&quot;;
char* str2=&quot;10001111010010101001001&quot;;
char* str3=&quot;01010010100101101&quot;;

int main()
{
	char buffer[30];
	LCS L;
	printf(&quot;The length is %d\n&quot;, L.compStr(str1, str2));
	L.print();
	strcpy(buffer, L.stack);
	printf(&quot;The length is %d\n&quot;, L.compStr(buffer, str3));
	L.print();
	printf(&quot;The length is %d\n&quot;, L.compStr(str1, str3));
	L.print();
	strcpy(buffer, L.stack);
	printf(&quot;The length is %d\n&quot;, L.compStr(buffer, str2));
	L.print();

	printf(&quot;The length is %d\n&quot;, L.compStr(str2, str3));
	L.print();
	strcpy(buffer, L.stack);
	printf(&quot;The length is %d\n&quot;, L.compStr(buffer, str1));
	L.print();


	return 0;
}

</pre>
<pre>
</pre>
<pre>
</pre>
<pre>





</pre>
<pre><span lang="en-ca"><font color="#0000FF"><b>How to run?</b></font></span></pre>
<pre><span lang="en-ca"><font color="#0000FF"><b>I try to get the longest common subsequence from THREE strings. First, I compare two strings and get the result </b></font></span></pre>
<pre><span lang="en-ca"><font color="#0000FF"><b>of LCS. Then I use the result to compare with the third because the common subsequence must be subsequence of</b></font></span></pre>
<pre><span lang="en-ca"><font color="#0000FF"><b>the longest common subsequence from previous two strings. Can you test it for me?</b></font></span></pre>
<pre><span lang="en-ca"> </span></pre>
<pre><span lang="en-ca">1. The following I am just comparing str1 and str2. Then I compare the result with str3.</span></pre>
<pre><span lang="en-ca">2. Secondly I change the comparing sequence: I compare str1 with str3. Then compare the result with str2.</span></pre>
<pre><span lang="en-ca">3. Thirdly I change the sequence again. str2 and str3 first and then result with str1.</span></pre>
<pre><span lang="en-ca">4. The results of 1) and 2) are different. And result of 1) and 3) are same.</span></pre>
<pre>The length is 15
 1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20  21  22  23
 0   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1
 1   1   1   1   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2
 0   2   2   2   2   2   2   2   3   3   3   3   3   3   3   3   3   3   3   3   3   3   3
 0   1   3   3   3   3   3   3   3   3   4   4   4   4   4   4   4   4   4   4   4   4   4
 1   1   3   3   4   4   4   4   4   4   4   4   5   5   5   5   5   5   5   5   5   5   5
 0   2   2   4   4   4   4   4   5   5   5   5   5   6   6   6   6   6   6   6   6   6   6
 1   2   2   4   5   5   5   5   5   6   6   6   6   6   7   7   7   7   7   7   7   7   7
 0   2   3   3   5   5   5   5   6   6   7   7   7   7   7   8   8   8   8   8   8   8   8
 0   1   3   4   5   5   5   5   6   6   7   8   8   8   8   8   8   9   9   9   9   9   9
 1   1   3   4   5   6   6   6   6   7   7   8   9   9   9   9   9   9   9  10  10  10  10
 0   2   2   4   5   6   6   6   7   7   8   8   9  10  10  10  10  10  10  10  11  11  11
 0   1   3   3   5   6   6   6   7   7   8   9   9  10  10  11  11  11  11  11  11  12  12
 1   1   3   3   4   6   7   7   7   8   8   9  10  10  11  11  12  12  12  12  12  12  13
 1   1   3   3   4   5   7   8   8   8   8   9  10  10  11  11  12  12  12  13  13  13  13
 1   1   3   3   4   5   6   8   8   9   9   9  10  10  11  11  12  12  12  13  13  13  14
 1   1   3   3   4   5   6   7   8   9   9   9  10  10  11  11  12  12  12  13  13  13  14
 0   2   2   4   4   5   6   7   8   9  10  10  10  11  11  12  12  13  13  13  14  14  14
 0   1   3   3   4   5   6   7   8   9  10  11  11  11  11  12  12  13  14  14  14  15  15
str1=010010100100111100
str2=10001111010010101001001
the common subsequence is:100101001001100
The length is 13
 1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17
 0   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1
 1   1   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2
 1   1   2   2   3   3   3   3   3   3   3   3   3   3   3   3   3
 0   2   2   3   3   3   4   4   4   4   4   4   4   4   4   4   4
 1   2   3   3   4   4   4   5   5   5   5   5   5   5   5   5   5
 0   2   3   4   4   4   5   5   6   6   6   6   6   6   6   6   6
 1   2   3   4   5   5   5   6   6   7   7   7   7   7   7   7   7
 1   2   3   4   5   6   6   6   6   7   8   8   8   8   8   8   8
 0   2   3   4   5   6   7   7   7   7   8   9   9   9   9   9   9
 1   2   3   4   5   6   7   8   8   8   8   9  10  10  10  10  10
 1   2   3   4   5   6   7   8   8   9   9   9  10  10  10  11  11
 0   2   3   4   5   6   7   8   9   9   9  10  10  11  11  11  12
 0   1   3   4   5   6   7   8   9   9   9  10  10  11  12  12  12
 1   1   2   4   5   6   7   8   9  10  10  10  11  11  12  13  13
 1   1   2   4   5   6   7   8   9  10  11  11  11  11  12  13  13
str1=100101001001100
str2=01010010100101101
the common subsequence is:1001010010110
The length is 14
 1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17
 1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1
 0   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2
 1   2   3   3   3   3   3   3   3   3   3   3   3   3   3   3   3
 1   2   3   3   4   4   4   4   4   4   4   4   4   4   4   4   4
 0   2   3   4   4   4   5   5   5   5   5   5   5   5   5   5   5
 1   2   3   4   5   5   5   6   6   6   6   6   6   6   6   6   6
 0   2   3   4   5   5   6   6   7   7   7   7   7   7   7   7   7
 1   2   3   4   5   6   6   7   7   8   8   8   8   8   8   8   8
 1   2   3   4   5   6   6   7   7   8   9   9   9   9   9   9   9
 0   2   3   4   5   6   7   7   8   8   9  10  10  10  10  10  10
 1   2   3   4   5   6   7   8   8   9   9  10  11  11  11  11  11
 1   2   3   4   5   6   7   8   8   9  10  10  11  11  11  12  12
 0   2   3   4   5   6   7   8   9   9  10  11  11  12  12  12  13
 0   1   3   4   5   6   7   8   9   9  10  11  11  12  13  13  13
 0   1   3   4   5   6   7   8   9   9  10  11  11  12  13  13  14
 0   1   3   4   5   6   7   8   9   9  10  11  11  12  13  13  14
 1   1   2   4   5   6   7   8   9  10  10  11  12  12  13  14  14
 1   1   2   4   5   6   7   8   9  10  11  11  12  12  13  14  14
str1=010010100100111100
str2=01010010100101101
the common subsequence is:01001010010111
The length is 13
 1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20  21  22  23
 0   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1
 1   1   1   1   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2
 0   2   2   2   2   2   2   2   3   3   3   3   3   3   3   3   3   3   3   3   3   3   3
 0   1   3   3   3   3   3   3   3   3   4   4   4   4   4   4   4   4   4   4   4   4   4
 1   1   3   3   4   4   4   4   4   4   4   4   5   5   5   5   5   5   5   5   5   5   5
 0   2   2   4   4   4   4   4   5   5   5   5   5   6   6   6   6   6   6   6   6   6   6
 1   2   2   4   5   5   5   5   5   6   6   6   6   6   7   7   7   7   7   7   7   7   7
 0   2   3   3   5   5   5   5   6   6   7   7   7   7   7   8   8   8   8   8   8   8   8
 0   1   3   4   5   5   5   5   6   6   7   8   8   8   8   8   8   9   9   9   9   9   9
 1   1   3   4   5   6   6   6   6   7   7   8   9   9   9   9   9   9   9  10  10  10  10
 0   2   2   4   5   6   6   6   7   7   8   8   9  10  10  10  10  10  10  10  11  11  11
 1   2   2   4   5   6   7   7   7   8   8   8   9  10  11  11  11  11  11  11  11  11  12
 1   2   2   4   5   6   7   8   8   8   8   8   9  10  11  11  12  12  12  12  12  12  12
 1   2   2   4   5   6   7   8   8   9   9   9   9  10  11  11  12  12  12  13  13  13  13
str1=01001010010111
str2=10001111010010101001001
the common subsequence is:1001010010111
The length is 15
 1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17
 0   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1
 1   1   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2
 1   1   2   2   3   3   3   3   3   3   3   3   3   3   3   3   3
 1   1   2   2   3   4   4   4   4   4   4   4   4   4   4   4   4
 0   2   2   3   3   4   5   5   5   5   5   5   5   5   5   5   5
 0   1   2   3   3   4   5   5   6   6   6   6   6   6   6   6   6
 0   1   2   3   3   4   5   5   6   6   6   7   7   7   7   7   7
 0   1   2   3   3   4   5   5   6   6   6   7   7   8   8   8   8
 1   1   2   3   4   4   5   6   6   7   7   7   8   8   8   9   9
 0   2   2   3   4   4   5   6   7   7   7   8   8   9   9   9  10
 1   2   3   3   4   5   5   6   7   8   8   8   9   9   9  10  10
 1   2   3   3   4   5   5   6   7   8   9   9   9   9   9  10  10
 0   2   3   4   4   5   6   6   7   8   9  10  10  10  10  10  11
 1   2   3   4   5   5   6   7   7   8   9  10  11  11  11  11  11
 0   2   3   4   5   5   6   7   8   8   9  10  11  12  12  12  12
 1   2   3   4   5   6   6   7   8   9   9  10  11  12  12  13  13
 0   2   3   4   5   6   7   7   8   9   9  10  11  12  13  13  14
 1   2   3   4   5   6   7   8   8   9  10  10  11  12  13  14  14
 1   2   3   4   5   6   7   8   8   9  10  10  11  12  13  14  14
 0   2   3   4   5   6   7   8   9   9  10  11  11  12  13  14  15
 1   2   3   4   5   6   7   8   9  10  10  11  12  12  13  14  15
 1   2   3   4   5   6   7   8   9  10  11  11  12  12  13  14  15
 0   2   3   4   5   6   7   8   9  10  11  12  12  13  13  14  15
str1=10001111010010101001001
str2=01010010100101101
the common subsequence is:100010100101101
The length is 13
 1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18
 0   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1
 1   1   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2
 1   1   2   3   3   3   3   3   3   3   3   3   3   3   3   3   3   3
 1   1   2   3   3   4   4   4   4   4   4   4   4   4   4   4   4   4
 0   2   2   3   4   4   5   5   5   5   5   5   5   5   5   5   5   5
 1   2   3   3   4   5   5   6   6   6   6   6   6   6   6   6   6   6
 0   2   3   3   4   5   6   6   6   7   7   7   7   7   7   7   7   7
 1   2   3   4   4   5   6   7   7   7   8   8   8   8   8   8   8   8
 1   2   3   4   4   5   6   7   8   8   8   9   9   9   9   9   9   9
 0   2   3   4   5   5   6   7   8   9   9   9  10  10  10  10  10  10
 1   2   3   4   5   6   6   7   8   9  10  10  10  10  10  10  11  11
 0   2   3   4   5   6   7   7   8   9  10  10  11  11  11  11  11  11
 0   1   3   4   5   6   7   7   8   9  10  10  11  12  12  12  12  12
 1   1   2   4   5   6   7   8   8   9  10  11  11  12  12  12  13  13
 0   2   2   4   5   6   7   8   8   9  10  11  12  12  13  13  13  13
str1=100010100101101
str2=010010100100111100
the common subsequence is:1001010010110
Press any key to continue</pre>

<pre></pre>

<pre></pre>

<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;                                   
&nbsp;&nbsp;&nbsp; <a href="game24.htm">                  







                       <img src="picture/back.gif" style="border: medium none" alt="back.gif (341 bytes)" width="32" height="35"></a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
<a href="index.htm">
<img src="picture/up.gif" style="border: medium none" alt="up.gif (335 bytes)" width="35" height="32"></a>       &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;                         
<img src="picture/next.gif" style="border: medium none" alt="next.gif (337 bytes)" width="32" height="35">          


</p>

</body>

</html>