Problem Description
DaYu在新的学习开始学习新的数学知识,一天DaYu学习集合的时候遇到一个问题,他有一个集合A和A的子集B,他想用一个二进制串表示集合B。
Input
多组输入,每组的第一行有两个数n,m,(0< m < n < 10^5).
第二行输入n个数表示集合A,第三行输入m个数表示集合B,|data_i|< 10^5
Output
输出一个01字符串表示集合B
Example Input
10 5
1 2 3 4 5 6 7 8 9 10
1 3 5 7 8
Example Output
1010101100
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int f(int a[],int s,int t,int key)
{
int left = s,right = t , mid;
if(s<=t)
{
mid = left+(right-left)/2;
if(a[mid]==key)
return mid;
else if(a[mid]>key)
return f(a,left,mid-1,key);
else return f(a,mid+1,right,key);
}
return -1;
}
int cmp(const void *a,const void *b)
{
return *(int *)a-*(int *)b;
}
int main()
{
int n , m ,i , j,p;
int a[100010] ,b [100010],c[100010];
while(scanf("%d %d",&n,&m)!=EOF)
{
p = 0;
memset(c,0,sizeof(c));
for(i = 0;i<n;i++)
scanf("%d",&a[i]);
for(j = 0;j<m;j++)
scanf("%d",&b[j]);
qsort(b,m,sizeof(b[0]),cmp);
for(i = 0;i<n;i++)
{
p = f(b,0,m-1,a[i]);
if(p!=-1)
c[i] = 1;
}
for(i = 0;i<n;i++)
printf("%d",c[i]);
printf("\n");
}
return 0;
}