1. 程式人生 > 實用技巧 >Automatic Control Machine(bitset+二進位制列舉)

Automatic Control Machine(bitset+二進位制列舉)

Automatic Control Machine

題解:\(bitset\)應用二進位制列舉

AC_Code:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cmath>
 4 #include <bitset>
 5 #include <string>
 6 #include <algorithm>
 7 #include <cstring>
 8 using namespace std;
 9 typedef long long ll;
10 const int maxn = 505; 11 const double eps = 1e-8; 12 13 char s[maxn]; 14 bitset<maxn>p[20]; 15 16 int main() 17 { 18 int t; scanf("%d",&t); 19 while( t-- ){ 20 int n,m; scanf("%d%d",&n,&m); 21 for(int i=1;i<=m;i++){ 22 scanf("%s",s+1); 23 p[i].reset(); //
reset:把p[i]中所有二進位制位都置為"0" 24 for(int j=1;j<=n;j++){ 25 if( s[j]=='1' ) p[i].set(j); //set(j):把p[i]的j位置設成"1" 26 } 27 } 28 29 int base = (1<<m)-1, ans=20; 30 for(int i=0;i<=base;i++){ //暴力列舉每一種組合,i的二進位制:第x位"0"代表不選x,第x位為"1"代表選x 31
bitset<maxn>cnt; 32 int k=0; 33 for(int j=0;j<m;j++){ 34 if( i&(1<<j) ){ 35 cnt |= p[j+1]; 36 k++; 37 } 38 } 39 if((int)cnt.count()==n ) ans=min(ans,k); 40 } 41 if( ans==20 ) printf("-1\n"); 42 else printf("%d\n",ans); 43 } 44 return 0; 45 }

順便總結一點\(bitset\)的用法:

bitset的定義和初始化

bitset<n> b;
b有n位,每位都為0

bitset<n> b(u);
b是unsigned long型u的一個副本


bitset<n> b(s);
b是string物件s中含有的位串的副本


bitset<n> b(s, pos, n);
b是s中從位置pos開始的n個位的副本



bitset的操作

b.any()
b中是否存在置為1的二進位制位?


b.none()
b中不存在置為1的二進位制位嗎?


b.count()
b中置為1的二進位制位的個數


b.size()
b中二進位制位的個數


b[pos]
訪問b中在pos處的二進位制位


b.test(pos)
b中在pos處的二進位制位是否為1?


b.set()
把b中所有二進位制位都置為1


b.set(pos)
把b中在pos處的二進位制位置為1


b.reset()
把b中所有二進位制位都置為0


b.reset(pos)
把b中在pos處的二進位制位置為0


b.flip()
把b中所有二進位制位逐位取反


b.flip(pos)
把b中在pos處的二進位制位取反