poj2503Babelfish(hash);

10 篇文章 0 订阅

题意:

给你n行单词,每行两个,让你找后面那个是否出现过,出现过就输出前面对应得单词,不然输出ch
n<100000

思路

第一想法快排+二分,但是是hash得专项训练,就没有打快排了,hash也很简单,用字符串hash,强行转换成数字为hash得key,然后就变成普通hash了。

时间复杂度

hash期望O(1),加上单词书为O(n)

const
 maxn=999987;

var
  a,hash:array[0..maxn] of string[30];
  i,j:longint;
  s,t:string[60];



procedure insertion(s,t:string[30]);
var
 i,l:longint;
 k:int64;
begin
 l:=length(s);
 k:=0;
 for i:=1 to l do
  k:=k*50+ord(s[i]);
 i:=k mod maxn;
 while (hash[i]<>'') and (hash[i]<>s) do i:=i mod maxn+1;
 hash[i]:=s;
 a[i]:=t;
end;

function find(s:string[30]):longint;
var
 i,j,l:longint;
 k:int64;
begin
 k:=0; l:=length(s);
 for i:=1 to l do
  k:=k*50+ord(s[i]);
 i:=k mod maxn;
 while (hash[i]<>'') and (hash[i]<>s) do i:=i mod maxn+1;
 if hash[i]=s then exit(i)
              else exit(0);

end;


begin
  a[0]:='eh';
  while true do
    begin
      readln(s);
      if s='' then break;
      t:=copy(s,1,pos(' ',s)-1);
      delete(s,1,pos(' ',s));
      insertion(s,t);
    end;
  while not eof do
    begin
      readln(s);
      writeln(a[find(s)]);
    end;
end.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值