BZOJ 2761: [JLOI2011]不重复数字 hash哈希

题目就不贴了

点我看题

题意:这题题意很简明,就是给一个序列,把序列里相同的删掉,然后输出,按原数列顺序。

思路:这题之前QZZ和ZN大神犇叫我去做,辣时还不会hash,就留着了。最近某夏令营学会了hash就回来写。

          就是很简单的hash裸题。

          我的hash就是把数字的每一位加起来然后累乘再膜。

          从夏令营中涨了姿势,hash可以选择不判重,然后直接通过多hash的方法减少碰撞概率。

         QAQ...刚开始以为3hash就够了,最后5hash才水过去。QAQ注意输出格式,行末没空格。

  1 const
  2    base1=23333;
  3    base2=66666;
  4    base3=23366;
  5    base4=66233;
  6    base5=12345;
  7    QZZ=182029; //膜神犇RP++
  8    HR=199999;//膜神犇RP++
  9    TJM=172709;//膜神犇RP++
 10    ZN=198719;//膜神犇RP++
 11    HJW=198953;//膜神犇RP++
 12 var n:longint;
 13     s:string;
 14     num:longint;
 15     a:array[0..50000]of longint;
 16     i,j,x,y,k:longint;
 17     hash1,hash2,hash3,hash4,hash5:int64;
 18     v1,v2,v3,v4,v5:array[0..1,0..200000]of boolean;
 19     bool:integer;
 20     t,qaq:longint;
 21 begin
 22   read(t);
 23   for qaq:=1 to t do
 24   begin
 25     read(n);
 26     for i:=0 to 1 do
 27     for j:=0 to 200000 do
 28     begin
 29       v1[i,j]:=false;
 30       v2[i,j]:=false;
 31       v3[i,j]:=false;
 32       v4[i,j]:=false;
 33       v5[i,j]:=false;
 34     end;
 35     num:=0;
 36     for i:=1 to n do
 37     begin
 38       read(x);
 39       if x>=0 then bool:=0 else
 40       begin
 41         bool:=1;
 42         x:=-x;
 43       end;
 44       str(x,s);
 45       if bool=1 then x:=-x;
 46       hash1:=1;
 47       hash2:=1;
 48       hash3:=1;
 49       hash4:=1;
 50       hash5:=1;
 51       for j:=1 to length(s) do
 52       begin
 53         hash1:=(hash1*base1+ord(s[j])) mod QZZ;
 54         hash2:=(hash2*base2+ord(s[j])) mod HR;
 55         hash3:=(hash3*base3+ord(s[j])) mod TJM;
 56         hash4:=(hash4*base4+ord(s[j])) mod ZN;
 57         hash5:=(hash5*base5+ord(s[j])) mod HJW;
 58       end;
 59       if (not v1[bool][hash1]) then
 60       begin
 61         v1[bool][hash1]:=true;
 62         v2[bool][hash2]:=true;
 63         v3[bool][hash3]:=true;
 64         v4[bool][hash4]:=true;
 65         v5[bool][hash5]:=true;
 66         inc(num);
 67         a[num]:=x;
 68       end else
 69       if (not v2[bool][hash2]) then
 70       begin
 71         v2[bool][hash2]:=true;
 72         v3[bool][hash3]:=true;
 73         v4[bool][hash4]:=true;
 74         v5[bool][hash5]:=true;
 75         inc(num);
 76         a[num]:=x;
 77       end else
 78       if (not v3[bool][hash3]) then
 79       begin
 80         v3[bool][hash3]:=true;
 81         v4[bool][hash4]:=true;
 82         v5[bool][hash5]:=true;
 83         inc(num);
 84         a[num]:=x;
 85       end else
 86       if (not v4[bool][hash4]) then
 87       begin
 88         v4[bool][hash4]:=true;
 89         v5[bool][hash5]:=true;
 90         inc(num);
 91         a[num]:=x;
 92       end else
 93       if (not v5[bool][hash5]) then
 94       begin
 95         v5[bool][hash5]:=true;
 96         inc(num);
 97         a[num]:=x;
 98       end;
 99     end;
100     for i:=1 to num-1 do
101     write(a[i],' ');
102     writeln(a[num]);
103   end;
104 end.
hash

 

转载于:https://www.cnblogs.com/Bunnycxk/p/7308959.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值