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

SRM331-CarolsSinging(暴力,位运算)的更多相关文章

  1. Gym - 100203A Ariel 暴力+位运算

    题意:第i种生物有k[i]个特征,分数是score[i],现在要参加竞赛,报出一种生物a,和一些特征h[i],参加竞赛的所有生物在这些h[i]上面的特征是一样的,a生物有h[i],则所有竞赛的生物都必 ...

  2. UVA 11464 暴力+位运算 ***

    题意:给你一个 n * n 的 01 矩阵,现在你的任务是将这个矩阵中尽量少的 0 转化为 1 ,使得每个数的上下左右四个相邻的数加起来是偶数.求最少的转化个数. 新风格代码 lrj书上说的很清楚了, ...

  3. UVa 818Cutting Chains (暴力dfs+位运算+二进制法)

    题意:有 n 个圆环,其中有一些已经扣在一起了,现在要打开尽量少的环,使所有的环可以组成一条链. 析:刚开始看的时候,确实是不会啊....现在有点思路,但是还是差一点,方法也不够好,最后还是参考了网上 ...

  4. Codeforces 868D Huge Strings - 位运算 - 暴力

    You are given n strings s1, s2, ..., sn consisting of characters 0 and 1. m operations are performed ...

  5. Gym 100818I Olympic Parade(位运算)

    Olympic Parade http://acm.hust.edu.cn/vjudge/contest/view.action?cid=101594#problem/I [题意]: 给出N个数,找出 ...

  6. 【UVA】658 - It&#39;s not a Bug, it&#39;s a Feature!(隐式图 + 位运算)

    这题直接隐式图 + 位运算暴力搜出来的,2.5s险过,不是正法,做完这题做的最大收获就是学会了一些位运算的处理方式. 1.将s中二进制第k位变成0的处理方式: s = s & (~(1 < ...

  7. UVA 565 565 Pizza Anyone? (深搜 +位运算)

      Pizza Anyone?  You are responsible for ordering a large pizza for you and your friends. Each of th ...

  8. POJ 1753 位运算+枚举

    题意: 给出4*4的棋盘,只有黑棋和白棋,问你最少几步可以使棋子的颜色一样. 游戏规则是:如果翻动一个棋子,则该棋子上下左右的棋子也会翻一面,棋子正反面颜色相反. 思路: 都是暴搜枚举. 第一种方法: ...

  9. C语言中的位运算的技巧

    一.位运算实例 1.用一个表达式,判断一个数X是否是2的N次方(2,4,8,16.....),不可用循环语句. X:2,4,8,16转化成二进制是10,100,1000,10000.如果减1则变成01 ...

随机推荐

  1. vscode中eslint插件的配置-prettier

    用vue-cli构建vue项目,会有个eslint代码检测的安装 可vscode自带代码格式化是prettier格式(右键有格式化文件或alt+shift+f) 这时候要在vscode上装一个esli ...

  2. Radio stations CodeForces - 762E (cdq分治)

    大意: 给定$n$个三元组$(x,r,f)$, 求所有对$(i,j)$, 满足$i<j, |f_i-f_j|\le k, min(r_i,r_j)\ge |x_i-x_j|$ 按$r$降序排, ...

  3. markdown图片转换demo

    markdown图片转换demo 一直以来都是用Markdown来写博客的,但是它的图片嵌入实在是太让人头秃,逼得我能找网上的图片就不用自己的,实在是麻烦.所以我在发现了一个可以生成markdown样 ...

  4. python--命令(各个模块的安装)

    python命令行 退出python命令行:exit() 安装pymysql pip install pymysql 安装request pip install requests 1.安装django ...

  5. JQuery事件(2)

    jQuery 事件 下面是 jQuery 中事件方法的一些例子: Event 函数 绑定函数至 $(document).ready(function) 将函数绑定到文档的就绪事件(当文档完成加载时) ...

  6. webpack4 打包

    1. 基本安装及命令 npm config set registry https://registry.npm.taobao.org     //  淘宝镜像npm install webpack-c ...

  7. golang连接activemq,发送接收数据

    介绍 使用golang连接activemq发送数据的话,需要使用一个叫做stomp的包,直接go get github.com/go-stomp/stomp即可 代码 生产者 package main ...

  8. 华为ensp问题:云映射本地网卡,直连路由器可以ping通,pc却不行?

    拓扑图:cloud 云映射本机物理网卡:192.168.56.1     R1可以Ping通,所有Pc都不行,路由表也存在路由信息,不知道什么问题?

  9. auth

    谨记:使用的任何框架在网上都会有对应的auth代码,多百度,直接引用插件就好了 tp5 auth 示例:https://blog.csdn.net/strugglm/article/details/7 ...

  10. js 简单实现隐藏和显示

    <html> <head> <meta charset="gb2312"> <title>隐藏和显示</title> & ...