C++-POJ3274-Gold Balanced Lineup[hash]

不是很懂?

胡乱hash

 1 #include <set>
 2 #include <map>
 3 #include <cmath>
 4 #include <queue>
 5 #include <vector>
 6 #include <cstdio>
 7 #include <cstdlib>
 8 #include <cstring>
 9 #include <iostream>
10 #include <algorithm>
11 using namespace std;
12 const int maxn=150001;
13 int n,k;
14 struct node{int p,next,a[31];}cow[maxn];
15 int hashHead[maxn],cnt;
16 
17 bool cmp(int a,int b){
18     for(int i=1;i<=k;i++)if(cow[a].a[i]!=cow[b].a[i])return false;
19     return true;
20 }
21 
22 int GetHash(int a){
23     int ans=0;
24     for(int i=1; i<=k; i++)ans=(ans*233+cow[a].a[i])%maxn;
25     return ans>0?ans:-ans;
26 }
27 
28 int main() {
29     scanf("%d%d",&n,&k);
30     for(int i=1; i<=n; i++){
31         int x;
32         scanf("%d",&x);
33         for(int j=1; j<=k; j++,x/=2)cow[i].a[j]=x%2;
34     }
35     for(int i=1; i<=n; i++)for(int j=1; j<=k; j++)cow[i].a[j]+=cow[i-1].a[j];
36     for(int i=1; i<=n; i++)for(int j=1; j<=k; j++)cow[i].a[j]-=cow[i].a[k];
37     int ans=0;
38     for(int i=1; i<=n; i++){
39         int hash=GetHash(i),p=0;
40         for(int j=hashHead[hash];j;j=cow[j].next)if(cmp(i,cow[j].p))p=cow[j].p;
41         if(p)ans=max(ans,i-p);
42         if(hash==0)ans=max(i,ans);//???
43         cow[++cnt].p=i;
44         cow[cnt].next=hashHead[hash];
45         hashHead[hash]=cnt;
46     }
47     printf("%d
",ans);
48     return 0;
49 }
~~Jason_liu O(∩_∩)O
原文地址:https://www.cnblogs.com/JasonCow/p/12300836.html