hdu 1004 (字符串哈希函数的应用)

/*
 * File:   hdu1004.cpp
 * Author: chenjiang
 *字符串哈希函数的应用 hdu 1004
 * Created on 2010年3月24日, 下午1:32
 */

#include <stdlib.h>
#include <iostream>
#include <string>
using namespace std;
char ch[1000],ch1[1000];
int a[1005];
//RS哈希
unsigned int RSHash(char *str) {
    unsigned int b = 378551;
    unsigned int a = 63689;
    unsigned int hash = 0;
    while (*str) {
        hash = hash * a + (*str++);
        a *= b;
    }
    return (hash & 0x7FFFFFFF);
}

/*
 *
 */
int main(int argc, char** argv) {

    int n, i, j, max;
    while (cin >> n) {
        if (n == 0)break;
        memset(a, 0, sizeof (a));
        max = 0;
        for (i = 0; i < n; i++) {
            cin >> ch;
            a[RSHash(ch) % 1000]++;
            if (a[RSHash(ch) % 1000] > max) {
                max = a[RSHash(ch) % 1000];
                strcpy(ch1,ch);
            }
        }
        cout << ch1 << endl;
    }
    return (EXIT_SUCCESS);
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值