【数据结构•hash表】OIBH模拟…

【数据结构•hash表】OIBH模拟赛2002#1之集合


  集合(oibh noip 2002模拟赛#1之第1题)
  给定两个集合A、B,集合内的任一元素x满足1 ≤ x ≤ 10^9,并且每个集合的元素个数不大于10^5。我们希望求出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!”

 

Input

  输入文件(SUBSET)有两行,分别表示两个集合,每行的第一个整数为这个集合的元素个数(至少一个),然后紧跟着这个集合的元素(均为不同的正整数)

Output

  输出文件 ( SUBSET ) 只有一行,就是A、B的关系。

Sample Input

2 55 27
2 55 27

Sample Output

A equals B

Hint

附加样例
======================================
样例输入二:
3 9 24 1995
2 9 24

样例输出二:
B is a proper subset of A

======================================

样例输入三:
3 1 2 3
4 1 2 3 4

样例输出三:
A is a proper subset of B

======================================

样例输入四:
3 1 2 3
3 4 5 6

样例输出四:
A and B are disjoint

======================================

样例输入五:
2 1 2
2 2 3

样例输出五:
I'm confused!

======================================

Source

 

经典例题。

hash函数:hash(k):=k mod 129971;

这是从小于130000的最大素数。

若取100001则会超时。

 const
 s=129971;

var
 na,nb:longint;
 a,b:array[0..130001]of longint;
 ha,hb:array[0..s-1]of longint;

procedure init;
var
 i:longint;
begin
 read(na);
 for i:=1 to na do read(a[i]);
 read(nb);
 for i:=1 to nb do read(b[i]);
end;

function hash(i:longint):longint;
begin
 hash:=i mod s;
end;

function locatea(x:longint):longint;
var
 i,org:longint;
begin
 org:=hash(x);
 i:=0;
 while (ha[(org+i)mod s]<>0)and(ha[(org+i)mod s]<>x)and(i<s) do inc(i);
 locatea:=(org+i)mod s;
end;

function locateb(x:longint):longint;
var
 i,org:longint;
begin
 org:=hash(x);
 i:=0;
 while (hb[(org+i)mod s]<>0)and(hb[(org+i)mod s]<>x)and(i<s) do inc(i);
 locateb:=(org+i)mod s;
end;

procedure main;
var
 i,aa,e,ea:longint;
begin
  e:=0; ea:=0;
  for i:=1 to na do ha[locatea(a[i])]:=a[i];
  for i:=1 to nb do hb[locateb(b[i])]:=b[i];

  for i:=0 to s-1 do
  if ha[i]<>0 then
   begin
   aa:=ha[i];
   if hb[locateb(aa)]=aa then e:=e+1
                        else ea:=1;
   end;


  if e=0 then begin writeln('A and B are disjoint'); exit; end;
  if (ea=1)and(nb>e)then begin writeln('I','''','m confused!'); exit; end;
  if ea=1 then  begin writeln('B is a proper subset of A'); exit; end;
  if nb>e then  begin writeln('A is a proper subset of B'); exit; end;
  if nb=e then begin writeln('A equals B'); exit; end;
end;

begin
 init;
 main;
end.

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值