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 
  • 相关阅读:
    2015生命之旅---第三站象山之行
    我的八年程序之路(四)程序路上的新起点
    2015生命之旅---第二站长沙杭州
    2015生命之旅---第一站重庆
    我的八年程序之路(三)为了理想放弃高薪
    锋友分享:国行和非国行iPhone的送修需知
    iPhone被盗后续更新二:被换机!已取机!没扣住新机!怎么找新机呢?事发半年后跟进...
    iPhone被盗后续更新一:怎么找老机
    iPhone被盗后怎么?这篇文章只办针对iOS7后的系统
    四个小诀窍 告诉你雪景怎么拍才能更好看
  • 原文地址:https://www.cnblogs.com/liuzhanshan/p/6861535.html
Copyright © 2011-2022 走看看