A. Search for Pretty Integers
【题目链接】:http://codeforces.com/contest/872/problem/A
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

You are given two lists of non-zero digits.

Let's call an integer pretty if its (base 10) representation has at least one digit from the first list and at least one digit from the second list. What is the smallest positive pretty integer?

Input

The first line contains two integers n and m (1 ≤ n, m ≤ 9) — the lengths of the first and the second lists, respectively.

The second line contains n distinct digits a1, a2, ..., an (1 ≤ ai ≤ 9) — the elements of the first list.

The third line contains m distinct digits b1, b2, ..., bm (1 ≤ bi ≤ 9) — the elements of the second list.

Output

Print the smallest pretty integer.

Examples
input
2 3
4 2
5 7 6
output
25
input
8 8
1 2 3 4 5 6 7 8
8 7 6 5 4 3 2 1
output
1
Note

In the first example 25, 46, 24567 are pretty, as well as many other integers. The smallest among them is 25. 42 and 24 are not pretty because they don't have digits from the second list.

In the second example all integers that have at least one digit different from 9 are pretty. It's obvious that the smallest among them is 1, because it's the smallest positive integer.

【题意】:给定2个数组,求一个最小数,满足里面含有2个数组中至少一个数。注意特判俩个数组中含有相等的数的求看!

【分析】:方法一:分别hash一下每个数组的数,出现的标记为1,如果某个数在两个数组同时标记为1,说明在两个数组内都出现过,直接输出该数(如果两个数组出现同一个数字,纵使不是里面最小的,也要单独输出)。还要把两个数组内最小的mina和minb用递归找出来,再分别比较两者,最小放前,稍大置后。

方法二:排序。不用标记,直接升序排序。先判断两数组是否出现同一个数(二层循环枚举一下),否则分别将两数组第一个(最小)数记录,比较一下大小,小的在前大的在后。

【代码】:

#include <bits/stdc++.h>

using namespace std;
int main()
{
int n,m,ma=,mb=;
int x,y,a[],b[];//或者不用int,用bool
memset(a,,sizeof(a));
memset(b,,sizeof(b));//hash数组注意清0,否则结果错误
cin>>n>>m;
for(int i=;i<=n;i++)
{
cin>>x;
a[x]=;
ma=min(x,ma);
}
for(int i=;i<=m;i++)
{
cin>>y;
b[y]=;
mb=min(y,mb);
}
for (int i=;i<=;i++)
{
if (a[i] && b[i])
{
cout<<i<<endl;
return ;//真的好用,不能是break,否则还会顺序执行下去,会是不正确的输出
}
}
cout<<min(ma,mb)<<max(ma,mb)<<endl; return ;
}

hash

#include<bits/stdc++.h>

using namespace std;

int main()
{
int n,m,a[],b[];
while(cin>>n>>m)
{
for(int i=;i<=n;i++)
cin>>a[i];
sort(a+,a+n+); for(int i=;i<=m;i++)
cin>>b[i];
sort(b+,b+m+); for(int i=;i<=n;i++)
{
for(int j=;j<=m;j++)
{
if(a[i]==b[j])
{
cout<<a[i]<<endl;
return ;
}
}
}
int ma=a[];
int mb=b[];
int minx=min(ma,mb);
int maxx=max(ma,mb);
cout<<minx<<maxx<<endl;
}
}

sort

