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




  • 相关阅读:
    Windows Server2016环境中安装Oracle12c数据库
    centos7 安装Oracle19C 数据库
    centos7 磁盘分区以及磁盘挂载
    PHP日常开发技巧散记
    代码压缩工具 webpack安装与入门使用【初级】
    程序员修炼之道系列 | 使用曳光弹找到目标
    程序员修炼之道系列 | 不要冲出前灯范围
    程序员修炼之道系列 | 敏捷估算
    程序员修炼之道系列 | “豆腐渣“工程竟然也能做原型
    官宣!禅道与极狐(GitLab)达成深度合作,携手推进开源开放DevOps生态
  • 原文地址:https://www.cnblogs.com/-maybe/p/4438313.html
Copyright © 2011-2022 走看看