zoukankan      html  css  js  c++  java
  • 动态规划求解最长公共子序列

    #include<iostream>
    #include<algorithm>
    using namespace std;
    void Print_LCS(int **b, string X,int m, int n);
    void LCS_Length(string X, string Y)
    {
        int m = X.length();
        int n = Y.length();
        X = "0" + X;
        Y = "0" + Y;
        int **b = new int*[m + 1];
        for (int i = 0; i < m + 1; i++)
        {
            b[i] = new int[n + 1];
        }
        int **c = new int*[m + 1];
        for (int i = 0; i < m + 1;i++)
        {
            c[i] = new int[n + 1];
        }
        for (int i = 1; i < m + 1; i++)
            c[i][0] = 0;
        for (int j = 0; j < n + 1; j++)
            c[0][j] = 0;
        for (int i = 1; i <= m; i++)
        {
            for (int j = 1; j <= n; j++)
            {
                if (X.at(i) == Y.at(j))
                {
                    c[i][j] = c[i - 1][j - 1] + 1;
                    b[i][j] = 1;
                }    
                else if (c[i - 1][j]>=c[i][j - 1])
                {
                    c[i][j] = c[i - 1][j];
                    b[i][j] = 0;
                }
                else
                {
                    c[i][j] = c[i][j - 1];
                    b[i][j] = -1;
                }
            }
        }
        Print_LCS(b,X,m,n);
        cout << "
    ";
    }
    void Print_LCS(int **b, string X,int m, int n)
    {
        if (m == 0 || n == 0)
            return;
        if (b[m][n] == 1)
        {
            Print_LCS(b,X,m-1,n-1);
            cout << X.at(m)<< "  ";
        }
        else if (b[m][n] == 0)
        {
            Print_LCS(b,X, m - 1, n);
        }
        else
        {
            Print_LCS(b, X,m , n-1);
        }
    }
    //带有备忘的LCS_LENGTH
    int LoopUp_LCS(string X, string Y, int **C,int m, int n)
    {
        if (C[m][n] > -1)
            return C[m][n];
        if (m == 0 || n == 0)
            C[m][n] = 0;
        else
        {
            if (X.at(m - 1) == Y.at(n - 1))
                C[m][n] = LoopUp_LCS(X,Y,C,m-1,n-1) + 1;
            else
            {
                C[m][n] = max(LoopUp_LCS(X, Y, C, m - 1, n), LoopUp_LCS(X, Y, C, m, n-1));
            }
        }
        return C[m][n];
    }
    int**Memoized_LCS_Length(string X, string Y)
    {
        int m = X.length();
        int n = Y.length();
        X = "0" + X;
        Y = "0" + Y;
        int **C = new int*[m + 1];
        for (int i = 0; i < m + 1; i++)
        {
            C[i] = new int[n + 1];
        }
        for (int i = 0; i < m + 1; i++)
        {
            for (int j = 0; j < n + 1; j++)
            {
                C[i][j] = -1;
            }
        }
    
        LoopUp_LCS(X,Y,C,m,n);
        return C;
    }
    //KMP
    
    int *Compute2_Prefix_Function(string P,int m)
    {
        int *t = new int[m + 1];
        t[1] = 0;
        int k = 0;
        for (int q = 2; q <= m; q++)
        {
            while (k > 0 && P[k + 1] != P[q])
                k = t[k];
            if (P[k + 1] == P[q])
                k = k + 1;
            t[q] = k;
        }
        for (int i = 1; i <= m; i++)
        {
            cout << t[i] << " ";
        }
        cout << "
    ";
        return t;
    }
    void KMP2_Matcher(string T, string P)
    {
        int n = T.length();
        int m = P.length();
        T = "0" + T;
        P = "0" + P;
        int *t = Compute2_Prefix_Function(P, m);
        int q = 0;
        for (int i = 1; i <= n; i++)
        {
            while (q > 0 && P[q + 1] != T[i])
                q = t[q];
            if (P[q + 1] == T[i])
                q = q + 1;
            if (q == m)
            {
                cout << "macther at " << i - m;
                q = t[q];
            }
        }
        cout << "
    ";
    
    }
    
    
    int main()
    {
        string X = "ABCBDAB";
        string Y = "BDCABA";
        LCS_Length(X,Y);
        int **C = Memoized_LCS_Length(X, Y);
        for (unsigned  i = 0; i < X.length() + 1; i++)
        {
            for (unsigned j = 0; j < Y.length(); j++)
            {
                cout << C[i][j] << "	";
            }
            cout << "
    ";
        }
        cout << "
    ";
        X = "ababababbc";
        Y = "ababbc";
        KMP2_Matcher(X,Y);
        return 0;
    }
  • 相关阅读:
    Java实现 蓝桥杯VIP 基础练习 完美的代价
    Java实现 蓝桥杯VIP基础练习 矩形面积交
    Java实现 蓝桥杯VIP 基础练习 完美的代价
    Java实现 蓝桥杯 蓝桥杯VIP 基础练习 数的读法
    Java实现 蓝桥杯 蓝桥杯VIP 基础练习 数的读法
    Java实现 蓝桥杯 蓝桥杯VIP 基础练习 数的读法
    Java实现 蓝桥杯 蓝桥杯VIP 基础练习 数的读法
    Java实现 蓝桥杯 蓝桥杯VIP 基础练习 数的读法
    核心思想:想清楚自己创业的目的(如果你没有自信提供一种更好的产品或服务,那就别做了,比如IM 电商 搜索)
    在Linux中如何利用backtrace信息解决问题
  • 原文地址:https://www.cnblogs.com/liuhg/p/LCS.html
Copyright © 2011-2022 走看看