美文网首页
最长公共子序列【动态规划】

最长公共子序列【动态规划】

作者: 编码的哲哲 | 来源:发表于2016-11-29 19:55 被阅读19次

include<iostream>

include<string>

using namespace std;

int main(){

cout<<"请输入两个字符串"<<endl;

string str1;
string str2;

cin>>str1>>str2;

int len1 = str1.length();
int len2 = str2.length();

//i,j公共子序列长度 
int c [len1+1][len2+1];

for(int i = 0; i <= len1; i++)
for(int j = 0; j <= len2; j++){
    
    if( i==0 || j==0){
        
        c[i][j] = 0; 
        
    }else if( str1[i-1] == str2[j-1] ){
        
        c[i][j] = c[i-1][j-1] + 1;
                    
    }else{
        
        c[i][j] = max(c[i-1][j], c[i][j-1]);
    }
}

cout<<"最大公共子序列长度为:"<<c[len1][len2]<<endl;

}

相关文章

网友评论

      本文标题:最长公共子序列【动态规划】

      本文链接:https://www.haomeiwen.com/subject/nakzpttx.html