#include <iostream> #include <string> #include <map> #include <algorithm> using namespace std; int N; map<int,bool> parseC(int& i, const string& input) { map<int,bool> values; while (input.at(i) != ')') { if (input.at(i) == '~') { i+=2; int j = i+1; while(true) { if (input.at(j) == ')' || input.at(j) == ' ') break; j++; } int nx = stoi(input.substr(i, j-i)) * -1; values.insert({abs(nx), false}); i = j; } else if (input.at(i) == 'x') { i++; int j = i+1; while(true) { if (input.at(j) == ')' || input.at(j) == ' ') break; j++; } int x = stoi(input.substr(i, j-i)); values.insert({x, true}); i=j; } else { i++; } } return values; } void integrate(map<int, bool>& val, map<int, bool>& constants) { if (val.size() == 1) { constants.insert(val.begin(), val.end()); } else { int bad = val.size(); pair<int, bool> last; bool isAlwaysTrue = false; for (auto v : val) { if (constants.find(v.first) != constants.end() && !isAlwaysTrue) { if (constants.at(v.first) == v.second) { isAlwaysTrue = true; break; } else { bad--; } } else { last = v; } } if (bad == 1) { constants.insert(last); } } } int unknownNumber(const map<int, bool>& v1, const map<int, bool>& constants) { int un =0; for (auto v : v1) { if (constants.find(v.first)==constants.end()) { un ++; } } return un; } int configurations(vector<bool> truefalse, const std::vector<map<int,bool>> mapVector) { if (truefalse.size() == N) { for (auto map : mapVector) { bool anyTrueInC = false; for (int i=0;i<N;i++) { if (map.find(i+1) != map.end() && map[i+1] == truefalse[i]) { anyTrueInC = true; break; } } if (!anyTrueInC) { return 0; } } return 1; } else { int points = 0; for (bool k : {true, false}) { vector<bool> tf = truefalse; tf.push_back(k); points += configurations(tf, mapVector); } return points; } } int main() { map<int, bool> constants; cin >> N; string input; cin >> input; std::vector<map<int,bool>> mapVector; for (int i=0;i<input.size();i++) { if (input.at(i) == '(') { i++; map<int, bool> val = parseC(i, input); mapVector.push_back(val); } } // while (!mapVector.empty()) // { // sort(mapVector.begin(), mapVector.end(), [&constants](const map<int, bool>& v1, const map<int, bool>& v2){ // return unknownNumber(v1, constants) < unknownNumber(v2, constants); // }); // if (unknownNumber(mapVector.at(0), constants) == 1) // { // integrate(mapVector.at(0), constants); // mapVector.erase(mapVector.begin()); // } // else // { // break; // } // } cout << configurations({}, mapVector); cout << endl; }
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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 | #include <iostream> #include <string> #include <map> #include <algorithm> using namespace std; int N; map<int,bool> parseC(int& i, const string& input) { map<int,bool> values; while (input.at(i) != ')') { if (input.at(i) == '~') { i+=2; int j = i+1; while(true) { if (input.at(j) == ')' || input.at(j) == ' ') break; j++; } int nx = stoi(input.substr(i, j-i)) * -1; values.insert({abs(nx), false}); i = j; } else if (input.at(i) == 'x') { i++; int j = i+1; while(true) { if (input.at(j) == ')' || input.at(j) == ' ') break; j++; } int x = stoi(input.substr(i, j-i)); values.insert({x, true}); i=j; } else { i++; } } return values; } void integrate(map<int, bool>& val, map<int, bool>& constants) { if (val.size() == 1) { constants.insert(val.begin(), val.end()); } else { int bad = val.size(); pair<int, bool> last; bool isAlwaysTrue = false; for (auto v : val) { if (constants.find(v.first) != constants.end() && !isAlwaysTrue) { if (constants.at(v.first) == v.second) { isAlwaysTrue = true; break; } else { bad--; } } else { last = v; } } if (bad == 1) { constants.insert(last); } } } int unknownNumber(const map<int, bool>& v1, const map<int, bool>& constants) { int un =0; for (auto v : v1) { if (constants.find(v.first)==constants.end()) { un ++; } } return un; } int configurations(vector<bool> truefalse, const std::vector<map<int,bool>> mapVector) { if (truefalse.size() == N) { for (auto map : mapVector) { bool anyTrueInC = false; for (int i=0;i<N;i++) { if (map.find(i+1) != map.end() && map[i+1] == truefalse[i]) { anyTrueInC = true; break; } } if (!anyTrueInC) { return 0; } } return 1; } else { int points = 0; for (bool k : {true, false}) { vector<bool> tf = truefalse; tf.push_back(k); points += configurations(tf, mapVector); } return points; } } int main() { map<int, bool> constants; cin >> N; string input; cin >> input; std::vector<map<int,bool>> mapVector; for (int i=0;i<input.size();i++) { if (input.at(i) == '(') { i++; map<int, bool> val = parseC(i, input); mapVector.push_back(val); } } // while (!mapVector.empty()) // { // sort(mapVector.begin(), mapVector.end(), [&constants](const map<int, bool>& v1, const map<int, bool>& v2){ // return unknownNumber(v1, constants) < unknownNumber(v2, constants); // }); // if (unknownNumber(mapVector.at(0), constants) == 1) // { // integrate(mapVector.at(0), constants); // mapVector.erase(mapVector.begin()); // } // else // { // break; // } // } cout << configurations({}, mapVector); cout << endl; } |