#include <iostream>
#include <cstdio>
#include <cstring>
#define N 100001
#define Prime 99983
using namespace std;
int n, k, flag[N][31], c[N][31], hash[Prime];
inline int hashcode(const int *v)
{
int s = 0;
for(int i = 0; i < k; i++)
s=((s << 2) + (v[i] >> 4)) ^ (v[i] << 10);
s = s % Prime;
s = s < 0 ? s + Prime : s;
return s;
}
int main()
{
int i, j, tmp, maximum = 0;
scanf("%d%d", &n, &k);
memset(flag, 0, sizeof(flag));
memset(c, 0, sizeof(c));
memset(hash, -1, sizeof(hash));
hash[hashcode(c[1])] = 0;
for (i = 1; i <= n; i++)
{
scanf("%d", &tmp);
for (j = 0; j < k; j++)
{
if ((tmp >> j) & 1) flag[i][j] = flag[i - 1][j] + 1;
else flag[i][j] = flag[i - 1][j];
c[i][j] = flag[i][j] - flag[i][0];
}
tmp = hashcode(c[i]);
while (hash[tmp] != -1)
{
for (j = 1; j < k; j++)
if (c[i][j] != c[hash[tmp]][j]) break;
if (j >= k)
{
j = i - hash[tmp];
maximum = maximum > j ? maximum : j;
break;
}
tmp++;
}
if (hash[tmp] == -1) hash[tmp] = i;
}
printf("%d\n", maximum);
return 0;
}
poj3274 hash数组
最新推荐文章于 2018-04-29 18:22:29 发布