UVa 531 Compromise:dp&lcs

Source: Internet
Author: User
Tags printf time limit

531-compromise

Time limit:3.000 seconds

Http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem &problem=472

Note Use a global variable flag to determine whether to output spaces.

Complete code:

/*0.029s*/#include <bits/stdc++.h> using namespace std;  
Char a[105][35], b[105][35];  
int dp[105][105], path[105][105];  
    BOOL first;///It is recommended that you adjust the output of a space with a global variable void Printlcs (int i, int j) {if (i = = 0 | | j = = 0) return;  
        if (path[i][j] = = 1) {Printlcs (i-1, j-1);  
            if (a) {printf ("%s", A[i]);  
        i = false;  
    else printf ("%s", A[i]);  
    else if (path[i][j] = = 2) Printlcs (I-1, J);  
else Printlcs (i, j-1);  
    int main () {int Lena, LenB, I, J; while (~SCANF ("%s", A[1])) {if (a[1][0]!= ' # ') for (Lena = 2; scanf ("%s", A[lena)), A[lena ][0]!= ' # ';  
        ++lena);  
        for (LenB = 1; scanf ("%s", B[lenb)), b[lenb][0]!= ' # '; ++lenb ';  
        memset (DP, 0, sizeof (DP));  
     for (i = 1; i < Lena. ++i) for (j = 1; j < LenB; ++j)       {if (strcmp (A[i], b[j]) = = 0) Dp[i][j] = Dp[i-1][j-1] + 1, path[i][j]  
                = 1;  
                else if (Dp[i-1][j] >= dp[i][j-1]) dp[i][j] = Dp[i-1][j], path[i][j] = 2;  
            else dp[i][j] = dp[i][j-1], path[i][j] = 3;  
        } A = true;  
        Printlcs (Lena-1, lenb-1);  
    Putchar (10);  
return 0; }

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

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.