• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
LyonLys
愿意在角落唱沙哑的歌 再大声也都是给你 请用心听 不要说话 Contact me via E-mail: lyon.lys@gmail.com
博客园    首页    新随笔    联系   管理    订阅  订阅

hdu 3006 The Number of set

http://acm.hdu.edu.cn/showproblem.php?pid=3006

  状态压缩,位运算!看到别人题解里有这题,就看到这题是用状态压缩来做的,当时我想都不想就马上把状态压缩的代码打上去。不过我打着打着才发现好像不是那么简单,就算压缩了也不能搜索啊。。。囧!然后我又陷入一片窘境。。。。。

  后来想到了可以改变思考的方向,不是找集合合并后会产生怎样的集合。因为元素的个数是限定的,所以集合的上限是确定的,因此我想到了搜索每种情况是否可能达成。

代码如下:

View Code
 1 #include <cstdio>
 2 #include <cstring>
 3 #include <cassert>
 4 #include <algorithm>
 5 #include <vector>
 6 
 7 using namespace std;
 8 
 9 typedef vector<int> vi;
10 
11 vi Set;
12 
13 int deal(int n, int m){
14     Set.clear();
15     for (int i = 0; i < n; i++){
16         int k, a, tmp;
17 
18         scanf("%d", &k);
19         tmp = 0;
20         while (k--){
21             scanf("%d", &a);
22             tmp |= 1 << (a - 1);
23         }
24         Set.push_back(tmp);
25     }
26     int ret = 0, test;
27 
28     for (int i = 0, endi = 1 << m; i < endi; i++){
29         test = 0;
30         for (vi::iterator ii = Set.begin(); ii != Set.end(); ii++){
31             if ((i | *ii) == i){
32                 test |= *ii;
33                 if (test == i){
34                     ret++;
35                     break;
36                 }
37             }
38         }
39     }
40 
41     return ret;
42 }
43 
44 int main(){
45     int n, m;
46 
47     while (~scanf("%d%d", &n, &m)){
48         printf("%d\n", deal(n, m));
49     }
50 
51     return 0;
52 }

——written by Lyon

 

posted @ 2012-09-14 00:11  LyonLys  阅读(194)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3