codeforces Round #440 A Search for Pretty Integers【hash/排序】
1 second
256 megabytes
standard input
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?
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.
Print the smallest pretty integer.
2 3
4 2
5 7 6
25
8 8
1 2 3 4 5 6 7 8
8 7 6 5 4 3 2 1
1
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/排序】的更多相关文章
- Codeforces Round #440 (Div. 2)【A、B、C、E】
Codeforces Round #440 (Div. 2) codeforces 870 A. Search for Pretty Integers(水题) 题意:给两个数组,求一个最小的数包含两个 ...
- Codeforces Round #440 (Div. 2, based on Technocup 2018 Elimination Round 2)
A. Search for Pretty Integers 题目链接:http://codeforces.com/contest/872/problem/A 题目意思:题目很简单,找到一个数,组成这个 ...
- 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 ...
- 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 ...
- 【Codeforces Round #440 (Div. 2) A】 Search for Pretty Integers
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 先枚举一个数字的情况. 再枚举两个数的情况就好. [代码] #include <bits/stdc++.h> #defi ...
- 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 ...
- 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 ...
- codeforces Educational Codeforces Round 5 A. Comparing Two Long Integers
题目链接:http://codeforces.com/problemset/problem/616/A 题目意思:顾名思义,就是比较两个长度不超过 1e6 的字符串的大小 模拟即可.提供两个版本,数组 ...
- 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 ...
随机推荐
- ES 1.7安装ik分词elasticsearch-analysis-ik-1.2.5中文同义词实现
ElasticSearch 中文同义词实现 https://blog.csdn.net/xsdxs/article/details/52806499 参考以下两个网址,但运行报错,以下是我自己改进方式 ...
- NAPT 分为锥型(Cone)和 对称型(Symmetric)
NAPT 分为锥型(Cone)和 对称型(Symmetric) 链接:https://www.zhihu.com/question/38729355/answer/86531260 实际上大部运营商提 ...
- 【ZJ选讲·字符串折叠】
给一个字符串(len<=100) 把这个字符串折叠(就是压缩) 记 X(子串) 表示重复 X次该子串 比如 3(orz) orzorzorz 来点神奇例子: AAAAAAAAAA ...
- intellij IDEA与springboot项目建立
概念问题: IntelliJ系中的Project相当于Eclipse系中的workspace.IntelliJ系中的Module相当于Eclipse系中的Project.IntelliJ中一个Proj ...
- oracle与mysql与sqlserver的分页
假设当前是第PageNo页,每页有PageSize条记录,现在分别用Mysql.Oracle和SQL Server分页查询student表. 1.Mysql的分页查询: 1 SELECT 2 * 3 ...
- BS架构下使用消息队列的工作流程
异步通信 对于BS(Browser-Server 浏览器)架构,很多情景下server的处理时间较长. 如果浏览器发送请求后,保持跟server的连接,等待server响应,那么一方面会对用户的体验有 ...
- jquery处理鼠标左中右键事件
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Html 让文字显示在图片的上面
如题: 第一种方式便是将 image 作为背景图片,即:background-image:url("......."); 在此可以控制背景图片的横向和纵向的平铺: backgrou ...
- 打砖块(codevs 1257)
题目描述 Description 在一个凹槽中放置了n层砖块,最上面的一层有n块砖,第二层有n-1块,……最下面一层仅有一块砖.第i层的砖块从左至右编号为1,2,……i,第i层的第j块砖有一个价值a[ ...
- C++ Review
#include "iostream" #include "iomanip" #include "cstdio" using namespa ...