求所有LCS
来源:互联网 发布:失忆听话水淘宝叫什么 编辑:程序博客网 时间:2024/06/11 06:28
//求取所有的最长公共子序列 #include <iostream> using namespace std; const int X = 100, Y = 100; //串的最大长度 char result[X+1]; //用于保存结果 int count = 0; //用于保存公共最长公共子串的个数 /*功能:计算最优值 *参数: * x:字符串x * y:字符串y * b:标志数组 * xlen:字符串x的长度 * ylen:字符串y的长度 *返回值:最长公共子序列的长度 * */ int Lcs_Length(string x, string y, int b[][Y+1], int xlen, int ylen) { int i = 0; int j = 0; int c[X+1][Y+1]; for (i = 0; i <= xlen; i++) { c[i][0]=0; } for (i = 0; i <= ylen; i++ ) { c[0][i]=0; } for (i = 1; i <= xlen; i++) { for (j = 1; j <= ylen; j++) { if (x[i - 1] == y[j - 1]) { c[i][j] = c[i-1][j-1]+1; b[i][j] = 1; } else if (c[i-1][j] > c[i][j-1]) { c[i][j] = c[i-1][j]; b[i][j] = 2; } else if(c[i-1][j] < c[i][j-1]) { c[i][j] = c[i][j-1]; b[i][j] = 3; } else { c[i][j] = c[i][j-1]; //或者c[i][j]=c[i-1][j]; b[i][j] = 4; } } } cout << "计算最优值效果图如下所示:" << endl; for(i = 1; i <= xlen; i++) { for(j = 1; j < ylen; j++) { cout << c[i][j] << " "; } cout << endl; } return c[xlen][ylen]; } /*功能:计算最长公共子序列 *参数: * i:字符串x的长度 * j:字符串y的长度 * x:字符串x * b:标志数组 * current_len:当前长度 * lcs_max_len:最长公共子序列长度 * */ void Display_Lcs(int i, int j, string x, int b[][Y+1], int current_len, int lcs_max_len) { if (i == 0 || j == 0) { for(int s = 0; s < lcs_max_len; s++) { cout << result[s]; } cout << endl; count++; return; } if(b[i][j] == 1) { current_len--; result[current_len]=x[i- 1]; Display_Lcs(i-1, j-1, x, b, current_len, lcs_max_len); } else { if(b[i][j] == 2) { Display_Lcs(i-1, j, x, b, current_len, lcs_max_len); } else { if(b[i][j] == 3) { Display_Lcs(i, j-1, x, b, current_len, lcs_max_len); } else { Display_Lcs(i, j-1, x, b, current_len, lcs_max_len); Display_Lcs(i-1, j, x, b, current_len, lcs_max_len); } } } } int main(int argc, char* argv[]) { string x = "ABCBDAB"; string y = "BDCABA"; int xlen = x.length(); int ylen = y.length(); int b[X + 1][Y + 1]; int lcs_max_len = Lcs_Length( x, y, b, xlen,ylen ); cout << lcs_max_len << endl; Display_Lcs( xlen, ylen, x, b, lcs_max_len, lcs_max_len ); cout << "共有:" << count << "种"; return 0; }
0 0
- 求所有LCS
- 动态规划 LCS 求两个序列A,B中所有的最长公共子序列
- NYOJ 题目760 See LCS again(LIS求LCS)
- lightoj 1157 - LCS Revisited LCS求方案数
- 动态规划:找出所有的LCS
- POJ1934:Trip(LCS + 所有路径打印)
- LCS算法求最长公共子串
- 求最长递减子序列LCS
- 求最长公共子序列(LCS)
- LCS算法求最长公共子串
- uva10635(LCS转换为求LIS)
- 动态规划和递归求lcs
- 求最长公共子序列(LCS)
- 求最长公共子序列LCS
- POJ 1159 (转换为求LCS)
- LCS求最长公共子串
- 【common subsequence】动态规划+回溯求LCS
- hdu1403(后缀数组求lcs模板)
- 从无头单链表中删除节点
- android 的好文章地址
- 珍惜因緣
- ubuntu12下安装jdk1.7与eclipse
- 博弈论-HDU-3951
- 求所有LCS
- Hue(二)安装
- GCC 中的编译器堆栈保护技术
- java计算时间差
- UTC转换本地时间
- 利用Nginx实现简单的动静分离
- Java网络编程中socket编程
- matlab中figure指令如果后面加1,2是什么意思,比如figure(1);figure(2)
- Tomcat 配置详解/优化方案