• 最长公共子序列(模板 LCSL)


    博客:
     https://www.cnblogs.com/sasuke-/p/5396843.html


    模板


    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #define MAXN 1010
    using namespace std;
    int c[MAXN][MAXN];//保存各个公共子序列的长度
    int b[MAXN][MAXN];//记录来源
    char s1[MAXN], s2[MAXN];
    int len1, len2;
    void LCSL()
    {
     for (int i = 1; i <= len1; ++i)//控制s1序列
     {
      for (int j = 1; j <= len2; ++j)//控制s2
      {
       if (s1[i - 1] == s2[j - 1])
       {//如果字符相同最长公共子序列+1
        c[i][j] = c[i - 1][j - 1] + 1;
        b[i][j] = 1;  //表示来源自斜对角
       }
       else
       {
        if (c[i][j - 1] >= c[i - 1][j])
        {
         c[i][j] = c[i][j - 1];//继承最相邻原有的最大长度
         b[i][j] = 2;//记录来源自左边
        }
        else
        {
         c[i][j] = c[i - 1][j];
         b[i][j] = 3;
        }
       }
      }
     }
    }
    void print(int i, int j)
    {
     if (i == 0 || j == 0)return;
     if (b[i][j] == 1)
     {
      print(i - 1, j - 1);
      cout << s1[i - 1];
     }
     else if (b[i][j]==2)
     {
      print(i, j - 1);
     }
     else
     {
      print(i - 1, j);
     }
    }
    int main()
    {
     cout << "输入s1:" << endl;
     cin >> s1;
     cout << "输入s2:" << endl;
     cin >> s2;
     len1 = strlen(s1);
     len2 = strlen(s2);
     LCSL();
     cout << "s1与s2的最长公共子序列为" << c[len1][len2] << endl;
     print(len1, len2);
     printf("\n");
    }

  • 相关阅读:
    Threaten Model
    什么是虚拟主机
    http代理服务器
    什么是https
    缓存的实现原理
    Cookie和Session
    HTTP协议详解
    心路历程——毕设程序mr跑不通的问题
    bash: hadoop:command not found
    Mapreduce 测试自带实例 wordcount
  • 原文地址:https://www.cnblogs.com/ALINGMAOMAO/p/9439715.html
Copyright © 2020-2023  润新知