码迷,mamicode.com
首页 > 其他好文 > 详细

中大周赛第7场 HASH 简单题

时间:2015-04-18 23:28:23      阅读:214      评论:0      收藏:0      [点我收藏+]

标签:

 
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 }
View Code

 





中大周赛第7场 HASH 简单题

标签:

原文地址:http://www.cnblogs.com/-maybe/p/4438313.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!