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
最新文章
- 详解ASP.NET MVC的请求生命周期
- line-height的一点粗浅认识
- JavaScript基础--事件驱动和访问CSS技术(十)
- (Python学习4)List对象
- sell- 获取邮箱的后缀
- 【转】双机高可用、负载均衡、MySQL(读写分离、主从自动切换)架构设计
- std::advance 给迭代器增加指定偏移量
- 一些正则在js使用方法
- Problem F: Exponentiation
- OCP读书笔记(25) - 题库(ExamE)
- 关于java集合排序
- 做电子商务的七个SEO技巧
- Junit单元测试实例
- 使用伪类before和after
- 《深入理解Java虚拟机》-----第6章 类文件结构——Java高级开发必须懂的
- HttpClientFactory与Steeltoe结合来完成服务发现
- ProcessingElement.h
- WPF中应用字体图标
- UVA12253 简单加密法 Simple Encryption
- ClickHouse之初步认识