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
SRM331-CarolsSinging(暴力,位运算)的更多相关文章
- Gym - 100203A Ariel 暴力+位运算
题意:第i种生物有k[i]个特征,分数是score[i],现在要参加竞赛,报出一种生物a,和一些特征h[i],参加竞赛的所有生物在这些h[i]上面的特征是一样的,a生物有h[i],则所有竞赛的生物都必 ...
- UVA 11464 暴力+位运算 ***
题意:给你一个 n * n 的 01 矩阵,现在你的任务是将这个矩阵中尽量少的 0 转化为 1 ,使得每个数的上下左右四个相邻的数加起来是偶数.求最少的转化个数. 新风格代码 lrj书上说的很清楚了, ...
- UVa 818Cutting Chains (暴力dfs+位运算+二进制法)
题意:有 n 个圆环,其中有一些已经扣在一起了,现在要打开尽量少的环,使所有的环可以组成一条链. 析:刚开始看的时候,确实是不会啊....现在有点思路,但是还是差一点,方法也不够好,最后还是参考了网上 ...
- Codeforces 868D Huge Strings - 位运算 - 暴力
You are given n strings s1, s2, ..., sn consisting of characters 0 and 1. m operations are performed ...
- Gym 100818I Olympic Parade(位运算)
Olympic Parade http://acm.hust.edu.cn/vjudge/contest/view.action?cid=101594#problem/I [题意]: 给出N个数,找出 ...
- 【UVA】658 - It's not a Bug, it's a Feature!(隐式图 + 位运算)
这题直接隐式图 + 位运算暴力搜出来的,2.5s险过,不是正法,做完这题做的最大收获就是学会了一些位运算的处理方式. 1.将s中二进制第k位变成0的处理方式: s = s & (~(1 < ...
- UVA 565 565 Pizza Anyone? (深搜 +位运算)
Pizza Anyone? You are responsible for ordering a large pizza for you and your friends. Each of th ...
- POJ 1753 位运算+枚举
题意: 给出4*4的棋盘,只有黑棋和白棋,问你最少几步可以使棋子的颜色一样. 游戏规则是:如果翻动一个棋子,则该棋子上下左右的棋子也会翻一面,棋子正反面颜色相反. 思路: 都是暴搜枚举. 第一种方法: ...
- C语言中的位运算的技巧
一.位运算实例 1.用一个表达式,判断一个数X是否是2的N次方(2,4,8,16.....),不可用循环语句. X:2,4,8,16转化成二进制是10,100,1000,10000.如果减1则变成01 ...
随机推荐
- CSS3鼠标悬停翻转按钮
在线演示 本地下载
- Docker 构建私有镜像仓库
在使用Docker一段时间后,往往会发现手头积累了大量的自定义镜像文件,这些文件通过公有仓库进行管理并不方便,另外有时候只是希望在内部用户之间进行分享,不希望暴露出去.这种情况下,就有必要搭建一个本地 ...
- [Nest] 02.nest之控制器
控制器 Controller Nest 的核心概念 模块 Module 控制器 Controller 服务与依赖注入 Provider Dependency injection 控制器负责处理应用的特 ...
- jQuery选择器引入
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...
- 二、redis学习(java操作redis缓存的工具jedis)
- 禁止ios10双指缩放
document.addEventListener('gesturestart', function(event) { event.preventDefault(); });
- 转载: utm坐标和经纬度相互转换
原文地址: https://blog.csdn.net/hanshuobest/article/details/77752279 //经纬度转utm坐标 int convert_lonlat_utm( ...
- subversion(SVN)服务配置及使用方法
1.安装 yum install httpd httpd-devel subversion mod_dav_svn mod_auth_mysql -y 2.查看版本 svnserve --vers ...
- python 出现OSError: [Errno 8] Exec format error的原因
访问 .py文件的网页的时候会出现 Exec format error的问题, 一般情况下是由于基于Unix(Linux,Mac OS)系统下的问题,办法如下 1 .chmod +x filenam ...
- idea 注册码 2月
https://blog.csdn.net/zhw0596/article/details/81394870 不显示.java后缀 https://segmentfault.com/q/1010000 ...