UVA 111 History Grading "LCS"

Source: Internet
Author: User

Brief Description:

A history exam, there are n historical events, and the years between them are different, and students are asked to arrange them in the correct order. There are two ways to score, using the second: Assuming that there are historical events 1,2,3,4, their correct chronological order is 1,2,3,4, and then assuming the student's answer is 1,3,2,4, then according to the correct number of relative order, answer three (1,2,4 or 1,3,4), That is, the longest common subsequence length with the correct answer is 3, which is the number of answers.

Analyse:

The longest common sub-sequence template problem, but the input is a big hole, his input is in order, Event 1 is ranked in the first few, Event 2 is ranked in the number of ..., so you have to first convert the input into the correct order.


CODE:

#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include < algorithm> #include <vector> #include <string> #include <queue> #include <deque> #include <stack> #include <map> #include <set> #define INF 0x7fffffff#define SUP 0x80000000#define Mem (A, B) memset (A,b,sizeof (a)) using namespace Std;typedef long long ll;const int n=100007;int st[22],t[22];int dp[22][22];int    Main () {int n;    int id;    scanf ("%d", &n);        for (int i=1;i<=n;i++) {scanf ("%d", &id);    St[id]=i;        } while (scanf ("%d", &id) ==1) {t[id]=1;            for (int i=2;i<=n;i++) {scanf ("%d", &id);        T[id]=i;        } mem (dp,0);                    for (int i=1;i<=n;i++) {for (int j=1;j<=n;j++) {if (St[i]==t[j])                dp[i][j]=dp[i-1][j-1]+1; else {Dp[i][j]=max (Dp[i][j],max (dp[i-1][J],DP[I][J-1]));    }}} printf ("%d\n", Dp[n][n]); } return 0;}


UVA 111 History Grading "LCS"

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.