求两字符串的最长相同子序列

/*
//ABDCDCBABC  CBABCDCDBA   7
最长相同子序列(不需要连续)
采用动态规划DP 二维表 输出最后一个位置就可
*/
#include <iostream> #include <string> using namespace std; int n, m; string a, b; int main() { cin >> a >> b; n = a.size(); m = b.size(); int f[n+1][m+1]; for(int i=0; i<=m; i++){ f[0][i] = 0; } for(int i=0; i<=n; i++){ f[i][0] = 0; } for(int i=1; i<=n; i++) for(int j=1; j<=m; j++) if(a[i-1] == b[j-1]) f[i][j] = f[i-1][j-1] + 1; else f[i][j] = max(f[i-1][j], f[i][j-1]); cout << f[n][m] << endl; for(int i=0; i<=n; i++) for(int j=0; j<=m; j++){ cout<<f[i][j]; if(j%m==0&&j!=0)cout<<endl; } return 0; }

  

猜你喜欢

转载自www.cnblogs.com/Stephen-Jixing/p/12628365.html