codeforces Round #440 A Search for Pretty Integers【hash/排序】的更多相关文章

  1. Codeforces Round #440 (Div. 2)【A、B、C、E】

    Codeforces Round #440 (Div. 2) codeforces 870 A. Search for Pretty Integers(水题) 题意:给两个数组,求一个最小的数包含两个 ...

  2. Codeforces Round #440 (Div. 2, based on Technocup 2018 Elimination Round 2)

    A. Search for Pretty Integers 题目链接:http://codeforces.com/contest/872/problem/A 题目意思:题目很简单,找到一个数,组成这个 ...

  3. Codeforces Round #440 (Div. 2) A,B,C

    A. Search for Pretty Integers time limit per test 1 second memory limit per test 256 megabytes input ...

  4. ACM-ICPC (10/15) Codeforces Round #440 (Div. 2, based on Technocup 2018 Elimination Round 2)

    A. Search for Pretty Integers You are given two lists of non-zero digits. Let's call an integer pret ...

  5. 【Codeforces Round #440 (Div. 2) A】 Search for Pretty Integers

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 先枚举一个数字的情况. 再枚举两个数的情况就好. [代码] #include <bits/stdc++.h> #defi ...

  6. Codeforces Educational Codeforces Round 5 A. Comparing Two Long Integers 高精度比大小,模拟

    A. Comparing Two Long Integers 题目连接: http://www.codeforces.com/contest/616/problem/A Description You ...

  7. Codeforces Round #440 (Div. 2, based on Technocup 2018 Elimination Round 2) D. Something with XOR Queries

    地址:http://codeforces.com/contest/872/problem/D 题目: D. Something with XOR Queries time limit per test ...

  8. codeforces Educational Codeforces Round 5 A. Comparing Two Long Integers

    题目链接:http://codeforces.com/problemset/problem/616/A 题目意思:顾名思义,就是比较两个长度不超过 1e6 的字符串的大小 模拟即可.提供两个版本,数组 ...

  9. Educational Codeforces Round 5 A. Comparing Two Long Integers

    A. Comparing Two Long Integers time limit per test 2 seconds memory limit per test 256 megabytes inp ...

随机推荐

  1. CSS优化压缩

    顾名思义缩写有简写意思,那就总结一下CSS缩写知识点.为什么要让CSS属性缩写?1.简化代码.一些CSS属性简写可以减少CSS代码从而减少CSS文件的占用字节.加快网页下载速度和网页加载速度.2.优化 ...

  2. P1404 平均数

    题目描述 给一个长度为n的数列,我们需要找出该数列的一个子串,使得子串平均数最大化,并且子串长度>=m. 输入输出格式 输入格式: N+1行, 第一行两个整数n和m 接下来n行,每行一个整数a[ ...

  3. AtCoder Grand Contest 025 Problem D - Choosing Points

    题目大意:输入$n,d1,d2$,你要找到$n^2$个整点 x, y 满足$0 \leqslant x, y<2n$.并且找到的任意两个点距离,既不是$\sqrt{d1}$,也不是 $\sqrt ...

  4. C++——编程常见错误

    C++库函数 C++标准库比C标准库要复杂很多,需要大家认真学习.C++标准库建立时间较晚,解决了C标准库的一些问题.通过认真学习.熟练掌握会对代码质量的提高有一定帮助. 一些建议: 1. 尽量使用迭 ...

  5. 几个JQuery解析XML的程序例子

    用JavaScript解析XML数据是常见的编程任务,JavaScript能做的,JQuery当然也能做.下面我们来总结几个使用JQuery解析XML的例子. 第一种方案: <script ty ...

  6. github导入springboot maven项目

    1.在GitHub里force喜欢的项目,获取GitHub项目地址,eclipse---import---project from git---clone uri---next---finish,项目 ...

  7. spring中Constructor、@Autowired、@PostConstruct的顺序【转】

    其实从依赖注入的字面意思就可以知道,要将对象p注入到对象a,那么首先就必须得生成对象p与对象a,才能执行注入.所以,如果一个类A中有个成员变量p被@Autowired注解,那么@Autowired注入 ...

  8. 压缩跟踪Compressive Tracking

    好了,学习了解了稀疏感知的理论知识后,终于可以来学习<Real-Time Compressive Tracking>这个paper介绍的感知跟踪算法了.自己英文水平有限,理解难免出错,还望 ...

  9. myisam_sort_buffer_size vs sort_buffer_size

    Q: I am MySQL on server with 6GB RAM. I need to know what is the difference between myisam_sort_buff ...

  10. Codeforces Round #523 (Div. 2) A. Coins

    A. Coins 题目链接:https://codeforc.es/contest/1061/problem/A 题意: 给出n和s,要在1-n中选数(可重复),问最少选多少数可以使其和为s. 题解: ...