解题思路:给出两个数列an,bn,其中an,bn中元素的顺序可以任意改变,求an,bn的LCS

因为数列中的元素可以按任意顺序排列,所以只需要求出an,bn中的元素有多少个是相同的即可。

反思:一开始以为就是求LCS,一直WA,后来才发现可以按任意顺序排列元素,把相同的元素都排在一起,就是最长的子序列。

Puzzle


Time Limit: 2 Seconds      Memory Limit: 65536 KB

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 <stdio.h>
int a[10010],b[10010];
void sort(int a[],int n)
{
int i,j,t;
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
}
int main()
{
int n,m,i,j,num,k;
while(scanf("%d %d",&n,&m)!=EOF)
{
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<m;i++)
scanf("%d",&b[i]);
sort(a,n);
sort(b,m);
num=0;
k=0; for(i=0;i<n;i++)
{
for(j=k;j<m;j++)
{
if(a[i]==b[j])
{
num++;
k=j+1;
break;
}
}
}
printf("%d\n",num);
}
return 0;
}

  

ZOJ 3019 Puzzle的更多相关文章

  1. ZOJ 2610 Puzzle 模拟

    大模拟:枚举6个方向.检查每一个0是否能移动 Puzzle Time Limit: 2 Seconds      Memory Limit: 65536 KB Little Georgie likes ...

  2. ZOJ 1602 Multiplication Puzzle(区间DP)题解

    题意:n个数字的串,每取出一个数字的代价为该数字和左右的乘积(1.n不能取),问最小代价 思路:dp[i][j]表示把i~j取到只剩 i.j 的最小代价. 代码: #include<set> ...

  3. ZOJ 3435 Ideal Puzzle Bobble

    ZOJ Problem Set - 3435 Ideal Puzzle Bobble Time Limit: 2 Seconds      Memory Limit: 65536 KB Have yo ...

  4. Multiplication Puzzle ZOJ - 1602

    Multiplication Puzzle ZOJ - 1602 传送门 The multiplication puzzle is played with a row of cards, each c ...

  5. [ZOJ]3541 Last Puzzle (区间DP)

    ZOJ 3541 题目大意:有n个按钮,第i个按钮在按下ti 时间后回自动弹起,每个开关的位置是di,问什么策略按开关可以使所有的开关同时处于按下状态 Description There is one ...

  6. [ZOJ 2836] Number Puzzle

    Number Puzzle Time Limit: 2 Seconds      Memory Limit: 65536 KB Given a list of integers (A1, A2, .. ...

  7. ZOJ 3541 The Last Puzzle(经典区间dp)

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3541 题意:有一排开关,有个开关有两个值t和d,t是按下开关后在t秒后会自 ...

  8. ZOJ 3814 Sawtooth Puzzle (2014年牡丹江赛区网络赛F题)

    1.题目描写叙述:点击打开链接 2.解题思路:本题是一道隐式图的搜索题目.一般来说,这类题目首先要定义状态,接下来是弄清楚状态怎样转移,以及状态怎样判重,怎样推断当前状态是否和目标状态同样.至于求解最 ...

  9. ZOJ 3435 Ideal Puzzle Bobble 莫比乌斯反演

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4119 依然是三维空间内求(1,1,1)~(a,b,c)能看到的整点数,平移一下 ...

随机推荐

  1. spring4+hibernate4+struts2环境搭建

    tomact配置请查看下面的文章 javaEE_maven_struts2_tomcat_first http://www.cnblogs.com/luotuoke/p/4543686.html po ...

  2. Discuz 3x 配置问题

    1.注意config里面配置的路径 2.注意ucenter里面 的密钥要一直 3.IP 的选择

  3. poj 3669 bfs(这道题隐藏着一个大坑)

    题意 在x,y坐标系,有流星会落下来,给出每颗流星落下来的坐标和时间,问你能否从(0,0)这个点到一个安全的位置.所谓的安全位置就是不会有流星落下的位置. 题解: 广搜,但是这里有一个深坑,就是搜索的 ...

  4. http请求后台报406错误

    1.springMVC的项目,通过浏览器访问后台方法遇到了报406的错误,找了很多原因,最终发现是因为缺少spring-mvc的json配置. 2.添加依赖:jackson-databind.jack ...

  5. 洛谷P1540 机器翻译 水题 模拟

    注意一下细节,尤其是更新minv时不要更新错. Code: #include<vector> #include<iostream> #include<cstdio> ...

  6. liunx 里面安装phpstudy环境s

    ngixwget -c http://lamp.phpstudy.net/phpstudy.bin  chmod +x phpstudy.bin    #权限设置 ./phpstudy.bin #运行 ...

  7. Problem 6

    Problem 6 # Problem_6.py """ The sum of the squares of the first ten natural numbers ...

  8. python第二周:数据类型、列表、字典

    1.模块初识: sys模块: import sys print(sys.path) #path打印环境变量--> ['F:\\python学习\\projects\\s14\\day2', 'F ...

  9. 2018 MAC下安装Redis和Redis可视化工具RDM并连接Redis

    实验环境:一台mac V:10.13.6 一.安装redis brew install redis 二.安装RDM 直接下载安装rdm dmg文件 链接: https://pan.baidu.com/ ...

  10. 免费ftp服务器FileZilla Server配置

    FileZilla Server下载安装完成后,必须启动软件进行设置,由于此软件是英文,本来就是一款陌生的软件,再加上英文,配置难度可想而知,小编从网上找到一篇非常详细的教程进行整理了一番,确保读到这 ...