这个题也是数组hash,不过很巧妙,先开始看不出来。
后来看了别人的解题才知道。强烈推荐
后来看了别人的解题才知道。强烈推荐
code:
#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
using namespace std;
#define mod 999983
struct hash
{
int d;
hash* next;
hash(){next = NULL;}
}*h[1000000];
int n,k;
int f[100010][32];
int ans = 0;
int key;
int get_key(int x)
{
int key = 0;
for(int i=0;i<k-1;i++)
key += f[x][i]%mod;
return (key+mod)%mod;
}
void insert(int x)
{
bool ret = false;
if(!h[key])
{
h[key] = new hash;
h[key]->d = 0;
h[key]->next = new hash;
h[key]->next->d = x;
return ;
}
hash *temp = h[key];
while(temp->next)
{
temp = temp->next;
int r = temp->d;
int flag = 1;
for(int i=0;i<k-1;i++)
if(f[x][i]!=f[r][i]){flag = 0;break;}
if(flag){ans = max(ans,x-r);ret = true;}
}
if(!ret)
{
temp->next = new hash;
temp = temp->next;
temp->d = x;
}
}
int main()
{
//freopen("input.txt","r",stdin);
while(~scanf("%d%d",&n,&k))
{
memset(h,0,sizeof(h));
ans = 0;
for(int i=0;i<k;i++)
f[0][i] = 0;
for(int i=1;i<=n;i++)
{
int t;
scanf("%d",&t);
for(int j=0;j<k;j++)
f[i][j] = f[i-1][j] + ((t>>j) & 1);
}
key = 0;
insert(0);
for(int i=1;i<=n;i++)
{
for(int j=0;j<k-1;j++)
f[i][j] = f[i][j+1] - f[i][j];
//for(int j=0;j<k-1;j++)
// cout << f[i][j] <<" ";
//cout << endl;
key = get_key(i);
insert(i);
}
printf("%d\n",ans);
}
return 0;
}