第七周作业—最长递增子序列

cpp] view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #include "stdafx.h"    
  2. #include "string.h"    
  3. #include <iostream>    
  4. using namespace std;    
  5.     
  6.   
  7. enum decreaseDir {kInit = 0, kLeft, kUp, kLeftUp};    
  8.     
  9. void LCS_Print(int **LCS_direction,     
  10.     char* pStr1, char* pStr2,     
  11.     size_t row, size_t col);    
  12.     
  13. int LCS(char* pStr1, char* pStr2)    
  14. {    
  15.     if(!pStr1 || !pStr2)    
  16.         return 0;    
  17.     
  18.     size_t length1 = strlen(pStr1);    
  19.     size_t length2 = strlen(pStr2);    
  20.     if(!length1 || !length2)    
  21.         return 0;    
  22.     
  23.     size_t i, j;    
  24.     
  25.     int **LCS_length;    
  26.     LCS_length = (int**)(new int[length1]);    
  27.     for(i = 0; i < length1; ++ i)    
  28.         LCS_length[i] = (int*)new int[length2];    
  29.     
  30.     for(i = 0; i < length1; ++ i)    
  31.         for(j = 0; j < length2; ++ j)    
  32.             LCS_length[i][j] = 0;    
  33.     
  34.     int **LCS_direction;    
  35.     LCS_direction = (int**)(new int[length1]);    
  36.     for( i = 0; i < length1; ++ i)    
  37.         LCS_direction[i] = (int*)new int[length2];    
  38.     
  39.     for(i = 0; i < length1; ++ i)    
  40.         for(j = 0; j < length2; ++ j)    
  41.             LCS_direction[i][j] = kInit;    
  42.     
  43.     for(i = 0; i < length1; ++ i)    
  44.     {    
  45.         for(j = 0; j < length2; ++ j)    
  46.         {    
  47.             //之前此处的代码有问题,现在订正如下:    
  48.             if(i == 0 || j == 0)     
  49.             {     
  50.                 if(pStr1[i] == pStr2[j])     
  51.                 {     
  52.                     LCS_length[i][j] = 1;     
  53.                     LCS_direction[i][j] = kLeftUp;     
  54.                 }     
  55.                 else     
  56.                 {     
  57.                     if(i > 0)     
  58.                     {     
  59.                         LCS_length[i][j] = LCS_length[i - 1][j];     
  60.                         LCS_direction[i][j] = kUp;     
  61.                     }     
  62.                     if(j > 0)     
  63.                     {     
  64.                         LCS_length[i][j] = LCS_length[i][j - 1];     
  65.                         LCS_direction[i][j] = kLeft;     
  66.                     }     
  67.                 }     
  68.             }    
  69.       
  70.             else if(pStr1[i] == pStr2[j])    
  71.             {    
  72.                 LCS_length[i][j] = LCS_length[i - 1][j - 1] + 1;    
  73.                 LCS_direction[i][j] = kLeftUp;    
  74.             }    
  75.            
  76.             else if(LCS_length[i - 1][j] > LCS_length[i][j - 1])    
  77.             {    
  78.                 LCS_length[i][j] = LCS_length[i - 1][j];    
  79.                 LCS_direction[i][j] = kUp;    
  80.             }    
  81.           
  82.             else    
  83.             {    
  84.                 LCS_length[i][j] = LCS_length[i][j - 1];    
  85.                 LCS_direction[i][j] = kLeft;    
  86.             }    
  87.         }    
  88.     }    
  89.     LCS_Print(LCS_direction, pStr1, pStr2, length1 - 1, length2 - 1); //调用下面的LCS_Pring 打印出所求子串。    
  90.     return LCS_length[length1 - 1][length2 - 1];                      //返回长度。    
  91. }    
  92.     
  93. void LCS_Print(int **LCS_direction,     
  94.     char* pStr1, char* pStr2,     
  95.     size_t row, size_t col)    
  96. {    
  97.     if(pStr1 == NULL || pStr2 == NULL)    
  98.         return;    
  99.     
  100.     size_t length1 = strlen(pStr1);    
  101.     size_t length2 = strlen(pStr2);    
  102.     
  103.     if(length1 == 0 || length2 == 0 || !(row < length1 && col < length2))    
  104.         return;    
  105.     
  106.     if(LCS_direction[row][col] == kLeftUp)    
  107.     {    
  108.         if(row > 0 && col > 0)    
  109.             LCS_Print(LCS_direction, pStr1, pStr2, row - 1, col - 1);    
  110.     
  111.         printf("%c", pStr1[row]);    
  112.     }    
  113.     else if(LCS_direction[row][col] == kLeft)    
  114.     {    
  115.         if(col > 0)    
  116.             LCS_Print(LCS_direction, pStr1, pStr2, row, col - 1);    
  117.     }    
  118.     else if(LCS_direction[row][col] == kUp)    
  119.     {    
  120.         if(row > 0)    
  121.             LCS_Print(LCS_direction, pStr1, pStr2, row - 1, col);    
  122.     }    
  123. }    
  124.     
  125. int _tmain(int argc, _TCHAR* argv[])    
  126. {    
  127.     char* pStr1="abcde";    
  128.     char* pStr2="acde";    
  129.     LCS(pStr1,pStr2);    
  130.     printf("\n");    
  131.     system("pause");    
  132.     return 0;    
  133. }   


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值