// initiate the direction matrix
int **LCS_direction;
LCS_direction = (int**)(new int[length1]);
for( i = 0; i < length1; ++ i)
LCS_direction[i] = (int*)new int[length2];
for(i = 0; i < length1; ++ i)
for(j = 0; j < length2; ++ j)
LCS_direction[i][j] = kInit;
for(i = 0; i < length1; ++ i)
{
for(j = 0; j < length2; ++ j)
{
if(i == 0 || j == 0)
{
if(pStr1[i] == pStr2[j])
{
LCS_length[i][j] = 1;
LCS_direction[i][j] = kLeftUp;
}
else
LCS_length[i][j] = 0;
}
// a char of LCS is found,
// it comes from the left up entry in the direction matrix
else if(pStr1[i] == pStr2[j])
{
LCS_length[i][j] = LCS_length[i - 1][j - 1] + 1;
LCS_direction[i][j] = kLeftUp;
}
// it comes from the up entry in the direction matrix
else if(LCS_length[i - 1][j] > LCS_length[i][j - 1])
{
LCS_length[i][j] = LCS_length[i - 1][j];
LCS_direction[i][j] = kUp;
}
// it comes from the left entry in the direction matrix
else
{
LCS_length[i][j] = LCS_length[i][j - 1];
LCS_direction[i][j] = kLeft;
}
}
}
LCS_Print(LCS_direction, pStr1, pStr2, length1 - 1, length2 - 1);
return LCS_length[length1 - 1][length2 - 1];
}
/////////////////////////////////////////////////////////////////////////////
// Print a LCS for two strings
// Input: LCS_direction - a 2d matrix which records the direction of
// LCS generation
// pStr1 - the first string
// pStr2 - the second string
// row - the row index in the matrix LCS_direction
// col - the column index in the matrix LCS_direction
/////////////////////////////////////////////////////////////////////////////
-
推荐文章
- 1秘书处面试问题及答案_学生会秘书处面试技巧
- 22022行政助理面试问题汇总
- 32022行政人员面试常见问题及回答技巧!
- 4江苏公务员面试试题的参考答案
- 5贵州公务员面试试题参考答案
- 6历年吉林公务员面试试题答案
- 7湖南公务员面试试题答案
- 8黑龙江公务员面试试题答案
- 9有关银行竞聘面试题目
- 10关于银行面试问题
- 11行政助理人员的面试题目
- 12有关教师面试题目
- 13关于学校教师面试问题
- 14教师行业常见的面试问题
- 15大学自主招生面试真题
-
热门文章
- 12022行政人员面试常见问题及回答技巧!
- 2程序员面试题精选100题(51)-顺时针打印矩阵[算法]
- 3程序员面试题精选100题(56)-C/C++/C#面试题(4)
- 4程序员面试题精选100题(37)-寻找丑数[算法]
- 5程序员面试题精选100题(53)-C++/C#面试题(2)
- 6程序员面试题精选100题(61)-数对之差的最大值[算法]
- 72022行政助理面试问题汇总
- 8程序员面试题精选100题(41)-把数组排成最小的数(算法)
- 9程序员面试题精选100题(35)-两链表的第一个公共结点[数据结构]
- 10程序员面试题精选100题(06)-整数二进制表示中1的个数[算法]
- 11程序员面试题精选100题(05)-数组中只出现一次的数字[算法]
- 12程序员面试题精选100题(50)-树的子结构[数据结构]
- 13广州2017年公办小学面试
- 14安庆潜山事业单位面试真题汇总
- 15程序员面试题精选100题(60)-判断二叉树是不是平衡[数据结构]