天生我材必有用,千金散尽还复来。 仰天大笑出门去,我辈岂是蓬蒿人。 大鹏一日同风起,扶摇直上九万里。 十步杀一人,千里不留行。 事了拂衣去,深藏身与名。 安能摧眉折腰事权贵,使我不得开心颜! 且乐生前一杯酒,何须身后千载名? 愿将腰下剑,直为斩楼兰。
 

lcs

 

lcs

刚刚重新学了一种模板。

#include<bits/stdc++.h>
using namespace std;
int fcs(const char *ts1,const char *ts2,string & str){//参数传入char * 和引用
    int l1 = strlen(ts1);
    int l2 = strlen(ts2);
    const char * s1 = ts1 - 1;//这里用于将char数组前移一位,方便后面操作。下标1对应第一个char
    const char * s2 = ts2 - 1;
    int m[101][101] = {0};
    int i,j;
    for(i = 1;i <= l1;i++){
        for(j = 1; j <= l2;j++){
            m[i][j] = max(m[i-1][j],m[i][j-1]);
            if(s1[i] == s2[j]){
                m[i][j] = max(m[i][j],m[i-1][j-1]+1);
            }
        }
    }
//后面求出最长公共子序列 i
= l1,j = l2; while(i != 0 && j!=0 ){ if(s1[i] == s2[j]){ str.push_back(s1[i]); i--; j--; }else{ if(m[i-1][j] > m[i][j-1]){ i--; }else{ j--; } } } reverse(str.begin(),str.end()); return m[l1][l2];//返回长度 } int main() { const char *s1 = "abcdef"; const char *s2 = "bcdefg"; string s; cout << fcs(s1,s2,s) << endl; cout << s << endl; return 0; }

 

posted @ 2019-02-12 12:09  gudy  阅读(249)  评论(0编辑  收藏  举报