zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    POJ 2236 Wireless Network(并查集)
    POJ 2010 Moo University
    POJ 3614 Sunscreen(贪心,区间单点匹配)
    POJ 2184 Cow Exhibition(背包)
    POJ 1631 Bridging signals(LIS的等价表述)
    POJ 3181 Dollar Dayz(递推,两个long long)
    POJ 3046 Ant Counting(递推,和号优化)
    POJ 3280 Cheapest Palindrome(区间dp)
    POJ 3616 Milking Time(dp)
    POJ 2385 Apple Catching(01背包)
  • 原文地址:https://www.cnblogs.com/Bunnycxk/p/7308959.html
Copyright © 2011-2022 走看看