动态规划求解最长公共子序列

本文介绍了一种求解最长公共子序列问题的算法实现,通过动态规划方法找到两个字符串的最长公共子序列,并详细展示了C++代码实现过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://blog.youkuaiyun.com/zzuchengming/article/details/50822735

实现代码如下:

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. #include "stdafx.h"  
  2. #include <iostream>  
  3. using namespace std;  
  4.   
  5. void lcs(char *a, char *b, int m, int n, int **c, int **s)  
  6. {  
  7.     if (m == 0 || n == 0 || a == NULL || b == NULL )  
  8.     {  
  9.         return;  
  10.     }  
  11.     int i,j;  
  12.     for (i = 0; i <= m; i++)  
  13.     {  
  14.         c[i][0] = 0;  
  15.     }  
  16.     for (j = 1; j <= n; j++)  
  17.     {  
  18.         c[0][j] = 0;  
  19.     }  
  20.     for (i = 1; i <= m; i++)  
  21.     {  
  22.         for (j = 1; j <= n; j++)  
  23.         {  
  24.             if (a[i-1] == b[j-1])//注意  
  25.             {  
  26.                 c[i][j] = c[i-1][j-1] + 1;//c[i][j]对应的是a[0到i-1]与b[0到j-1]的公共子序列  
  27.                 s[i][j] = 0;  
  28.             }  
  29.             else  
  30.                 if (c[i-1][j] > c[i][j-1])  
  31.                 {  
  32.                     c[i][j] = c[i-1][j];  
  33.                     s[i][j] = 1;  
  34.                 }  
  35.                 else  
  36.                 {  
  37.                     c[i][j] = c[i][j-1];  
  38.                     s[i][j] = -1;  
  39.                 }  
  40.         }  
  41.     }  
  42. }  
  43.   
  44. void printlcs(char *a, int line, int row, int **c, int **s)  
  45. {  
  46.     //if (line == 0 || row == 0)  
  47.     //{  
  48.     //  return;  
  49.     //}  
  50.     if (c[line][row] == 0)//为什么加这个  
  51.     {  
  52.         return;  
  53.     }  
  54.     int i = line, j = row;  
  55.     if (s[i][j] == 0)  
  56.     {  
  57.         cout << a[i - 1] << ' ';//为什么不是输出a[i]  
  58.         printlcs(a, i - 1, j - 1, c, s);  
  59.     }  
  60.     else  
  61.         if (s[i][j] == 1)  
  62.         {  
  63.             printlcs(a, i - 1, j, c, s);  
  64.         }  
  65.         else  
  66.             printlcs(a, i, j - 1, c ,s);  
  67. }  
  68. int _tmain(int argc, _TCHAR* argv[])  
  69. {  
  70.     char a[] = "abcbdab";  
  71.     char b[] = "bdcaba";  
  72.     int length1 = sizeof(a)/sizeof(a[0]);  
  73.     int length2 = sizeof(b)/sizeof(b[0]);  
  74.     int **c = new int*[length1];  
  75.     int **s = new int*[length1];  
  76.     for (int i = 0; i < length1; i++)  
  77.     {  
  78.         c[i] = new int[length2];  
  79.         s[i] = new int[length2];  
  80.     }  
  81.     lcs(a, b, length1 - 1, length2 - 1, c, s);  
  82.     cout << c[length1 - 1][length2 - 1];  
  83.     printlcs(a, length1 - 1, length2 - 1, c, s);  
  84.     system("pause");  
  85.     return 0;  
  86. }  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值