ZOJ 3954 Seven-Segment Display (预处理)
2017-04-12 11:51
260 查看
解题思路
首先考虑将 9 个数字所表示的所有可能字符串按状压的形式表示成long long类型的长整数。例如当 1~9 分别被表示成串 {“1001111”,”0010010”,”0000110”,”1001100”,”0100100”,”0100000”,”0001111”,”0000000”,”0000100”} 时,其对应的长整数为 79×1288+18×1287+6×1286+76×1285+36×1284+32×1283+15×1282+0×1281+4×1280。
鉴于题意中表述的
a~g可互相交换,故对于每组
a~g表示的位置不同, 1~9 共有 7! 种不同的表示方法。同时,由于对 input 的判断有些数字可能不出现,故长整数表示的数的值共有 7!×29 。打表预处理。
对于每次 input ,同样按照上述操作将其用长整数表示,同时判断是否存在该长整数。总复杂度为 O(T×log(7!×29))
代码
#include<bits/stdc++.h> using namespace std; set<long long> st; string s[10] = {"1001111","0010010","0000110","1001100","0100100","0100000","0001111","0000000","0000100"}; char k[10]; int num[10]; void dfs(int idx, long long tot) { if(idx == 9){ st.insert(tot); return; } for(int i=0;i<2;i++) dfs(idx+1, tot*128 + num[idx]*i); } void init() { int arr[7] = {0, 1, 2, 3, 4, 5, 6}; do { for(int i=0;i<9;i++) { num[i] = 0; for(int j=0;j<7;j++) (num[i] *= 2) += s[i][ arr[j] ]=='1'?1:0; } dfs(0, 0); } while(next_permutation(arr, arr+7)); } int main() { init(); int T, n, x; scanf("%d",&T); while(T--) { scanf("%d",&n); memset(num, 0, sizeof(num)); for(int i=0;i<n;i++) { scanf("%d %s",&x, k); for(int j=0;j<7;j++) (num[x]*=2) += (k[j]=='1'?1:0); } long long tot = 0; for(int i=1;i<=9;i++) (tot *= 128) += num[i]; if(st.find(tot) != st.end()) printf("YES\n"); else printf("NO\n"); } }
相关文章推荐
- ZOJ 3954 Seven-Segment Display (预处理/状态压缩)
- zoj 3954 Seven-Segment Display 位运算
- ZOJ 3954 Seven-Segment Display(思维)
- zoj 3954 Seven-Segment Display 位运算
- zoj 3954 Seven-Segment Display 位运算
- zoj3954 Seven-Segment Display
- zoj 3954 Seven-Segment Display 位运算
- zoj 3954 Seven-Segment Display 位运算
- ZOJ 3954 Seven-Segment Display
- zoj 3954 Seven-Segment Display 位运算
- zoj 3954 Seven-Segment Display
- zoj 3954 Seven-Segment Display 位运算
- zoj 3954 Seven-Segment Display 位运算
- ZOJ 3954 Seven-Segment Display
- zoj 3954 Seven-Segment Display 位运算
- zoj 3954 Seven-Segment Display 位运算
- ZOJ-3954-Seven-Segment Display【17th浙大校赛】
- zoj 3954 Seven-Segment Display 位运算
- zoj 3954 Seven-Segment Display 位运算
- zoj 3954 Seven-Segment Display 位运算