河北省省赛重现赛-K Multiple Longest Commom Subsequence

2017: K Multiple Longest Commom Subsequence

描述

题目描述:

KK has two sequences, AAA and BBB, and wants to find the kkk multiple longest common subsequence.A sequence SSS is a kkk multiple common subsequence of AAA and BBB if and only if it satisfies the following conditions:

  1. SSS is a subsequence of AAA and is a subsequence of BBB. (A subsequence is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.)

  2. The length of SSS is t×kt\times kt×k where ttt is a nonnegative integer. The first element of SSS is S[1]S[1]S[1]. If we divide the sequence into ttt groups with the iiith group containing S[(i−1)×k+j](1≤j≤k)S[(i - 1) \times k + j] (1 \leq j \leq k)S[(i−1)×k+j](1≤j≤k), for every element ggg, it shares the same value with other elements that are in the same group which ggg belongs to.

For example, [1,1,2,2][1, 1, 2, 2][1,1,2,2] is a double common subsequence of [1,2,3,1,2,3,2][1, 2, 3, 1, 2, 3, 2][1,2,3,1,2,3,2] and [1,3,1,2,2][1, 3, 1, 2, 2][1,3,1,2,2]. KK wants to know the maximum length of such sequence.

输入:

The first line is an integer TTT, denoting the number of test cases.

For each test case, the first line are three integers kkk , nnn, mmm, denoting the kind of subsequence, the length of AAA and the length of BBB.

The second line are nnn integers A1∼AnA_1 \sim A_nA1​∼An​, representing the elements of AAA.

The third line are mmm integers B1∼BmB_1 \sim B_mB1​∼Bm​, representing the elements of BBB.

1≤T≤101 \leq T \leq 101≤T≤10 , 1≤k,n,m≤1031\leq k, n, m \leq 10^31≤k,n,m≤103, 1≤Ai,Bi≤1031\leq A_i, B_i \leq 10^31≤Ai​,Bi​≤103.

输出:

For each test case, output a line with the maximum length of kkk multiple common subsequence.

样例输入

3
1 4 5
1 2 3 4
4 1 3 2 4
2 8 7
1 1 2 2 3 3 4 4
1 2 3 1 2 3 3
3 9 9
1 1 1 2 2 2 3 3 3
1 2 3 1 2 3 1 2 3

样例输出

3
4
3

这题最重要的一点就是把 k个相同的数当成一个整体来看

 

题解:最长公共子序列(LCS),这个问题是要分第a[i]==b[i] 和 a[i]!=b[j],如果a[i]==b[i] 那么就要看是否有前 k个相同的a[i] ,和前k个相同的b[i],并分别记录最前面的那个下标为pa[i],和pb[i],如果pa[i]!=0 && pb[i]!=0 那么就一定存在这样的k个相同的数字,那么dp[i][j]就等于 dp[i-pa[i]][j-pb[j]];        否则,就是找不到这样k个相同的a[i] 和 k个相同的b[i]与其对应。所以,就和a[i]!=b[j]的情况一样了,就等于dp[i][j] = max(dp[i-1][j],dp[i][j-1]);

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<queue>
#include<string.h>
#define rep(i,a,b) for(int i=(a);i<=(b);++i)
using namespace std;
const int N = 1010;
int T,k,n,m;
int dp[N][N];
int a[N],b[N],pa[N],pb[N];
queue<int> q[N];
int main()
{

    int T; cin>>T;
    while(T--)
    {
        cin>>k>>n>>m;
        memset(dp,0,sizeof(dp));
        memset(pa,0,sizeof(pa));
        memset(pb,0,sizeof(pb));
        rep(i,1,n)cin>>a[i];
        rep(i,1,m) cin>>b[i];
        queue<int> q[1005];
        rep(i,1,n)
        {
            q[a[i]].push(i);
            if(q[a[i]].size()==k)
            {
                pa[i]=q[a[i]].front();
                q[a[i]].pop();
            }
        }
        rep(i,1,1000) while(!q[i].empty()) q[i].pop();
        rep(i,1,m)
        {
            q[b[i]].push(i);
            if(q[b[i]].size()==k)
            {
                pb[i]=q[b[i]].front();
                q[b[i]].pop();
            }
            else if(q[b[i]].size()==1) pb[i]=0;
        }
        rep(i,1,n)
            rep(j,1,m)
            {
                int ans=max(dp[i-1][j],dp[i][j-1]);
                if(a[i]==b[j] && pa[i]!=0 && pb[j]!=0)
                    ans=max(ans,dp[pa[i]-1][pb[j]-1]+k);
                dp[i][j]=max(dp[i][j],ans);
            }
        cout<<dp[n][m]<<endl;
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值