hdu-1800(字符串hash)

题目链接:传送门

思路:

就是找最多多少个扫帚,每个扫帚上有连续递增的序列,就是找一个最多重复数字的重复次数。

由于是30位,每次用char*类型,然后用hash映射一下,排序找最多就行了。

注意:

(1)num最小也是1。

(2)注意前导零。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 30030;
const int INF = 0x7ffffff;
typedef unsigned long long ULL;
ULL base=133,a[maxn];
char str[maxn];
ULL Hash(char *s)
{
    ULL ans=0,H=0;
    while(*s=='0') s++;
    while(*s)
    {
        H=H*base+(*s++);
    }
    return H&INF;
}
int main(void)
{
    int n,i;
    while(~scanf("%d",&n))
    {
        for(i=0;i<n;i++)
        {
            scanf("%s",&str);
            a[i]=Hash(str);
        }
        sort(a,a+n);
        int num=1,tp=1;
        for(i=1;i<n;i++)
        {
            if(a[i]==a[i-1])
            {
                tp++;
                if(num<tp) num=tp;
            }
            else tp=1;
        }
        printf("%d\n",num);
    }
    return 0;
}
View Code

猜你喜欢

转载自www.cnblogs.com/2018zxy/p/10208576.html