poj3274 Find a balanced sequence (hash plus a little mathematical thinking)

topic portal

The main idea of ​​the title: There are n cows, each cow has k attributes, and then there are n numbers. The 1 and 0 on the binary bits of each number indicate the presence or absence of a certain attribute, and then a special sequence is, a The sum of various attributes of all cows in the interval is equal (there are e species 1 attribute e species 2 attributes and so on), ask you what is the length of the longest special sequence of this steak.

Ideas: It looks like DP, but the ideas don't work out, and then looking at the ideas of online bigwigs, it seems to open the door to a new world.

The array sum[i][j] represents the sum of cow attribute j from 1 to i. Therefore, the requirement of the title is equivalent to satisfy

sum[i][0]-sum[j][0]==sum[i][1]-sum[j][1]==.....==sum[i][k-1]-sum[j][k-1] (j<i)

the largest ij

Transform the above formula into

sum[i][1]-sum[i][0]==sum[j][1]-sum[j][0]

sum[i][2]-sum[i][0]==sum[j][2]-sum[j][0]

.
sum[i][k-1]-sum[i][0]==sum[j][k-1]-sum[j][0]


令C[i][l]=sum[i][l]-sum[i][0] (0<l<k)。

So only the largest ij in C[i]==C[j] needs to be satisfied.

Take an example to illustrate:

   x attribute cow

   7          1 1 1            1

   6          0 1 1            2

   7          1 1 1            3

   2          0 1 0            4

   1          1 0 0            5

   4          0 0 1            6

   2          0 1 0            7

Accumulate sum[i] row by row:

1 1 1

1 2 2

2 3 3

2 4 3

3 4 3

3 4 4

3 5 4

Both subtract the first column to get c[i]:

0 0 0

0 1 1

0 1 1

0 2 1

0 1 0

0 1 1

0 2 1

So the maximum interval is 6-2 = 4

The main purpose of this question is to let me understand the use of hashing. Although I am still groping how to use it, I have slowly opened the door of hashing, and the transformation of the meaning of this question is also very important. .

#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<sstream>
#include<cstring>
#include<bitset>
#include<cstdio>
#include<string>
#include<deque>
#include<stack>
#include<cmath>
#include<queue>
#include<set>
#include<map>
#define INF 0x3f3f3f3f
#define CLR(x,y) memset(x,y,sizeof(x))
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
using namespace std;
typedef pair<int,int> pii;
typedef long long ll;
const double PI=acos(-1.0);
int fact[10]= {1,1,2,6,24,120,720,5040,40320,362880};
const int maxn = 100005;
const int MAX = 100005;
const int mod = 1000000;
int hash[MAX*10];//hash table storage subscript
int next[MAX*10];//next as the adjacency list
int sum[MAX][35];//The sum of the corresponding attributes from the 1st cow to the ith head
int c[MAX][35];//Store the difference between the attribute j of each cow and the first attribute
int n,k;
int gethash(int *cc){
	int key=0;
	for(int i=1;i<=k;i++){
		key=(key)%mod+cc[i]%mod*cc[i]%mod;//Hash function viewed online
		key%=mod;
	}
	return abs(key);//key may be negative
}
bool compare(int id,int x){
	for(int i=1;i<=k;i++){
		if(c[id][i]!=c[x][i])return false;
	}
	return true;
}
int main(){
	cin>>n>>k;
	memset(hash,-1,sizeof(hash));
	hash[0]=0;//These two lines are important because it is possible that the entire sequence is balanced
	next[0]=-1;//The sequence expression is ij, so the hash of some cows may be 0
	int maxlen=0;
	for(int i=1;i<=n;i++){
		int x;
		scanf("%d",&x);
		for(int j=1;j<=k;j++){
			sum[i][j]+=sum[i-1][j]+x%2;
			c[i][j]=sum[i][j]-sum[i][1];
			x/=2;
		}
		int key=gethash(c[i]);
		for(int j=hash[key];j!=-1;j=next[j]){
			if(compare(i,j)){
				maxlen=max(i-j,maxlen);
			}
		}
		next[i]=hash[key];//Adjacency list
		hash[key]=i;
	}
	cout<<maxlen<<endl;
}
old Balanced Lineup
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 16727   Accepted: 4736

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 only K 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

Hint

In the range from cow #3 to cow #6 (of size 4), each feature appears in exactly 2 cows in this range

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326662477&siteId=291194637
Recommended