HDU 5495 LCS (and check set)

Source: Internet
Author: User

Problem descriptionyou is given and sequence{A1,a2,.. . ,an} and{B1,b2,.. . ,bn} . Both sequences is permutation of{1,2,.. . ,N} . You is going to find another permutation{P1,P2,.. . ,pn} such that the length of the LCS (longest common subsequence) of{AP1, Ap2,., Apn} and{BP1,bp2,< Span id= "mathjax-span-121" class= "Mo". ,bp n} is maximum.

Inputthere is multiple test cases. The first line of input contains an integerT, indicating the number of test cases. For each test case:

The first line contains an integern(1≤n≤5) -The length of the permutation. The second line containsNIntegersa1,a2,.. . ,an . The third line containsNIntegersb1,b2,.. . ,bn .

The sum of n in the test cases would not exceed 2x6.

Outputfor every test case, output the maximum length of lcs.  test instructions: give you two strings of numbers and then let you order the two sequences in this order after the LCS Max, and output the length of the LCS.   Because this problem of data large with LCS certainly not, enumeration scheme certainly not. However, since it is possible to move freely and the number is all the numbers in 1~n, the associated numbers can be calculated, and the maximum number of these associations can be composed of len-2 LCS sequences, for example. 6 1 5 3 2 6 4 3 6 2 4 5 1 associated can be obtained (1-3-2-4-1), (5-6-5) 1 3 2 4       5 2 4 1       6 5 so combined post LC S is for (5-2) + (3-2) =4 and if there are only two such as (5-5) This time add 1 on the line   
#include <iostream> #include <cstring> #include <cstdio>using namespace std;const int M = 1e5 + 10;int a[m        ], B[m];int f[m], va[m];void init (int n) {for (int i = 0; I <= N; i++) {f[i] = i;    Va[i] = 1;    }}int find (int x) {if (x = F[x]) f[x] = find (f[x]); return f[x];}    void Union (int x, int y) {int xx = find (x);    int yy = find (y);        if (xx! = yy) {f[x] = yy;    VA[YY] + = va[xx];    }}int Main () {int t;    scanf ("%d", &t);        while (t--) {int n;        scanf ("%d", &n);        Init (M);        for (int i = 0; i < n; i++) {scanf ("%d", &a[i]);            } for (int i = 0; i < n; i++) {scanf ("%d", &b[i]);        Union (A[i], b[i]);        } int count = 0; for (int i = 0; i < n; i++) {if (f[a[i]] = = A[i]) {if (va[a[i] = = 1) {C                ount++;                    } else {Count + = (Va[a[i]]-1);    }}} printf ("%d\n", count); } return 0;}

Hdu 5495 LCS (and set)

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.