最长公共子序列LCS
2011-11-24 12:24
316 查看
//LCS算法,最长公共子序列 来自《算法导论》
#include<iostream> #include<cstdlib> #include<cmath> #define N 105 char s[N+1][N+1]; using namespace std; int LCS( const char*s1,const char*s2){ int m = strlen(s1); int n = strlen(s2); int i,j; s[0][0]=0; for( i = 0;i <= m;i++){ s[i][0]=0; } for( j = 0;j <= n;j++){ s[0][j]=0; } for( i = 1;i <= m;i++){ for( j = 1;j <= n;j++){ if(s1[i]==s2[j]){ s[i][j]=s[i-1][j-1]+1; } else if(s[i-1][j]>s[i][j-1]){ s[i][j]=s[i-1][j]; } else { s[i][j]=s[i][j-1]; } } } return s[m] ; } int main(){ char s1 ,s2 ; while( cin >> s1 >> s2){ cout << LCS(s1,s2) << endl; } system("pause"); }
相关文章推荐
- 最长公共子序列(LCS)
- 动态规划算法解最长公共子序列LCS问题
- 算法12—动态规划算法之:最长公共子序列 & 最长公共子串(LCS)
- LCS最长公共子序列java版
- 最长公共子序列(LCS---动态规划)
- LCS最长公共子序列(最优线性时间O(n))
- 求最长公共子序列(LCS)
- 51Nod 1006 最长公共子序列Lcs
- 最长公共子序列(LCS)
- 最长公共子序列(Lcs)51NOD-1006
- 最长公共子序列(LCS)
- 最长公共子序列问题LCS
- LCS,最长公共子序列
- 【51NOD-0】1006 最长公共子序列Lcs
- 51Nod 1006 最长公共子序列Lcs问题 模板题
- 最长公共子序列问题LCS
- 动态规划之最长公共子序列(LCS)
- 51Nod-1006-最长公共子序列Lcs
- 算法:动态规划——最长公共子序列(LCS)
- 51Nod 1006 最长公共子序列Lcs(DP)