zoukankan      html  css  js  c++  java
  • LCS的dp解法及其回溯操作的代码模板

    纯粹就是留个模板,
    不再叙述相应的实现原理了。

    #include<iostream>
    #include<algorithm>
    #include<string>
    #include<stack>
    #include<vector>
    #include<cstdio>
    #include<cstring>
    using namespace std;
    
    const int maxn = 2000;//兼容的最长字符串长度
    char a[maxn],b[maxn];
    int len1,len2;
    string ans;
    int dp[maxn][maxn];
    
    int LCS(int x,int y){//dp核心函数
        if(x<0 || y<0)return 0;
        if(dp[x][y]!=-1)return dp[x][y];
        dp[x][y] = (a[x]==b[y])? (1+LCS(x-1,y-1)) : max(LCS(x-1,y),LCS(x,y-1));
        return dp[x][y];
    }
    
    int BackTrace(){//回溯核心函数
        int x = len1 - 1;
        int y = len2 - 1;
        ans="";
        while(x>=0 && y>=0){
            if(a[x]==b[y]){
                ans += a[x];
                x--;y--;
            }else{
                if(dp[x-1][y] > dp[x][y-1])x--;
                else y--;
            }
        }
        //回溯后对字符串反转才是答案
        reverse(ans.begin(),ans.end());
        return 0;
    }
    
    //上面两个核心函数其实思维角度是一致的
    
    int main(){
        //初始化
        memset(a,0,sizeof(a));
        memset(b,0,sizeof(b));
        scanf("%s %s",a,b);
        len1 = strlen(a);
        len2 = strlen(b);
        memset(dp,-1,sizeof(dp));
        //LCS检测
        LCS(len1-1,len2-1);
        //回溯
        BackTrace();
        //输出答案
        cout<<ans<<endl;
        return 0;
    }
    
    

    OK

  • 相关阅读:
    jsp完成页面自动刷新
    ssm整合案例
    MySql 里的IFNULL、NULLIF和ISNULL用法
    数据库优化
    数据库读写并发控制
    solr入门
    AngularJS入门(一)
    jsp和html的的区别
    Docker 网络(十一)
    Docker 容器资源隔离 namespace(十)
  • 原文地址:https://www.cnblogs.com/savennist/p/13654333.html
Copyright © 2011-2022 走看看