ZOJ 3019 Puzzle

本文探讨了在给定两个整数序列的情况下,通过重新排列序列元素以找到它们的最长公共子序列的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

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;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值