ZOJ Problem Set - 3019 Puzzle
来源:互联网 发布:微服务架构 数据库 编辑:程序博客网 时间:2024/05/15 09:54
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 andb.
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 41 2 3 2 11 4 2 1
Sample Output
3
Author: ZHUANG, Junyuan
Source: ZOJ Monthly, August 2008
分析:
开始看到LCS(最长公共子序列)还以为要用dp,仔细一看,原来You can arrange elements in a and b in any order.(a,b中元素的顺序可以任意排),这样问题就更简单了。直接sort后进行模拟。
ac代码:
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<algorithm>
using namespace std;
#define M 10001
#define N 10001
int a[M],b[N];
int main(){
int x,y,i,j,n;
while(cin>>x>>y)
{
for(i=0;i<x;i++){
cin>>a[i];
}
for(j=0;j<y;j++){
cin>>b[j];
}
sort(a,a+x);
sort(b,b+y);
n=0,i=j=0;
while(i!=x&&j!=y){
if(a[i]==b[j]){
i++;
j++;
n++;
}
else if(a[i]<b[j]) i++;
else j++;
}
cout<<n<<endl;
}
return 0;
}
- ZOJ Problem Set - 3019 Puzzle
- ZOJ Problem Set - 2060
- ZOJ Problem Set - 2972
- ZOJ Problem Set - 1037
- ZOJ Problem Set - 1048
- ZOJ Problem Set - 1049
- ZOJ Problem Set - 1051
- ZOJ Problem Set - 1067
- ZOJ Problem Set - 1115
- ZOJ Problem Set - 1151
- ZOJ Problem Set - 1205
- ZOJ Problem Set - 1025
- ZOJ Problem Set - 1029
- ZOJ Problem Set - 1076
- ZOJ Problem Set - 1117
- ZOJ Problem Set - 1041
- ZOJ Problem Set - 1004
- ZOJ Problem Set - 1005
- 数字谜2 C实现
- 如何解决window下VP9的VS版本遇到的LNK问题
- windows下cocos2dx 2.2.x编译安卓工程
- 为mysql数据库建立索引
- oracle数据库体系架构详解
- ZOJ Problem Set - 3019 Puzzle
- swap过高的问题解决
- 解决格式化字符输入的困扰--Android
- web 开发
- 菜鸟学JS(五)——window.onload与$(document).ready()
- Lua 学习笔记:沙盒
- 浅谈erlang游戏服务器项目--英雄远征服务启动流程
- 如何配置eclipse/zend studio的代码自动提示功能
- Ubuntu 修改时区和时间