ZOJ 3019 Puzzle

Source: Internet
Author: User

Problem-solving ideas: Give two series An,bn, in which the order of elements in an,bn can be arbitrarily changed, to seek an,bn LCS

Since the elements in the sequence can be arranged in any order, it is only necessary to ask how many of the elements in the an,bn are the same.

Reflection: At first thought is to seek LCS, has been WA, later found that you can arrange the elements in any order, the same elements are all together, is the longest sub-sequence.

Puzzle Time limit: 2 Seconds Memory Limit: 65536 KB

For sequences of integers a and B, if you can make the and the other sequences the same by deleting some elements in A and B, we cal L The remaining sequence "the common sub sequence". And we call the longest one the LCS.

Now is given, sequences of integers a and b. You can arrange elements in a and b in any order. You is to calculate the max length of the LCS for each arrangement of a and b.

Input

Input would consist of multiple test cases. The first line of each case is a integers N (0 < n < 10000), M (0 < M < 10000) indicating the length of a and b. The second line was N 32-bit signed integers in a. The third line was M 32-bit signed integers in b.

Output

Each case one line. The max length of the LCS of each arrangement of a and b.

Sample Input

5 41 2 3 2 11 4 2 1

Sample Output

3

#include <stdio.h>  int a[10010],b[10010];void sort (int a[],int n) {int i,j,t;for (i=0;i<n-1;i++) {for (j=i+ 1;j<n;j++) {if (A[i]>a[j]) {t=a[i];a[i]=a[j];a[j]=t;}}}} int main () {int n,m,i,j,num,k;while (scanf ("%d%d", &n,&m)!=eof) {for (i=0;i<n;i++) scanf ("%d", &a[i]); for (i=0;i<m;i++) scanf ("%d", &b[i]); sort (a,n); sort (b,m); Num=0;k=0;for (i=0;i<n;i++) {for (j=k;j<m;j++) {if (A[i]==b[j]) {num++;k=j+1;break;    }}} printf ("%d\n", num);} return 0;}

  

ZOJ 3019 Puzzle

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.