zoukankan      html  css  js  c++  java
  • SRM331-CarolsSinging(暴力,位运算)

    Problem Statement

    When the Christmas dinner is over, it's time to sing carols. Unfortunately, not all the family members know the lyrics to the same carols. Everybody knows at least one, though.

    You are given a lyrics. The j-th character of the i-th element of lyrics is 'Y' if the i-th person knows the j-th carol, and 'N' if he doesn't. Return the minimal number of carols that must be sung to allow everyone to sing at least once.

    Definition

    Class:

    CarolsSinging

    Method:

    choose

    Parameters:

    vector <string>

    Returns:

    int

    Method signature:

    int choose(vector <string> lyrics)

    (be sure your method is public)

    Limits

    Time limit (s):

    840.000

    Memory limit (MB):

    64

    Constraints

    - lyrics will contain between 1 and 30 elements, inclusive.

    - Each element of lyrics will contain between 1 and 10 characters, inclusive.

    - Each element of lyrics will contain the same number of characters.

    - Each element of lyrics will contain only 'Y' and 'N' characters.

    - Each element of lyrics will contain at least one 'Y' character.

    Examples

    0)

    {"YN","NY"}

    Returns: 2

    Both carols need to be sung.

    1)

    {"YN","YY","YN"}

    Returns: 1

    Everybody knows the first carol, so singing just that one is enough.

    2)

    {"YNN","YNY","YNY","NYY","NYY","NYN"}

    Returns: 2

    Singing the best known carol is not always the optimal strategy. Here, the optimal way is to pick the first two carols even though four people know the third one.

    3)

    {"YNNYYY","YYNYYY","YNNYYN","NYYNNN","YYYNNN","YYYNNY","NYYYYY","NYNYYY","NNNNYY", "YYYYYY","YNNNNN","YYYYNY","YYNNNN","NNYYYN","NNNNYY","YYYNNN","NYNNYN","YNNYYN", "YYNNNY","NYYNNY","NNYYYN","YNYYYN","NNNYNY","YYYYNN","YYNYNN","NYYNYY","YYNYYN"}

    Returns: 4

    This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.

     

    题意:

    每个人有自己会唱和不会唱的歌,求至少唱多少首歌,可以让每个人至少唱一次

    思路:

    题目的数据范围很小,最多只有10首歌,所以我们可以枚举所有的可能,总共才有2^10==1024种情况,然后用n^2的复杂度去判断每个人能否唱歌即可

    实现的时候使用位运算可以很巧妙且轻松

    下面是扒下来的dalao的代码,其中用到了GCC的内置函数(又涨姿势了。。),后面有一篇博文专门介绍GCC内置函数

    代码:

      1 #include<string>
      2 #include<vector>
      3 using namespace std;
      4 struct CarolsSinging
      5 {
      6   int choose(vector <string> lyrics)
      7   {
      8     int ret = 987654321;
      9     int n = lyrics[0].size();
     10     for(int carols = 0; carols < (1<<n); ++carols)  //利用位运算遍历每一种情况
     11     {
     12       bool succ = true;
     13       for(int i = 0; i < lyrics.size(); ++i)    //遍历每个人,判断是否每个人都能有歌唱
     14       {
     15         bool sings = false;
     16         for(int j = 0; j < lyrics[i].size(); ++j)   //遍历歌,判断这个人能唱这首歌吗?
     17           if(lyrics[i][j] == 'Y' && (carols & (1<<j)))
     18             sings = true;
     19         if(!sings) { succ = false; break; }
     20       }
     21       if(succ) ret =min(ret,__builtin_popcount(carols));
     22     }
     23     return ret;
     24   }
     25 };
     26 
  • 相关阅读:
    jquery中attr和prop的区别
    director.js:客户端的路由---简明中文教程
    MVC利用Routing实现多域名绑定一个站点、二级域名以及二级域名注册Area
    机器学习——几种分类算法的汇总
    图像预处理(二值化)
    深度学习训练数据打标签过程
    卷积神经网络(CNN)
    机器学习算法中的准确率(Precision)、召回率(Recall)、F值(F-Measure)
    TortoiseGit上传项目到GitHub
    机器学习中的数据清洗与特征工程
  • 原文地址:https://www.cnblogs.com/liuzhanshan/p/6861535.html
Copyright © 2011-2022 走看看