ACM: hash题 poj 3274 (题目看了好…

本文介绍了一种算法,用于解决寻找最大平衡区间的问题。在该问题中,我们需要找到一个区间,使得在这个区间内的每一种特征出现的次数相同。文章详细解释了如何通过预处理和哈希技巧来高效地解决这个问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

                                                         Gold Balanced Lineup

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

 

题意: 现在有N头奶牛, 每头奶牛有一个特征值,这个数是由K位二进制表示,对应的位数为1的代表具有该’位‘特征

         牛排成一列,求最长的连续区间.(这里理解好久的最长区间).

解题思路:

                1.假设sum[i][j]表示前i头奶牛的第j特征的和. 在区间[a+1,b]之间.

                有: sum[b][1] - sum[a][1] = sum[b][2] - sum[b][1] = sum[b][j] - sum[a][j]   (1<=j<=K )

                2. 公式变形:

                    sum[b][1] - sum[b][2] = sum[a][1] - sum[b][1]

                    sum[b][1] - sum[b][j] = sum[a][1] - sum[a][j]

                即: sum[i][1] - sum[i][2] 也是个常量.

                 设: c[i][j] = sum[i][1] - sum[i][2];   (1<=j<=K )

                3. 在网上查找讨论发现对c[i]数组进行hash查找. 先将数组转化字符串用UNIX哈希函数.

 

代码:

#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
#define MAX 100003
#define KK 33
#define MAXHASH 10007

int a[MAX];
int sum[MAX][KK];
int c[MAX][KK];
int n, k;
int head[MAXHASH+5];
int next[MAX];
int ans;

int elfHash(char *key)
{
    unsigned long h = 0;
    while(*key)
    {
        h = (h << 4) + *key++;
        unsigned long g = h & 0xf0000000L;
        if(g) h ^= g >> 24;
        h &= ~g;
    }
    return (h+MAXHASH) % MAXHASH;
}

int getHash(int num[])
{
    char str[KK];
    int i;
    for(i = 1; i <= k; ++i)
        str[i] = num[i]+'a';
    str[i] = '\0';
    return elfHash(str+1);
}

void run()
{
    memset(head,-1,sizeof(head));
    for(int i = 0; i <= n; ++i)
    {
        for(int j = 1; j <= k; ++j)
            c[i][j] = sum[i][j] - sum[i][1];
        int h = getHash(c[i]);
        int flag = 0;
        for(int e = head[h]; e != -1; e = next[e])
        {
            int t;
            for(t = 1; t <= k; ++t)
            {
                if(c[i][t] != c[e][t])
                    break;
            }
            if(t > k)
            {
                if(i-e > ans)
                    ans = i-e;
                flag = 1;
                break;
            }
        }
        if(flag == 0)
        {
            next[i] = head[h];
            head[h] = i;
        }
    }
    printf("%d\n",ans);
}

int main()
{
//    freopen("input.txt","r",stdin);
    while(scanf("%d %d",&n,&k) != EOF)
    {
        for(int i = 1; i <= n; ++i)
            scanf("%d",&a[i]);
            
        memset(sum,0,sizeof(sum));        
        for(int i = 1; i <= n; ++i)
        {
            int t = a[i];
            for(int j = 1; j <= k; ++j)
            {
                sum[i][j] = sum[i-1][j] + t%2;
                t /= 2;
            }
        }
        
        ans = 0;
        run();
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值