Gold Balanced Lineup hash function, first contact, draws blog thought the great God, read a long time to understand, weak and vegetables, ...

Problem Description

Farmer John's N cows (1 ≤ N ≤ 100,000) share many similarities. In fact, FJ has been able to narrow down the list of features shared by his cows to a list of onlyK different features (1 ≤ K ≤ 30). For example, cows exhibiting feature #1 might have spots, cows exhibiting feature #2 might prefer C to Pascal, and so on.

FJ has even devised a concise way to describe each cow in terms of its "feature ID", a single K-bit integer whose binary representation tells us the set of features exhibited by the cow. As an example, suppose a cow has feature ID = 13. Since 13 written in binary is 1101, this means our cow exhibits features 1, 3, and 4 (reading right to left), but not feature 2. More generally, we find a 1 in the 2^(i-1) place if a cow exhibits feature i.

Always the sensitive fellow, FJ lined up cows 1..N in a long row and noticed that certain ranges of cows are somewhat "balanced" in terms of the features the exhibit. A contiguous range of cows i..j is balanced if each of the K possible features is exhibited by the same number of cows in the range. FJ is curious as to the size of the largest balanced range of cows. See if you can determine it.

 

 

Input
Line 1: Two space-separated integers,  N and  K
Lines 2.. N+1: Line  i+1 contains a single  K-bit integer specifying the features present in cow  i. The least-significant bit of this integer is 1 if the cow exhibits feature #1, and the most-significant bit is 1 if the cow exhibits feature # K.
 

 

Output
Line 1: A single integer giving the size of the largest contiguous balanced group of cows.
 

 

Sample Input
7 3 7 6 7 2 1 4 2
 

 

Sample Output
4
****************************************************************************************************************************
  1  / * 
  2  array SUM [i] [j] denotes the number of occurrences from the first to the i-j is the first cow properties.
  . 3  
  . 4  it is equivalent to subject to:
   . 5  
  . 6  seeking to meet
   . 7  
  . 8  SUM [I] [0] -sum [J] [0] = SUM [I] [. 1] -sum [J] [. 1] = ... SUM = .. [I] [K-. 1] -sum [J] [K-. 1] (J <I)
   . 9  
10  greatest ij of
 . 11  
12 is   
13 is  
14  to transform the obtained formula
 15  
16  SUM [I] [ . 1] -sum [I] [0] = SUM [J] [. 1] -sum [J] [0]
 . 17  
18 is  SUM [I] [2] -sum [I] [0] = SUM [J] [2 ] -sum [J] [0]
 . 19  
20 is  ......
 21 is  
22 is  SUM [I] [K-. 1] -sum [I] [0] = SUM [J] [K-. 1] -sum [J ] [0]
 23 is  
24   
25  
26 is Order C [I] [Y] = SUM [I] [Y] -sum [I] [0] (0 <Y <K)
 27  
28  initial condition C [0] [0 ~. 1-K] = 0
 29  
30   
31 is  
32  so only to meet the needs of C [i] [] == C [j] [] the largest ij, where 0 <= j <i <=
 n. 33 is  
34 is  C [i] [] == C [j] [] that is equal to the two-dimensional array C [] [] value of the i-th row and the j-th row of the corresponding column,
 35  
36  then is transformed into the original title seek array C ij are equal and separated by a distance of two farthest rows.
37 [  * / 
38 is #include <the iostream>
 39 #include < String >
 40 #include <CString>
 41 is #include <the cmath>
 42 is #include <cstdio>
 43 is  the using  namespace STD;
 44 is  const  int MAXN = 107 777;
 45  int the hash [MAXN], C [MAXN] [ 50 ], SUM [MAXN];
 46 is  int I, J, K, n-, m;
 47  // min_index [] array represents the equivalent subscript line 
48  int min_index [MAXN], Next [MAXN];
 49  BOOL   Judge ( int a, int b) // determines a row and row b are equal to 
50  {
 51 is      for ( int IT = 0 ; IT <K; IT ++ )
 52 is      {
 53 is          iF (C [A] [IT] =! C [B] [IT])
 54 is            return  to false ;
 55      }
 56     return true;
 57 }
 58 int hashcode(int *v)//hash函数
 59 {
 60    int s=0;
 61    for(int it=0;it<k;it++)
 62    {
 63        s=((s<<2)+(v[it]>>4))^(v[it]<<10);
 64    }
 65    s=s%maxn;
 66    if(s<0)
 67     s=s+MAXN;
 68     return S;
 69  }
 70  int all_0 ( int index) // determines whether a row is 0 
71 is  {
 72     for ( int IT = 0 ; IT <K; IT ++ )
 73 is     {
 74         IF (C [index] [I] ! = 0 )
 75           return  to false ;
 76     }
 77     return  to true ;
 78  }
 79  void INSERT ( int index)
 80  {
 81     int H = hashCode (C [index]);
 82      IF (! H)
 83      {
 84          IF (all_0 (index))
 85          {
 86              min_index [index] = 0 ;
 87              return ;
 88          }
 89      }
 90      int U = the hash [ H];
 91 is      IF (! U) // if the hash array is 0, the assignment 
92      {
 93          min_index [index] = index;
 94          hash [H] = index;
 95          return ;
96      }
 97      the while (U)
 98      {
 99          IF (Judge (index, U)) // find equal, returns 
100          {
 101              min_index [index] = min_index [U];
 102              return ;
 103          }
 104          U = Next [ U];
 105      }
 106      min_index [index] = index;
 107      Next [index] = the hash [H];
 108      the hash [H] = index;
 109  
110  }
 111  int main ()
 112 {
113     while(~scanf("%d%d",&n,&k))
114     {
115         memset(c,0,sizeof(c));
116         memset(min_index,0,sizeof(min_index));
117         memset(next,0,sizeof(next));
118         memset(hash,0,sizeof(hash));
119         int sum[39]={0},num,max1=0;
120         for(i=1;i<=n;i++)
121         {
122             scanf("%d",&num);
123             for(j=0;j<k;j++)
124             {
125                 sum[j]+=((1<<j)&num)?1:0;
126                 c[i][j]=sum[j]-sum[0];
127             }
128             insert(i);
129         }
130         for(i=1;i<=n;i++)//Selecting the maximum value 
131 is          {
 132              IF (MAX1 <I- min_index [I])
 133               MAX1 = I- min_index [I];
 134          }
 135          the printf ( " % D \ n- " , MAX1);
 136      }
 137      return  0 ;
 138 }
View Code

 

Reproduced in: https: //www.cnblogs.com/sdau--codeants/p/3392749.html

Guess you like

Origin blog.csdn.net/weixin_34293059/article/details/93432851