解题思路:给出两个数列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. 这里介绍两种将矩阵写入TXT文件的方法。

    方法1 fid = fopen('data.txt','wt'); % data.txt为写入文件名 matrix = M; % M为要存储的矩阵 [m,n]=size(matrix); for i= ...

  2. 采用requests库构建简单的网络爬虫

    Date: 2019-06-09 Author: Sun 我们分析格言网 https://www.geyanw.com/, 通过requests网络库和bs4解析库进行爬取此网站内容. ​ 项目操作步 ...

  3. luogu P2634 [国家集训队]聪聪可可 点分治

    Description 聪聪和可可是兄弟俩,他们俩经常为了一些琐事打起来,例如家中只剩下最后一根冰棍而两人都想吃.两个人都想玩儿电脑(可是他们家只有一台电脑)……遇到这种问题,一般情况下石头剪刀布就好 ...

  4. ES2015 模板字符串 ``

    js中类似`${xx,yy}`的语句是什么意思? `string` 是模板字符串,ES2015新增的符号. var x = 'a', y = 'b'; var z = `${x,y}`; //'b' ...

  5. git 教程2 (git常用命令解说)

    <1>$ git -- help (调出git的帮助文档) <2>$ git +命令 --help (查看某个具体命令的帮助文档) <3>$ git --versi ...

  6. HDU-3015 Disharmony Trees [数状数组]

    Problem Description One day Sophia finds a very big square. There are n trees in the square. They ar ...

  7. Django 连接MySQL 通过pymysql 库

    ython3 如何安装pymysql 库,在此不再做多的讲解,如果有想知道如何安装的朋友,请求参考下面的连接地址: 第一步:应用setting.py 文件设置mysql 数据库连接相关属性.   DA ...

  8. 洛谷 P1567 统计天数

    题目背景 统计天数 题目描述 炎热的夏日,KC非常的不爽.他宁可忍受北极的寒冷,也不愿忍受厦门的夏天.最近,他开始研究天气的变化.他希望用研究的结果预测未来的天气. 经历千辛万苦,他收集了连续N(1& ...

  9. Hibernate Session操作

    1.增加 @Test public void add(){ Configuration cfg=new Configuration().configure(); SessionFactory fact ...

  10. Python编程:从入门到实践 - matplotlib篇 - Random Flow

    随机漫游 # random_flow.py 随机漫游 import random class RandomFlow(): """一个生成随机漫游数据的类"&qu ...