A. Search for pretty integers "topic link": Http://codeforces.com/contest/872/problem/Atime limit per test1 secondmemory limit Per test256 megabytesinputstandard inputoutputstandard output
You are given and lists of non-zero digits.
Let's call a integer pretty if its (base) representation have at least one digit from the first list and at least O NE digit from the second list. What is the smallest positive pretty integer?
Input
The first line contains 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, ..., a c15>n (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.
Examplesinput
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, 24567 is pretty, as well as many and other integers. The smallest among them is 25. pretty because they don ' t has digits from the second list.
The second example all integers, that has at least one digit different from 9 is pretty. It's obvious that the smallest among them is 1, because it's the smallest positive integer.
"Test Instructions": Given 2 arrays, find a minimum number that satisfies at least one of the 2 arrays contained within. Note that the two arrays contain equal numbers for the sake of looking!
"Analysis": Method one: Hash each of the number of each array, the occurrence of the mark is 1, if a number in two arrays are marked as 1, the description in two arrays have occurred, the direct output of the number (if two arrays appear the same number, even if not the smallest inside, but also separate output). The smallest Mina and minb in the two arrays are also used to find out the recursion , and then compare the two, the smallest before, slightly larger.
Method Two: Sort. Sort directly in ascending order without marking. First determine whether the two array appears the same number (two-tier loop enumeration), otherwise two array the first (smallest) number record, compare the size, small in the front of the large.
"Code":
#include <bits/stdc++.h>using namespacestd;intMain () {intN,m,ma=Ten, mb=Ten; intx,y,a[Ten],b[Ten];//or without int, with BOOLMemset (A,0,sizeof(a)); memset (b,0,sizeof(b));//Hash array Note 0, otherwise the result is wrongCin>>n>>m; for(intI=1; i<=n;i++) {cin>>x; A[X]=1; Ma=min (x,ma); } for(intI=1; i<=m;i++) {cin>>y; B[y]=1; MB=min (Y,MB); } for(intI=1; i<=9; i++) { if(A[i] &&B[i]) {cout<<i<<Endl; return 0;//really good, can not be break, otherwise it will be executed sequentially, will be incorrect output}} cout<<min (MA,MB) <<max (MA,MB) <<Endl; return 0;}
Hash
#include <bits/stdc++.h>using namespacestd;intMain () {intn,m,a[Ten],b[Ten]; while(cin>>n>>m) { for(intI=1; i<=n;i++) Cin>>A[i]; Sort (a+1, a+n+1); for(intI=1; i<=m;i++) Cin>>B[i]; Sort (b+1, b+m+1); for(intI=1; i<=n;i++) { for(intj=1; j<=m;j++) { if(a[i]==B[j]) {cout<<a[i]<<Endl; return 0; } } } intma=a[1]; intmb=b[1]; intminx=min (MA,MB); intmaxx=Max (MA,MB); cout<<minx<<maxx<<Endl; }}
Sort
Codeforces Round #440 A Search for pretty integers "hash/sort"