Longest common sub-sequence LCS (template) Poj 1458

Source: Internet
Author: User
Tags bitset stdin strlen cmath
one, Standard template
#include <iostream> #include <stdio.h> #include <cstring> #include <vector> #include <cmath&
Gt
#include <algorithm> #include <set> #include <cassert> #include <time.h> #include <queue> #include <map> #include <stack> #include <bitset> #include <string> #include <sstream> #

Define INF 0x3f3f3f3f #define PRINT (x) cout<<x<<endl;

using namespace Std;
    Template <class type> Type stringtonum (const string& str) {istringstream ISS (str);
    Type num;
    ISS >> num;    
return num;

}//====================================================== #define MAXN 205 int DP[MAXN][MAXN]; int Whlcs (char s1[],int len1,char s2[],int len2) {for (int. i=1;i<=len1;++i) {for (int j=1;j<=len2;++j
            ) {if (s1[i-1]==s2[j-1]) dp[i][j]=dp[i-1][j-1]+1;
        else Dp[i][j]=max (dp[i-1][j],dp[i][j-1]); }} RetuRN Dp[len1][len2];

    } int main () {freopen ("Input.txt", "R", stdin);

    Char S1[MAXN],S2[MAXN];
        while (scanf ("%s%s", S1,S2)!=eof) {int res = WHLCS (S1,strlen (S1), S2,strlen (S2));
    PRINT (RES);
} return 0; }

Of course, in the case of memory constraints can be used so-called "scrolling array", because when the substring is a row of brush, and the related rows will only be the previous row, so that only save 2 rows of good (alternating use).

int Whlcs (char s1[],int len1,char s2[],int len2) {

    int rowflag = 1;
    for (int i=1;i<=len1;++i) {for

        (int j=1;j<=len2;++j) {

            if (s1[i-1]==s2[j-1])
                dp[rowflag][j]=dp[! rowflag][j-1]+1;
            else
                Dp[rowflag][j]=max (dp[!rowflag][j],dp[rowflag][j-1]);
        }
        Rowflag =!rowflag;
    }
    return dp[!rowflag][len2];
}

AC Code

#include <iostream> #include <stdio.h> #include <cstring> #include <vector> #include <cmath&
Gt
#include <algorithm> #include <set> #include <cassert> #include <time.h> #include <queue> #include <map> #include <stack> #include <bitset> #include <string> #include <sstream> #

Define INF 0x3f3f3f3f #define PRINT (x) cout<<x<<endl;

using namespace Std;
    Template <class type> Type stringtonum (const string& str) {istringstream ISS (str);
    Type num;
    ISS >> num;    
return num; }//====================================================== #define MAXN 205 int Same (int a,int b) {return a==b?1:0 ; Equal return 1} int maxofthree (int a,int b,int c) {return (a>=b && a>=c)? A: ((B>=a && B&G
t;=c)? b:c);
} int DP[MAXN][MAXN];
    int Whlcs (char s1[],int len1,char s2[],int len2) {memset (dp,0,sizeof (DP));
for (int i=1;i<=len1;i++) {        for (int j=1;j<=len2;j++) {dp[i][j]=maxofthree (Dp[i-1][j-1]+same (s1[i-1],s2[j-1]), Dp[i-1][j], DP I [J-1]);
Here to simplify the judgment}} return dp[len1][len2];

    } int main () {//freopen ("Input.txt", "R", stdin);

    Char S1[MAXN],S2[MAXN];
        while (scanf ("%s%s", S1,S2)!=eof) {int res = WHLCS (S1,strlen (S1), S2,strlen (S2));
    PRINT (RES);
} return 0; }
Second, the problem analysis 1, DP

Often encounter complex problems can not be easily decomposed into several sub-problems, but will be decomposed a series of sub-problems. By simply using the method of decomposing large problem into sub-problem and synthesizing the solution of the sub-problem, the problem solving time will be increased according to the problem scale.

In order to save time for repeating the same sub-problem, an array is introduced, regardless of whether they are useful for the final solution, and the solution of all the sub-problems is stored in the array, which is the basic method used in the dynamic programming method. 2. Solving LCS

Introduce a two-dimensional array c[][], using c[i][j] to record the length of the LCS of X[i] and Y[j], B[i][j] record c[i][j] is calculated by the value of which sub-problem, in order to determine the direction of the search (if you need to record the path).

We do a recursive calculation from the bottom up, then c[i-1][j-1],c[i-1][j] and c[i][j-1] have been computed before calculating c[i,j]. At this point we can calculate c[i][j] According to x[i] = Y[j] or x[i]! = Y[j].

The problem is written in a recursive style:

The process of outputting the longest common sub-sequence:

3. Algorithm Analysis

Since each call moves at least one step up or to the left (or up to left), the maximum number of calls (M + N) is encountered when i = 0 or j = 0, which begins to return. Returns the same direction as the recursive call, with the same number of steps, so the algorithm has a time complexity of θ (M + N). References

[1] http://blog.csdn.net/yysdsyl/article/details/4226630

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.