1. 程式人生 > >BZOJ 2761: [JLOI2011]不重復數字 hash哈希

BZOJ 2761: [JLOI2011]不重復數字 hash哈希

ger begin 數列 false 裸題 length code 方法 base

題目就不貼了

點我看題

題意:這題題意很簡明,就是給一個序列,把序列裏相同的刪掉,然後輸出,按原數列順序。

思路:這題之前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

BZOJ 2761: [JLOI2011]不重復數字 hash哈希