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. python中迭代器和生成器的详细解释

    https://www.cnblogs.com/wilber2013/p/4652531.html

  2. 从入门到自闭之Python函数初识

    函数初识 定义:def--关键字 ​ 将某个功能封装到一个空间中就是一个函数 功能: ​ 减少重复代码 函数的调用 ​ 函数名+():调用函数和接收返回值 函数的返回值 return 值 == 返回值 ...

  3. jenkins-docker部署

    安装docker http://www.cnblogs.com/cjsblogs/p/8717304.html 安装jenkins mkdir -p /root/dockerfile/base/cen ...

  4. mysql转换表的存储引擎方法

    如果转换表的存储引擎,将会丢失原存储引擎的所有特性. 例如:如果将innodb转换成myisam,再转回innodb,原innodb表的的外键将丢失. 假设默认存储引擎是MyISAM转为InnoDB ...

  5. innodb中一颗B+树能存储多少条数据

    如图,为B+树组织数据的方式: 实际存储时当然不会每个节点只存3条数据. 以InnoDB引擎为例,简单计算一下一颗B+树可以存放多少行数据. B+树特点:只有叶子节点存储数据,而非叶子节点存放的是用来 ...

  6. workerman 实践 及 不能多人连接的问题

    官网:https://www.workerman.net/ 手册地址:https://www.workerman.net/doc 追加内容: 请在开发前多读读 开发必读http://doc.worke ...

  7. java实现spark常用算子之frist

    import org.apache.spark.SparkConf;import org.apache.spark.api.java.JavaRDD;import org.apache.spark.a ...

  8. spring boot入门与进阶教程

    SpringBoot入门.SpringBoot进阶.Spring Cloud微服务.Spring Ecosystem 微服务相关.Spring Boot 入门 IDEA 版本.Spring Boot集 ...

  9. js特效背景--点线随着鼠标移动而改变

    https://blog.csdn.net/css33/article/details/89450852 https://www.cnblogs.com/qq597585136/p/7019755.h ...

  10. HTTP/HTTPS协议 & GraphQL(非RESTFUL方式)

    HTTP访问控制-跨域资源共享(CORS) 缓存管理 HTTP VS HTTPS架构 TLS协议 HTTPS会话劫持 基于HTTP协议的服务器消息机制 1. Longpoll 2. SSE 3. We ...