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




  • 相关阅读:
    sqlserver 自学笔记 函数实训 学分学期转换函数的设计
    jquery dom操作
    jquery clone方法
    Go开发常见陷阱
    Go 语言从新手到大神:每个人都会踩的五十个坑(转)
    Go文件操作大全
    linux下安装go
    Go 学习笔记
    分布式系统设计系列 -- 基本原理及高可用策略 (转)
    安装Redis图形监控工具---RedisLive
  • 原文地址:https://www.cnblogs.com/-maybe/p/4438313.html
Copyright © 2011-2022 走看看