Uva--111history Grading +DP

Source: Internet
Author: User

Test instructions

is to ask for the longest common subsequence of two sequences.

Ideas:

The input of this topic is very pit father, if the input understanding clearly, this topic is not difficult. The input of the topic indicates where the number in the position is placed, for example, the input is 1,3,2,4 its corresponding sequence should be 1,3,2,4;

Here are 2 code, one is the classic solution, and one is the code that I wrote today to turn the problem into the longest-way solution on a dag graph.


The code is as follows:


#include <iostream> #include <cstring> #include <cstdio>using namespace Std;int d[30],n,map[30][30];    int dp (int i) {if (d[i]>0) return d[i];    D[i]=1;            for (int j=1;j<=n;j++) if (Map[i][j]) {int T=DP (j) +1;        if (d[i]<t) d[i]=t; } return d[i];}    void Init () {memset (d,0,sizeof (d)); memset (map,0,sizeof (map));}    int main () {int i,j,a[30],b[30];         while (scanf ("%d", &n)!=eof) {int x;             for (i=1;i<=n;i++) {scanf ("%d", &x);         A[x]=i;             } while (scanf ("%d", &x)!=eof) {Init (); b[x]=1;                 for (i=2;i<=n;i++) {scanf ("%d", &x);             B[x]=i;                        } for (i=1;i<=n;i++) {for (j=1;j<=n;j++) if (B[i]==a[j])                 Break for (int k=i+1;k<=n;k++) for (inT d=j;d<=n;d++) if (b[k]==a[d]) map[i][k]=1;             } int ans=0;             for (i=1;i<=n;i++) Ans=max (ANS,DP (i));         printf ("%d\n", ans); }} return 0;}

#include <iostream> #include <cstring> #include <cstdio>using namespace Std;int main () {int i,j,a[30],    B[30],dp[30][30],n;         while (scanf ("%d", &n)!=eof) {int x;             for (i=1;i<=n;i++) {scanf ("%d", &x);         A[x]=i;             } while (scanf ("%d", &x)!=eof) {b[x]=1;                 for (i=2;i<=n;i++) {scanf ("%d", &x);             B[x]=i;           } memset (Dp,0,sizeof (DP));                          for (i=1;i<=n;i++) for (j=1;j<=n;j++) {if (A[i]==b[j])                     dp[i][j]=dp[i-1][j-1]+1;              else Dp[i][j]=max (dp[i-1][j],dp[i][j-1]);              } int ans=0;              for (i=1;i<=n;i++) for (j=1;j<=n;j++) Ans=max (Ans,dp[i][j]);         printf ("%d\n", ans); }} return 0;}


Uva--111history Grading +DP

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.