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 
  • 相关阅读:
    离屏渲染说明文章地址
    苹果文档文章查看地址
    仿照GPUImageMovieOutput写的只支持BGRA32的视频Buffer读取
    拍照摄像拉近摄像头
    消除nonnull警告
    人体姿态识别
    AR资讯文章
    获取图片中对象轮廓并替换白色
    jar包和war包的介绍和区别(转载)
    css美化Div边框的样式实例*(转载)
  • 原文地址:https://www.cnblogs.com/liuzhanshan/p/6861535.html
Copyright © 2011-2022 走看看