#include #include #include #include int val(string s) { int w = 0; int pot = 1; for (int j=s.length()-1; j>=0; j--) { int dig=s[j] - '0'; w += pot*dig; pot *= 2; } return w; } int findOnes(string s2, char ch) { string s = s2; for (int j=s.length()-1; j>=0; j--) { if (s[j] == ch) s[j] = '1'; else s[j] = '0'; } return val(s); } struct Op { string s; int c; int xorOp, andOp, orOp; Op(string s, int c) : s(s), c(c) { xorOp = findOnes(s, 'F'); orOp = findOnes(s, 'S'); andOp = ~findOnes(s, 'C'); } int apply(int v) { return ((v | orOp) & andOp) ^ xorOp; } }; struct Word { int val; int cost; Word(int val, int cost) : val(val), cost(cost) {} bool operator<(const Word b) const { return cost < b.cost; } }; int len, nOp, nWords; const int Inf = 1000000000; int dist(vector ops, int start, int end) { int mx = 1 << len; vector words; for (int i=0; i q; for (int i=0; isecond; q.erase(q.begin()); for (int i=0; i> nCases; for (int caseNr=0; caseNr> len >> nOp >> nWords; vector ops; for (int o=0; o> s >> c; ops.push_back(Op(s, c)); } for (int i=0; i> start >> target; int d = dist(ops, val(start), val(target)); if (d == Inf) cout << "NP "; else cout << d << " "; } cout << endl; } }