#include <stdio.h> #include <string> #include <vector> #include <stdlib.h> std::vector<std::pair<int,int> > rules; int main(){ int n; scanf("%d",&n); char word[100]; int cl; int mask; while(scanf("%s",word) != EOF){ std::string str(word); int end = 0; if (str[0] == '^' || str[0] == 'v') continue; if (str[0] == '('){ cl = 0; mask = 0; str = str.substr(1, str.size() - 1); } if (str[str.size() - 1] == ')'){ str = str.substr(0, str.size() - 1); end = 1; } int neg = (str[0] == '~'); int pos; if (neg) pos = atoi(str.substr(2,str.size() - 2).c_str()); else pos = atoi(str.substr(1,str.size() - 1).c_str()); if (!neg) cl = cl | 1 << (pos - 1); mask = mask | 1 << (pos - 1); //printf("%d %d\n", neg, pos); if (end) { rules.push_back(std::pair<int,int>(cl,mask)); //printf("============== %d %d\n",cl,mask); } } int numberMoves = 1 << n; int total = 0; int hit; for(int vars = 0; vars < numberMoves; vars++){ hit = 1; for(int j = 0; j < (int)rules.size() && hit; j++){ if (((vars & rules[j].second) ^ rules[j].first) == rules[j].second) hit = 0; } total += hit; } printf("%d\n",total); return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | #include <stdio.h> #include <string> #include <vector> #include <stdlib.h> std::vector<std::pair<int,int> > rules; int main(){ int n; scanf("%d",&n); char word[100]; int cl; int mask; while(scanf("%s",word) != EOF){ std::string str(word); int end = 0; if (str[0] == '^' || str[0] == 'v') continue; if (str[0] == '('){ cl = 0; mask = 0; str = str.substr(1, str.size() - 1); } if (str[str.size() - 1] == ')'){ str = str.substr(0, str.size() - 1); end = 1; } int neg = (str[0] == '~'); int pos; if (neg) pos = atoi(str.substr(2,str.size() - 2).c_str()); else pos = atoi(str.substr(1,str.size() - 1).c_str()); if (!neg) cl = cl | 1 << (pos - 1); mask = mask | 1 << (pos - 1); //printf("%d %d\n", neg, pos); if (end) { rules.push_back(std::pair<int,int>(cl,mask)); //printf("============== %d %d\n",cl,mask); } } int numberMoves = 1 << n; int total = 0; int hit; for(int vars = 0; vars < numberMoves; vars++){ hit = 1; for(int j = 0; j < (int)rules.size() && hit; j++){ if (((vars & rules[j].second) ^ rules[j].first) == rules[j].second) hit = 0; } total += hit; } printf("%d\n",total); return 0; } |