Skip to main content
  1. Posts/

hdu 1800 Flying to the Mars (字符串hash)

·2 mins
Note: This article is available in Chinese only. 本文暂无英文版本。 View original

题目链接

题意:n个人,每个人有一个level值,用一个最长30位的,可能带前缀0的数字串表示,如果i的level大于j的level,那么i可以教j飞行,每个人只能有一个老师,每个人也只能收一个徒弟。师生可以共用一把扫帚飞行。现在问最少需要多少扫帚。

思路:分析发现,影响扫帚多少的是相等的数有多少,因为只要不相等,就肯定可以构成师生关系….

更确切得说,是所有数出现次数的最大值。

有一个trick点,就是带前缀0和不带前缀0的两个level被认为是相等的,hash的时候要处理前缀0.

 1/* ***********************************************
 2Author :111qqz
 3Created Time :2016年11月22日 星期二 19时18分30秒
 4File Name :code/hdu/1800.cpp
 5************************************************ */
 6
 7#include <cstdio>
 8#include <cstring>
 9#include <iostream>
10#include <algorithm>
11#include <vector>
12#include <queue>
13#include <set>
14#include <map>
15#include <string>
16#include <cmath>
17#include <cstdlib>
18#include <ctime>
19#define fst first
20#define sec second
21#define lson l,m,rt<<1
22#define rson m+1,r,rt<<1|1
23#define ms(a,x) memset(a,x,sizeof(a))
24typedef long long LL;
25#define pi pair < int ,int >
26#define MP make_pair
27
28using namespace std;
29const double eps = 1E-8;
30const int dx4[4]={1,0,0,-1};
31const int dy4[4]={0,-1,1,0};
32const int inf = 0x3f3f3f3f;
33unsigned int BKDHash(char *str)
34{
35    unsigned int seed = 251;
36    unsigned int hash = 0 ;
37    while (*str=='0') str++; //带前缀0的和不带前缀0的认为是同一个数,因此要处理前缀0.
38    while (*str) hash = hash*seed+(*str++);
39    return (hash&0x7fffffff);
40}
41int n;
42map<int,int>mp;
43char st[305];
44int main()
45{
46	#ifndef  ONLINE_JUDGE
47//	freopen("code/in.txt","r",stdin);
48  #endif
49	while (~scanf("%d",&n))
50	{
51	    mp.clear();
52	    for ( int i = 1; i <= n ; i++)
53	    {
54		scanf("%s",st);
55		int id = BKDHash(st);
56		if (!mp[id]) mp[id] = 1;
57		else mp[id]++;
58	    }
59	    int ans = 0;
60	    for ( auto it = mp.begin(); it !=mp.end();  it++)
61	    {
62		ans = max(ans,it->sec);
63	    }
64	    printf("%d\n",ans);
65	}
66
67
68  #ifndef ONLINE_JUDGE
69  fclose(stdin);
70  #endif
71    return 0;
72}

Related

codeforces 4C. Registration system (字符串hash)

·1 min
题目链接 题意:网站的注册系统..处理用户要注册的用户名,如果数据库中没有重名输出OK,否则输出要注册的用户名的字符串+num,num的大小为之前一共有多少个用户试图用该用户名。

hdu 1880 魔咒词典 (字符串hash)

·1 min
题目链接 题意:给你一部魔咒词典。当哈利听到一个魔咒时,你的程序必须告诉他那个魔咒的功能;当哈利需要某个功能但不知道该用什么魔咒时,你的程序要替他找到相应的魔咒。如果他要的魔咒不在词典中,就输出“what?”

uva 156 - Ananagrams

·2 mins
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=92 题意:给出一段文字,包含若干个单词,以’#‘结束。按照字典序输出所有的ananagrams。所谓ananagram,是指经过任意的重排后,不能得到这段文字中的另一个单词(不区分大小写) 思路:首先是字符串的读入…可以整行读入然后用空格分隔单词。由于补区分大小写,所以要都转化成小写…但是输出的时候要输出原始,所以还记得保留一份。而且要能够通过新的找到原始的(我用了一个toori的map<string,string>来实现) 然后最关键的部分是如何判断两个单词经过重排是否能一样…