给定两个集合A、B,集合内的任一元素x满足1 ≤ x ≤ 109,并且每个集合的元素个数不大于105。我们希望求出A、B之间的关系。
任务 :给定两个集合的描述,判断它们满足下列关系的哪一种:
A是B的一个真子集,输出“A is a proper subset of B”
B是A的一个真子集,输出“B is a proper subset of A”
A和B是同一个集合,输出“A equals B”
A和B的交集为空,输出“A and B are disjoint”
上述情况都不是,输出“I’m confused!”
思路
时间复杂度o(n)。
代码
const
maxn=400319;
var
n,m,s:longint;
hx:array [0..maxn] of longint;
functiontry(k:longint):longint;
var
t:longint;
begin
t:=k mod maxn;
while (hx[t]<>0)and(hx[k]<>k) do
t:=(t+1) mod maxn;
try:=t;
end;
procedureinit;var
t,i:longint;
beginread(n);
for i:=1to n dobeginread(t);
hx[try(t)]:=t;
end;
end;
proceduremain;var
i,t:longint;
beginread(m);
for i:=1to m dobeginread(t);
if hx[try(t)]<>t then inc(s);
end;
end;
procedureprint;beginif (n=m) and (s=0) thenbeginwrite('A equals B');
exit;
end;
if (n>m) and (s=0) thenbeginwrite('B is a proper subset of A');
exit;
end;
if (m>n) and (n+s=m) thenbeginwrite('A is a proper subset of B');
exit;
end;
if (s=m) thenbeginwrite('A and B are disjoint');
exit;
end;
write('I''m confused!');
end;
begin
init;
main;
print;
end.