河北省省赛重现赛-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:
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.)
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;
}
河北省省赛重现赛-K Multiple Longest Commom Subsequence相关推荐
- 2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 F Color it
链接:https://www.nowcoder.com/acm/contest/163/F 来源:牛客网 2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 F Color it 时间限制:C ...
- 2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 J Beautiful Numbers
传送门 题意:问你从[1,N]有多少个数能被自身的SOD(sum of digits)整除 题解:数位dp,枚举SOD,因为最多只有12位,所以只要枚举1到12*9,一维记录pos,二维记录当前剩余要 ...
- 最长公共子序问题 ( LCS, Longest Commom Subsequence )
Common Subsequence 时间限制: 1 Sec 内存限制: 128 MB 题目描述 给定序列的子序列是将给定的序列的一些元素(可能没有)省去.给定一个序列X = <x1, x2, ...
- 最长公共子序问题 ( LCS, Longest Commom Subsequence )POJ-1458
A subsequence of a given sequence is the given sequence with some elements (possible none) left out. ...
- leetcode 1143. Longest Commom Subsequence 最长公共子序列(中等)
一.题目大意 标签: 动态规划 https://leetcode.cn/problems/longest-common-subsequence 给定两个字符串 text1 和 text2,返回这两个字 ...
- Dream_Chaser队训练赛第一场 K题
Dream_Chaser队训练赛第一场 K题 题目来自2012成都区域赛 K - Yet Another Multiple Problem Time Limit:20000MS Memory ...
- 北京信息科技大学第十三届程序设计竞赛暨ACM选拔赛(重现赛)题解
题目链接: 北京信息科技大学第十三届程序设计竞赛暨ACM选拔赛(重现赛)_ACM/NOI/CSP/CCPC/ICPC算法编程高难度练习赛_牛客竞赛OJ大学ACM校赛新生赛是面向ACM/ICPC/CCP ...
- 第 45 届国际大学生程序设计竞赛(ICPC)亚洲区域赛(济南)(重现赛)
第 45 届国际大学生程序设计竞赛(ICPC)亚洲区域赛(济南)(重现赛) 导语 涉及的知识点 题目 C D G J M 参考文献 导语 日常的队内集训,开始的时候状态其实很好,但是到了后两题就出现了 ...
- 广东工业大学文远知行杯新生程序设计竞赛(重现赛)复习
广东工业大学文远知行杯新生程序设计竞赛(重现赛) 1,F,亚子和燐子的game 思路: 我们可以维护一个堆(里面的数都相等,其实就是一个数),每扔进去一个数,就继续维持堆中数字相等(维持方法,就是谁大 ...
最新文章
- mysql5.7gtid_MySQL5.7 GTID 运维实战
- 学习响应式BootStrap来写融职教育网站,要是踩到坑就找我。
- system.gc会立即执行垃圾回收吗_JVM基础到实战03-垃圾回收概念
- C/C++中rand()函数产生随机数的用法
- 前端学习(2606):vue简单叙述
- 大数据应用时代来袭 SaaS走向没落?
- loc与iloc函数的使用
- 博客园修改TinyMCE编辑器为Markdown编辑器的方法
- 第15章-使用远程服务
- 广东高等学校计算机水平考试准考证打印,广东高考准考证打印系统
- JDK 的 ORACLE 官网下载步骤
- 数据治理-数据质量-数据质量参考架构
- 关于 Cannot assign requested address 错误
- 获取手机状态栏的高度
- 引力模型-高维固定效应面板泊松模型
- IAP跟成就系统的思路
- 终端定制行业分销初步设计
- 音视频开发(一):三种方式绘制图片
- 收藏!YOLOv4全文解读与翻译总结!(附思维导图和论文译文)
- 双系统windows7重做系统后修复linux引导