zoukankan      html  css  js  c++  java
  • 中大周赛第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 }
    View Code




  • 相关阅读:
    Squid-Squid 多层Cache 如何设置实现墙内直连,墙外域名走国外Proxy
    利用win2008虚拟化hyper-v 和squid反向代理,自己做个IDC
    再次分享 pyspider 爬虫框架
    刘宇:我如何5分钟拿到李书福的投资?
    刘宇:2014年投资感悟
    刘宇(正和磁系资本创始人)_百度百科
    python编写的自动获取代理IP列表的爬虫-chinaboywg-ChinaUnix博客
    采集爬虫中,解决网站限制IP的问题?
    Web 应用性能和压力测试工具 Gor
    dnspod-sr内网轻量级DNS首选方案
  • 原文地址:https://www.cnblogs.com/-maybe/p/4438313.html
Copyright © 2011-2022 走看看