码迷,mamicode.com
首页 > 其他好文 > 详细

ZOJ 3019 Puzzle

时间:2014-08-11 18:00:02      阅读:188      评论:0      收藏:0      [点我收藏+]

标签:os   io   strong   for   ar   line   amp   ios   

 

For sequences of integers a and b, if you can make the two sequences the same by deleting some elements in a and b, we call the remaining sequence "the common sub sequence". And we call the longest one the LCS.

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

Input

Input will consist of multiple test cases. The first line of each case is two integers N(0 < N < 10000), M(0 < M < 10000) indicating the length of a and b. The second line is N 32-bit signed integers in a. The third line is 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 4
1 2 3 2 1
1 4 2 1

Sample Output

3


公共序列最长(乱序)


排序,然后找就行



#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
#define N 100005

int a[N],b[N];

int main()

{
    int aa,bb,i;
    while(~scanf("%d%d",&aa,&bb))
    {
        for(i=0;i<aa;i++)
            scanf("%d",&a[i]);

        for(i=0;i<bb;i++)
            scanf("%d",&b[i]);

        sort(a,a+aa);
        sort(b,b+bb);

        int ans=0;
        int j=0,temp=0;//temp是重点

        for(i=0;i<aa;i++)
            for(j=temp;j<bb;j++)
            if(a[i]==b[j])
        {
            ans++;
            temp=j+1;
            break;
        }

        printf("%d\n",ans);
    }
    return 0;
}



ZOJ 3019 Puzzle,布布扣,bubuko.com

ZOJ 3019 Puzzle

标签:os   io   strong   for   ar   line   amp   ios   

原文地址:http://blog.csdn.net/u014737310/article/details/38493955

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!