hdu 1800 Flying to Mars 字符串哈希

分析:

题意很好懂,将一列数划分为尽可能少的有序数列。

这其中有一个简单难得的性质:相同的数的个数决定了划分的数列个数。

如果题目数据小的话,可以直接统计。但是本题有提示数据位数直逼30位,而且有前导0,故考虑字符串哈希统计个数。

代码:

(参考了书上的,毕竟刚学,先掌握一下姿势。)
#include <cstdio>
#include <cstring>
#include <cstdlib>

const int maxn = 7010;

char s[32];
int hash[maxn], count[maxn];
int n;
int ans;

inline 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;
}

inline void hashit(char *s) {
        int k;
        while (*s == '0') s++;
        k = ELFHash(s);
        int t = k % maxn;
        while (hash[t] != k && hash[t] != -1) {
                t = (t + 10) % maxn;
        }
        if (hash[t] == -1) {
                count[t] = 1;
                hash[t] = k;
        }
        else if (++count[t] > ans)
                ans = count[t];
        return;
}

int main() {
        #ifndef LOC
                freopen("in.txt", "r", stdin);
        #endif
        while (~scanf("%d", &n)) {
                memset(hash, -1, sizeof(hash));
                getchar();
                ans = 1;
                for (int i = 0; i < n; i++) {
                        scanf("%s", s);
                        hashit(s);
                }
                printf("%d\n", ans);
        }
        return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值