A typical DP water question-the longest common subsequence POJ 2250

Source: Internet
Author: User

I did not write this article because I think there are too few materials for the longest public subsequence,

It was such a familiar problem that I finally found the bug after WA all morning,

Lessons learned from the morning's pain: as a programmer, even if you are familiar with simple problems, you must keep strict logic at all times.

Let's start with the code with a bug.

#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>#define N 200using namespace std;string sa[N],sb[N],str;int dp[N][N],ans[N];struct node{    int pa,pb;}pre[N][N];int main(){    //freopen("8.in","r",stdin);    while(cin>>str) {        int la=0;        int lb=0;        sa[++la]=str;        while(cin>>str) {            if(str[0]=='#') break;            sa[++la]=str;        }        while(cin>>str) {            if(str[0]=='#') break;            sb[++lb]=str;        }        memset(dp,0,sizeof(dp));        for(int i=1;i<=la;i++) {            for(int j=1;j<=lb;j++) {                int a=dp[i-1][j];                int b=dp[i][j-1];                int inc=0;                if(sa[i]==sb[j]) inc=1;                int c=dp[i-1][j-1]+inc;                if(a>b && a>c) {                    dp[i][j]=a;                    pre[i][j].pa=i-1;                    pre[i][j].pb=j;                }                else if(b>a && b>c) {                    dp[i][j]=b;                    pre[i][j].pa=i;                    pre[i][j].pb=j-1;                }                else {                    dp[i][j]=c;                    pre[i][j].pa=i-1;                    pre[i][j].pb=j-1;                }            }        }        int p1,p2,cnt=0;        for(p1=la,p2=lb;p1>0 && p2>0;){            int np1=pre[p1][p2].pa;            int np2=pre[p1][p2].pb;            if(sa[p1]==sb[p2]) ans[cnt++]=p1;            p1=np1;            p2=np2;        }        //cout<<dp[la][lb]<<endl;        sort(ans,ans+cnt);        for(int i=0;i<cnt-1;i++) cout<<sa[ans[i]]<<" ";        cout<<sa[ans[cnt-1]]<<endl;    }    return 0;}


AC code:

# Include <cstdio> # include <cstring> # include <iostream> # include <algorithm> # define N 200 using namespace std; string sa [N], sb [N], str; int dp [N] [N], ans [N]; struct node {int pa, pb;} pre [N] [N]; int main () {// freopen ("8.in"," r ", stdin); while (cin> str) {int la = 0; int lb = 0; sa [++ la] = str; while (cin> str) {if (str [0] = '#') break; sa [++ la] = str;} while (cin> str) {if (str [0] = '#') break; sb [++ lb] = str;} memset (dp, 0, sizeof (dp); for (int I = 1; I <= la; I ++) {for (int j = 1; j <= lb; j ++) {int a = dp [I-1] [j]; int B = dp [I] [J-1]; int inc = 0; if (sa [I] = sb [j]) inc = 1; int c = dp [I-1] [J-1] + inc; // The problem lies in the following if-else statements: if (c> a & c> B) {dp [I] [j] = c; pre [I] [j]. pa = I-1; pre [I] [j]. pb = J-1;} else if (B> a & B> c) {dp [I] [j] = B; pre [I] [j]. pa = I; pre [I] [j]. pb = J-1;} else {dp [I] [j] = a; pre [I] [j]. pa = I-1; pre [I] [j]. pb = j ;}} int p1, p2, cnt = 0; for (p1 = la, p2 = lb; p1> 0 & p2> 0 ;) {int np1 = pre [p1] [p2]. pa; int np2 = pre [p1] [p2]. pb; if (sa [p1] = sb [p2] & p1> np1 & p2> np2) ans [cnt ++] = p1; p1 = np1; p2 = np2;} sort (ans, ans + cnt); for (int I = 0; I <cnt-1; I ++) cout <sa [ans [I] <"; cout <sa [ans [cnt-1] <endl;} return 0 ;}


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.