中大周赛第7场 HASH 简单题

Description

Spies use attributes to disguise themselves to make sure that they are not recognized. For example, when putting on sunglasses, a spy suddenly looks completely different and cannot be recognized anymore. Every combination of attributes gives a different appearance, but not all combinations are possible. For example, a hat and a turban are both headgear and cannot be used at the same time. Given the list of available attributes, compute how many distinct disguises can be made.

Input

On the first line one positive number: the number of test cases, at most 100. After that per test case:

  • one line with an integer n (0 ≤ n ≤ 30): the number of available attributes.
  • n lines with two space-separated strings: the name and the category of the attribute. All strings consist of at least 1 and at most 20 lowercase letters. Within a test case all names are distinct.

Output

Per test case:

  • one line with an integer: the number of possible distinct disguises that can be made with the given attributes, such that at most one attribute from each category is used.

Sample Input

2
3
hat headgear
sunglasses eyewear
turban headgear
3
mask face
sunglasses face
makeup face

Sample Output

5
3

不管前面的名字,直接记录后面每种类型的个数就好了,对于每种类型i,有a[i]个,则有a[i]+1种选择,拿其中一个或者不拿,所以一共有sum=(a[i]+1)的乘积再减去1,因为有一种情况是所有的都不拿,不符合要求。初始化:a初始化为0然后题目每给出一种类型,我们就在 a[该类型]++而类型给我们的是字符串,所以要hash。

注意,hash的时候取余时的mod要大一点,不然很可能会重复。

 1 #include<iostream>
 2 #include<cstring>
 3 using namespace std;
 4
 5 const int maxn=3500;
 6
 7 unsigned int BKDRHash(char*s)
 8 {
 9     unsigned int seed=13131;
10     unsigned int hash=0;
11     while(*s)
12     {
13         hash=hash*seed+(*s++);
14     }
15     return (hash%maxn);             //题目这里最大的n是30,但是wa了,因为mod开小了可能会重复
16 }
17
18 int a[maxn+5];
19
20 int main()
21 {
22     int test;
23     cin>>test;
24     while(test--)
25     {
26         memset(a,0,sizeof(a));
27         int n;
28         cin>>n;
29         char name[30],cat[30];
30         for(int i=1;i<=n;i++)
31         {
32             cin>>name;
33             cin>>cat;
34             a[BKDRHash(cat)]++;
35         }
36         int sum=1;
37         for(int i=0;i<maxn+5;i++)
38         {
39             sum=sum*(a[i]+1);
40         }
41         sum--;
42         cout<<sum<<endl;
43     }
44     return 0;
45 }

				
时间: 2024-07-29 01:08:45

中大周赛第7场 HASH 简单题的相关文章

SDUT 周赛 神奇的树(简单题 注意数据类型的溢出 )

神奇的树 Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 SDUT有一颗神奇的苹果树.假如某天早上这树上有x个苹果,那么这树这一天会再结出x个苹果来,也就是说到了晚上会有2*x个苹果,到了深 夜,会有专人人来摘苹果,而摘苹果的人总会使苹果数剩下当前数量对m的余数,也就是说假如当前数量为a,那么第二天早上会剩下a%m个苹果.也许聪明的你 已经发现了,有可能从某一天开始,这棵树上就再也没有苹果了.那么给你第一天早上的苹果数,请你判

hdu 4970 Killing Monsters(简单题) 2014多校训练第9场

Killing Monsters                                                                        Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Problem Description Kingdom Rush is a popular TD game, in which you should b

poj 3270 Cow Sorting 置换群 简单题

假设初始状态为 a:2 3 1 5 4 6 则目标状态为 b:1 2 3 4 5 6且下标为初始状态中的3 1 2 4 5 6(a[3],a[1]...) 将置换群写成循环的形式 (2,3,1),(5,4),6就不用移动了. 移动方式2种 1:选循环内最小的数和其他len-1个数交换 2:选整个序列最小的数和循环内最小的数交换,转到1,再换回来. #include<cstdio> #include<queue> #include<algorithm> #include&

[BestCoder Round #3] hdu 4907 Task schedule (模拟简单题)

Task schedule Problem Description 有一台机器,并且给你这台机器的工作表,工作表上有n个任务,机器在ti时间执行第i个任务,1秒即可完成1个任务. 有m个询问,每个询问有一个数字q,表示如果在q时间有一个工作表之外的任务请求,请计算何时这个任务才能被执行. 机器总是按照工作表执行,当机器空闲时立即执行工作表之外的任务请求. Input 输入的第一行包含一个整数T, 表示一共有T组测试数据. 对于每组测试数据: 第一行是两个数字n, m,表示工作表里面有n个任务,

2018 HDU多校第三场赛后补题

2018 HDU多校第三场赛后补题 从易到难来写吧,其中题意有些直接摘了Claris的,数据范围是就不标了. 如果需要可以去hdu题库里找.题号是6319 - 6331. L. Visual Cube 题意: 在画布上画一个三维立方体. 题解: 模拟即可. 代码: #include <bits/stdc++.h> using namespace std; int a, b, c, R, C; char g[505][505]; int main () { int T; cin >>

poj2105 IP Address(简单题)

题目链接:http://poj.org/problem?id=2105 Description Suppose you are reading byte streams from any device, representing IP addresses. Your task is to convert a 32 characters long sequence of '1s' and '0s' (bits) to a dotted decimal format. A dotted decima

数论 --- 简单题

吃糖果 Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 22376    Accepted Submission(s): 6396 Problem Description HOHO, 终于从Speakless手上赢走了所有的糖果,是Gardon吃糖果时有个特殊的癖好,就是不喜欢将一样的糖果放在一起吃,喜欢先吃一种,下一次吃另一 种,这样:

BZOJ 2683 简单题 ——CDQ分治

简单题 #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; #define F(i,j,k) for (int i=j;i<=k;++i) #define D(i,j,k) for (int i=j;i>=k;--i) #define maxn 2000005 int sum[maxn]; void a

HNU 12868 Island (简单题)

题目链接:http://acm.hnu.cn/online/?action=problem&type=show&id=12868&courseid=272 解题报告:输入n*m的地图,+表示土地,-表示水,要你求这个海岛的海岸线有多长,扫一遍就可以了. 1 #include<cstdio> 2 const int maxn = 2000; 3 char map[maxn][maxn]; 4 int _x[4] = {-1,0,1,0}; 5 int _y[4] = {0