zoukankan      html  css  js  c++  java
  • DP——最长公共子序列

        好吧,我承认写这篇博客是为了凑数量,但还是要好好写……

        最长公共子序列,众所周知的问题,给定两个序列,求最长公共子序列……(其实还有一种叫 diff算法的东西,更优,但我不会……)

        其实,DP方程浅显易懂,直接上代码就可以……

        时间复杂度O(mn)。

        代码:

     1 #include<cstdio>
     2 #include<iostream>
     3 #include<cstring>
     4 #include<string>
     5 #include<cmath>
     6 #include<algorithm>
     7 using namespace std;
     8 int n,m;
     9 string s,t;
    10 int dp[1000][1000];
    11 
    12 int main(){
    13     getline(cin,s);
    14     getline(cin,t);
    15     n=s.length()-1;
    16     m=t.length()-1;
    17     for(int i=0;i<=n;i++)
    18       for(int j=0;j<=m;j++){
    19           if(s[i]==t[j]){
    20               dp[i+1][j+1]=dp[i][j]+1;
    21           }else{
    22               dp[i+1][j+1]=max(dp[i][j+1],dp[i+1][j]);
    23           }
    24       }
    25     printf("%d",dp[n+1][m+1]);
    26     return 0;
    27 }
  • 相关阅读:
    mysql权限
    Win7_64位使用Mysql Odbc
    二叉树的遍历
    Notepad++的使用
    mysql与mysqld
    Mysql 聚集函数和分组
    Linux 目录
    Linux 倒引号、单引号、双引号
    openkm安装过程
    rhel 7 设置默认运行级别为图形
  • 原文地址:https://www.cnblogs.com/Misaki-Mei/p/7347895.html
Copyright © 2011-2022 走看看