HDU 1800 hash 找出现最多次数的字符串的次数

乘法hash:

这类hash函数利用了乘法的不相关性

int Hash(char *str)
{
    int seed = 131 , value=0;
    while(*str != ‘\0‘){
        value = value*seed+(*str++);
    }
    return value&0x7fffffff;
}

这里用的乘数是131 , 还推荐的乘数还有1313 , 13131 , 131313等

除了乘以一个固定的数,常见的还有乘以一个不断改变的数,比如:

int Hash(char *str)
{
    int b = 378551 , a = 63689;

  int hash = 0;
    while(*str != ‘\0‘){
        hash = hash*a+(*str++);

    a*a*b;
    }
    return value&0x7fffffff;
}

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 #include <iostream>
 5 using namespace std;
 6 char str[105];
 7 int _hash[5000];
 8
 9 int Hash(char *str)
10 {
11     while(*str == ‘0‘) str++; //这道题目的字符串要去除前导0
12     int seed = 131 , value=0;
13     while(*str != ‘\0‘){
14         value = value*seed+(*str++);
15     }
16     return value;
17 }
18
19 int main()
20 {
21   //  freopen("a.in" , "r" , stdin);
22     int n;
23     while(~scanf("%d" , &n))
24     {
25         memset(_hash , 0 , sizeof(_hash));
26         for(int i=0 ; i<n ; i++)
27         {
28             scanf("%s" , str);
29             int index = Hash(str);
30           //  cout<<"index: "<<i<<" "<<index<<endl;
31             _hash[i]=index;
32         }
33         sort(_hash , _hash+n);
34         int ans = 0;
35         int cnt = 1;
36         for(int i=1 ; i<n ; i++){
37             if(_hash[i] == _hash[i-1]){
38                 cnt++;
39
40             }
41             else{
42                 ans = max(ans , cnt);
43                 cnt = 1;
44             }
45         }
46         ans = max(ans , cnt);
47         printf("%d\n" , ans);
48     }
49     return 0;
50 }
时间: 2024-08-11 07:39:08

HDU 1800 hash 找出现最多次数的字符串的次数的相关文章

HDU 1800 字典树

Flying to the Mars Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 10065    Accepted Submission(s): 3270 Problem Description In the year 8888, the Earth is ruled by the PPF Empire . As the popul

HDU 1800 Flying to the Mars (水题)

Flying to the Mars Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 11099    Accepted Submission(s): 3572 Problem Description In the year 8888, the Earth is ruled by the PPF Empire . As the popul

HDU 1800:Flying to the Mars

Flying to the Mars Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 10817    Accepted Submission(s): 3469 Problem Description In the year 8888, the Earth is ruled by the PPF Empire . As the popu

Flying to the Mars HDU - 1800(字典树)

Flying to the Mars HDU - 1800 题目链接:https://vjudge.net/problem/HDU-1800 题目:在8888年,地球由PPF帝国统治.随着人口的增长,PPF需要为新生儿寻找更多的土地.最后,PPF决定攻击统治火星的Kscinow.问题来了!士兵怎么能到达火星? PPF召集他的士兵并询问他们的建议. “匆匆......”一名士兵回答. “闭嘴 !我是否必须提醒你,从这里到火星没有任何道路!“PPF回复道. “飞!”另一个答案. PPF笑道:“聪明的

hdu 1800 字符串哈希 裸题

题意:意思是有若干个飞行员,需要在扫帚上练习飞行,每个飞行员具有不同的等级,且等级高的飞行员可以当等级低的飞行员的老师,且每个飞行员至多有且只有一个老师和学生.具有老师和学生关系的飞行员可以在同一把扫帚上练习,并且这个性质具有传递性.即比如有A,B,C,D,E五个飞行员,且等级是A>B>C>D>E,那么可以使A当B的老师,B当C的老师,E当D的老师,那么A,B,C可以在同一扫帚上练习,D,E在同一把扫帚上练习,这样需要2把扫帚,而如果是A当B的老师,B当C的老师,C当D的老师,D当

UVa 11235 Frequent values (RMQ &amp;&amp; 区间出现最多次的数的次数)

题意 : 给出一个长度为 n 的不降序序列,并且给出 q 个形如(L, R)的问询,问你这个区间出现的最多次的数的次数. 分析 : 很自然的想到将区间"缩小",例如1 1 2 3 3 3就可以变成2 1 3,构造出"数量数组",这个数组实际上就是已经将原来区间分了块,但是问询的区间不可能就是这些"数量数组"构成的"块",不过先来想想问询的区间可不可能包含这里面的某些"块"?很显然是有可能的,那么从这些&qu

[email&#160;protected]返回字符串中出现次数最多的那个字符和次数2

1 <!DOCTYPE html> 2 <html> 3 <head lang="en"> 4 <meta charset="UTF-8"> 5 <title>@返回字符串中出现次数最多的那个字符和次数2</title> 6 7 </head> 8 <body> 9 </body> 10 11 <script type="text/javasc

hdu 1800 (map)

链接:http://acm.hdu.edu.cn/showproblem.php?pid=1800 Flying to the Mars Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 10830    Accepted Submission(s): 3472 Problem Description In the year 8888, t

hdu 1800 Flying to the Mars(简单模拟,string,字符串)

题目 又来了string的基本用法 //less than 30 digits //等级长度甚至是超过了int64,所以要用字符串来模拟,然后注意去掉前导零 //最多重复的个数就是答案 //关于string的修改增加的用法 #include <cstdio> #include<iostream> #include <cstring> #include <algorithm> #include<string> using namespace std