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

    // zuidazixulie.cpp : 定义控制台应用程序的入口点。
    //
    
    #include "stdafx.h"
    #include<iostream>
    #include<string>
    using namespace std;
    
    char b[10][10];
    int c[10][10];
    void Lcs(string x, string y)
    {
    
        int m = x.length();
        int n = y.length();
        for (int i = 1;i <= m; i++)
            c[i][0] = 0;
        for (int j = 0; j <= n; j++)
            c[0][j] = 0;
        for(int i=1;i<=m;i++)
            for (int j = 1; j <= n; j++)
            {
                if (x[i] == y[j]) {
                    c[i][j] = c[i - 1][j - 1] + 1;
                    b[i][j] = 'x';
                }
                else if (c[i - 1][j] >= c[i][j - 1]) {
                    c[i][j] = c[i - 1][j];
                    b[i][j] = '|';
                }
                else {
                    c[i][j] = c[i][j-1];
                    b[i][j] = '-';
                }
            }
    }
    
    void print(string x, int i, int j)
    {
        if (i == 0 || j == 0) return;
        if (b[i][j] == 'x') {
            print(x, i - 1, j - 1);
            cout << x[i];
        }
        else if (b[i][j] = '|')
            print(x, i - 1, j);
        else
            print(x, i,j - 1);
    }
    int main()
    {
        string x = "ABCD";
        string y = "bBCD";
        Lcs(x, y);
        print(x, 4, 3);
        while (1);
        return 0;
    }
     
    

      

  • 相关阅读:
    AdaBoost算法学习
    梯度下降与随机梯度下降
    Logistic Regression学习
    PCA算法
    VS2013常见错误排查
    K临近算法
    遗传算法小结
    SLIC超像素(superpixel)算法
    openslide api函数概要
    线程钩子
  • 原文地址:https://www.cnblogs.com/linear/p/6611446.html
Copyright © 2011-2022 走看看