Problem:
Problem C:
Midnight Teatime
ICPCの国内予選に備えて問題を解いていた僕は, その日3つ目のAcceptを貰ったところでキーボードを叩く手を止めた. 時計を見れば, もう日付が変わろうかという時刻だ. 紅茶とお菓子で一服して, 今日はもう寝ることにしよう. そう思って僕はキッチンへと向かった.
ダージリンのかぐわしい香りがキッチンを満たした頃, 妹がやってきた. 受験生である彼女は今日もこんな時間まで真面目に勉強していたようだ. 僕は彼女を誘って, 小さな深夜のお茶会を開くことにした.
都合の良いことに, キッチンには4つのお菓子があった. これをただ2人で分けるのもつまらないので,  僕はこのお菓子を賭けて簡単なゲームをしないかと提案した. そのゲームの内容を説明しよう.
まず最初に, 僕はどのノードも0個または2個の子を持つような二分木を書く. 次に, その木の葉に当たる(つまり, 子を持たない)ノードに, それぞれ S = {a, b, c, d} の任意の部分集合を書き込む. 4つのお菓子は, それぞれ a, b, c, d に対応する.
最後に妹は, 木の内部接点(つまり、2つの子を持つ)ノードに 'A', 'O', 'X' のいずれかの文字を書き込む.
妹は, 木の根にあたるノードが示すお菓子を得る. ただし, ノードが示すお菓子とは,
そのノードが葉であれば, そこに書かれているお菓子
そのノードが内部接点であれば,
そのノードに書かれた文字が A のとき、 sl と sr の積集合
そのノードに書かれた文字が O のとき、 sl と sr の和集合
そのノードに書かれた文字が X のとき、 sl と sr の対称差
のことである. ここで、sl はそのノードの左の子ノードが示すお菓子, sr はそのノードの右の子ノードが示すお菓子を指す. 2つの集合の対称差は, どちらか一方の集合にのみ含まれるような元から成る集合である.
このゲームに妹は乗ってきた. それどころか, お菓子を4つとも巻き上げてやろうと目を輝かせている. 出来れば僕の分も残しておいてくれると嬉しいのだけれども.
僕が書いた木に対して, 妹が全てのお菓子を得られるような内部接点の書き込み方は, いったい何通りあるだろうか？
Input
入力ファイルは、複数のデータセットを含む.
データセットの最初の行には, 木の情報が与えられる. 木の記述は,
"(" <左部分木の記述> <一つのスペース> <右部分木の記述> ")"
または,
<一つの数字>
のどちらかの形式を取る. 前者が内部接点の記述, 後者が葉の記述である.
次の行は1つの整数 N (N < 10) を含み, 続く N 行に葉に書き込まれた部分集合の情報が与えられる.
部分集合の情報は, 空白文字で区切られた4つの数字で表される. 4つの文字はそれぞれ, その部分集合が a, b, c, d を含むかどうかを表す. 含むならば 1 が、含まないならば 0 が与えられる.
葉の記述として与えられた数字が n であるとき、その葉に書き込まれた部分集合はこれら N 個のうち n 番目のものである. 1 ≤ n ≤ N と仮定してよい.
与えられる木は、最大 8 個の内部接点を含む.
木の記述の代わりに与えられる "END" という文字列が, 入力の終わりを表す.
Output
各データセットについて, 妹が全てのお菓子を得られるような内部接点の書き込み方の数を、一行に出力せよ.
Sample Input
(1 2)
2
0 1 0 1
1 0 1 0
((1 2) 3)
3
1 1 0 0
1 0 1 0
0 0 0 1
END
Output for the Sample Input
2
2

Submitted solutions:

code_snippet: #include <bits/stdc++.h>

constexpr int ROOT = 0;
constexpr int NONE = -1;

struct BinaryTree {
    static constexpr int N = 10;
    struct Node {
        int left;
        int right;
        int id = NONE;

        bool is_child() {
            return left == NONE && right == NONE;
        }

        Node(int left, int right, int id = NONE): left(left), right(right), id(id) {}
    };

    std::vector<Node> nodes;
    int internal_count = 0;

    BinaryTree() {}

    int add_node(bool is_child = false) {
        int size = nodes.size();
        if (is_child) {
            nodes.emplace_back(NONE, NONE);
        } else {
            nodes.emplace_back(NONE, NONE, internal_count++);
        }
        return size;
    }

    Node& node(int u) {
        return nodes[u];
    }

    int size() {
        return nodes.size();
    }
};

unsigned root_state(BinaryTree tree, std::vector<unsigned> leaf_state, const std::vector<char>& node_state) {
    auto dfs = [&](auto self, int u) -> unsigned {
        int i = tree.node(u).id;
        if (tree.node(u).is_child()) {
            return leaf_state[i];
        }
        unsigned left = self(self, tree.node(u).left);
        unsigned right = self(self, tree.node(u).right);
        if (node_state[i] == 'A') {
            return left & right;
        } else if (node_state[i] == 'O') {
            return left | right;
        } else {
            return left ^ right;
        }
    };

    return dfs(dfs, ROOT);
}

struct Parser {
    using CopyIter = std::string::const_iterator;
    using Iter = CopyIter&;
    std::string line;
    BinaryTree tree;
    Parser(std::string s): line(s) {}

    void skip(Iter it, char c) {
        ++it;
    }

    BinaryTree parse() {
        auto it = line.cbegin();
        binary_tree(it, ROOT);
        return tree;
    }

    int binary_tree(Iter it, int u) {
        if (*it != '(') {
            return child(it);
        }
        tree.add_node();

        {
            skip(it, '(');
            int left = binary_tree(it, tree.size());
            tree.node(u).left = left;
        }
        skip(it, ' ');
        {
            int right = binary_tree(it, tree.size());
            tree.node(u).right = right;
            skip(it, ')');
        }

        return u;
    }

    int child(Iter it) {
        int num = number(it) - 1;
        int v = tree.add_node(true);

        tree.node(v).id = num;
        return v;
    }

    int number(Iter it) {
        int res = 0;
        while (it != line.end() && std::isdigit(*it)) {
            res = 10 * res + *it - '0';
            it++;
        }
        return res;
    }
};

int main() {
    std::string line;
    while (true) {
        std::getline(std::cin, line);
        if (line == "END") break;
        Parser parser(line);
        BinaryTree tree = parser.parse();

        int size = tree.size();

        int n;
        std::cin >> n;
        std::vector<unsigned> leaf_state(n);
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < 4; j++) {
                unsigned v;
                std::cin >> v;
                leaf_state[i] |= (v << j);
            }
        }

        int m = 1;
        std::vector<char> node_state(tree.internal_count);
        int ans = 0;
        for (int i = 0; i < tree.internal_count; i++) m *= 3;

        for (int i = 0; i < m; i++) {
            int k = i;
            for (int j = 0; j < tree.internal_count; j++) {
                int digit = k % 3;
                k /= 3;
                if (digit == 0)
                    node_state[j] = 'A';
                else if (digit == 1)
                    node_state[j] = 'O';
                else
                    node_state[j] = 'X';
            }
            if (root_state(tree, leaf_state, node_state) == 0b1111) {
                ans++;
            }
        }
        std::cout << ans << std::endl;
        std::cin.ignore();
    }
}
accuracy: 1
time_ms: 10
memory_kb: 3372
final_rank: 17

code_snippet: #include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <bitset>
#include <climits>
#include <string>
#include <cmath>
#include <bitset>
#include <complex>
#include <functional>
#include <ctime>
#include <cassert>
#include <fstream>
#include <stack>
#include <random>
#include <iomanip>
#include <time.h>
#include <list>
#include <unordered_map>

#include <algorithm>
#include <array>

using namespace std;
typedef long long ll;
typedef long double dd;
typedef unsigned long long ull;
typedef vector<ll> vl;
typedef vector<dd> vd;
typedef vector<vector<ll>> vvl;
typedef vector<vector<dd>> vvd;
//#define i_7 (ll)(1E9+7)
#define i_7 998244353
#define i_5 i_7-2
ll mod(ll a){
    ll c=a%i_7;
    if(c>=0)return c;
    return c+i_7;
}
typedef pair<ll,ll> l_l;
typedef pair<dd,dd> d_d;
ll inf=(ll)1E18;
#define rep(i,l,r) for(ll i=l;i<=r;i++)
#define pb push_back
ll max(ll a,ll b){if(a<b)return b;else return a;}
ll min(ll a,ll b){if(a>b)return b;else return a;}
dd EPS=1E-9;
dd PI=acos(-1);
#define endl "\n"
#define fastio ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);





bool is_op(char c){
    vector<char> ops = {'+', '-', '*', '/'};
    rep(i,0,(ll)ops.size()-1){
        if(c == ops[i]){
            return true;
        }
    }
    return false;
}
string sNumber(string &s, ll &cur){
    string res;
    while(isdigit(s[cur])){
        res += s[cur];
        cur++;
    }
    return res;
}
ll Number(string &s, ll &cur){
    ll res=0;
    while(isdigit(s[cur])){
        res *= 10;
        res += s[cur] - '0';
        cur++;
    }
    return res;
}


ll cnt=0;

ll exp(string &s, ll &cur, const vl &v, const vl &u){
    if(s[cur] == '('){
        assert(s[cur]=='(');cur++;
        ll le = exp(s, cur, v, u);
        assert(s[cur]==' ');cur++;
        ll ri = exp(s, cur, v, u);
        assert(s[cur]==')');cur++;
        ll res;
        if(u[cnt] == 0){
            res = le&ri;
        }else if(u[cnt] == 1){
            res = le|ri;
        }else{
            res = le^ri;
        }
        cnt++;
        return res;
    }else{
        ll idx = Number(s, cur);
        return v[idx-1];
    }
}

signed main(){fastio
    while(true){
        string s;
        getline(cin, s);
        if(s=="END")break;
        string sn;
        getline(cin, sn);
        ll n = stoll(sn);
        vl v(n,0);
        rep(i,0,n-1){
            string t;
            getline(cin, t);
            v[i] = 0;
            for(ll j=0;j<8;j+=2){
                v[i] += (1<<(j/2))*(t[j]-'0');
            }
        }
        ll m = 0;
        rep(i,0,s.size()-1){
            if(s[i] == '(')m++;
        }
        ll M = powl(3, m);
        ll ans=0;
        rep(i,0,M-1){
            ll now=i;
            vl u;
            rep(j,0,m-1){
                u.pb(now%3);
                //cout<<now%3<<' ';
                now/=3;
            }
            ll cur=0;
            cnt=0;
            ll res=exp(s, cur, v, u);
            //assert(cnt == m-1);
            //cout<<res<<endl;
            if(res == 15){
                ans++;
            }
        }
        cout<<ans<<endl;
    }
    
}
accuracy: 1
time_ms: 10
memory_kb: 3512
final_rank: 19

code_snippet: #include <cstdio>
#include <cstdint>
#include <cassert>
#include <cctype>
#include <string>
#include <vector>
#include <algorithm>

bool next_operation(std::string &s) {
  for (size_t i=s.length(); i--;) {
    if (s[i] == 'A') {
      s[i] = 'O';
      return true;
    } else if (s[i] == 'O') {
      s[i] = 'X';
      return true;
    } else if (s[i] == 'X') {
      s[i] = 'A';
    }
  }
  return false;
}

int parse(const std::string &s, size_t &i, size_t preced=0) {
  const std::vector<std::string> ops={"AOX"};

  if (preced == ops.size()) {
    if (s[i] == '(') {
      int res=parse(s, ++i, 0);
      assert(s[i] == ')');
      ++i;
      return res;
    }
    if ('0' <= s[i] && s[i] < '0'+16) {
      return s[i++]-'0';
    }
    assert(false);
  }

  int lhs=parse(s, i, preced+1);
  while (i < s.length()) {
    char op=s[i];
    if (!isupper(op)) break;
    int rhs=parse(s, ++i, preced+1);
    if (op == 'A') lhs &= rhs;
    if (op == 'O') lhs |= rhs;
    if (op == 'X') lhs ^= rhs;
  }
  return lhs;
}

int testcase_ends() {
  char buf[1024];
  scanf(" %[^\n]", buf);
  std::string s=buf;
  if (s == "END") return 1;

  size_t n;
  scanf("%zu", &n);
  std::vector<int> item(n);
  for (size_t i=0; i<n; ++i) {
    for (int j=0; j<4; ++j) {
      int x;
      scanf("%d", &x);
      item[i] |= x<<j;
    }
  }

  {
    for (size_t i=0; i<s.length(); ++i) {
      if (isdigit(s[i])) {
        s[i] = item[s[i]-'1']+'0';
      } else if (s[i] == ' ') {
        s[i] = 'A';
      }
    }
  }

  int res=0;
  do {
    size_t i=0;
    if (parse(s, i) == 15) ++res;
  } while (next_operation(s));
  printf("%d\n", res);
  return 0;
}

int main() {
  while (!testcase_ends()) {}
}
accuracy: 1
time_ms: 130
memory_kb: 2724
final_rank: 18

code_snippet: #include "bits/stdc++.h"
#include<unordered_map>
#include<unordered_set>
#pragma warning(disable:4996)
using namespace std;
using ld = long double;
template<class t>
using table = vector<vector<t>>;
const ld eps = 1e-9;

//// < "d:\d_download\visual studio 2015\projects\programing_contest_c++\debug\a.txt" > "d:\d_download\visual studio 2015\projects\programing_contest_c++\debug\b.txt"
  
vector<int>aget(const vector<int>&l, const vector<int>&r,const int type) {
	vector<int>v(4);
	for (int i = 0; i < 4; ++i) {
		if (type == 0)v[i] = l[i] || r[i];
		else if (type == 1)v[i] = l[i] && r[i];
		else v[i] = l[i] ^ r[i];
	}
	return v;
}

struct node {
	int type;
	node* l;
	node* r;

	node() : l(nullptr), r(nullptr) {
		type = -1;
	}
};


string st;
int a = 0;
int  num = 0;
vector<vector<int>>cakes;
node* make_tree() {
	node* no=new node();
	if (st[a] == '(') {
		num++;
		no->type = -num;
		a++;
		no->l = make_tree();
		a++;
		assert(st[a] == ' ');
		a++;
		no->r = make_tree();
		a++;
		assert(st[a] == ')');
	}
	else {
		no->type = st[a] - '1';
	}
	return no;
}

vector<int>getans(node* now,const vector<int>&types) {
	if (now->type <0) {
		return aget(getans(now->l, types), getans(now->r, types), types[-now->type - 1]);
	}
	else {
		return cakes[now->type];
	}
}

long long int powint(long long int a, long long int b) {
	assert(b >= 0);
	if (b == 0)return 1;
	if (b == 1)return a;
	long long int ans = 1;
	long long int aa = powint(a, b / 2);
	ans *= aa*aa;
	if (b % 2)ans *= a;
	return ans;
}

int main() {
	while (getline(cin, st)) {
		cakes.clear();
		if (st == "END")break;
		a = 0;
		num = 0;
		node* root(make_tree());
		int M; cin >> M;
		for (int i = 0; i < M; ++i) {
			vector<int>v(4);
			for (int j = 0; j < 4; ++j)cin >> v[j];
			cakes.emplace_back(v);
		}
		int sum = 0;
		for (int i = 0; i <powint(3,num); ++i) {
			vector<int>types(num);
			{
				int n(i);
				for (int i = 0; i < num; ++i) {
					types[i] = n % 3;
					n /= 3;
				}
			}
			auto ans = getans(root, types);
			if (all_of(ans.begin(), ans.end(), [](const int a) {return a == 1; })) {
				sum++;
			}
		}
		cout << sum << endl;
		getline(cin, st);
	}

	return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 3132
final_rank: 16

code_snippet: #include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<map>
#include<set>
#include<utility>
#include<cmath>
#include<cstring>
#include<queue>
#include<cstdio>
#include<sstream>
#define loop(i,a,b) for(int i=a;i<b;i++) 
#define rep(i,a) loop(i,0,a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(),in.end()
const double PI=acos(-1);
const double EPS=1e-8;
const int inf=1e9;
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pii;
string s;
vvi in;
vi w;
int n,m,out;
int a,b;
vi f(){
	int e=w[b];
	a++;
	vi ss(4),t(4);
	if(s[a]=='('){b++;ss=f();}
	else ss=in[s[a]-'1'];
	a++;
	if(s[a]=='('){b++;t=f();}
	else t=in[s[a]-'1'];
	vi re(4);
	rep(i,4)if(e==0)re[i]=(ss[i]&&t[i]);
	else if(e==1)re[i]=(ss[i]||t[i]);
	else if(e==2)re[i]=(ss[i]^t[i]);
	a++;
	return re;
}
int main(){
	while(getline(cin,s),s!="END"){
		rep(i,s.size())if(s[i]==' '){
			s.erase(s.begin()+i);
			i--;
		}
		m=out=0;
		rep(i,s.size())if(s[i]=='(')m++;
		cin>>n;
		in=vvi(n,vi(4));
		w=vi(m);
		rep(i,n)rep(j,4)cin>>in[i][j];
		
		int q=pow(3,m);
		rep(i,q){
			int t=i;
			rep(j,m){
				w[j]=t%3;
				t/=3;
			}
			a=b=0;
			vi tmp=f();
			sort(all(tmp));
			if(tmp[0]==1)out++;
		}
		cout<<out<<endl;
		getline(cin,s);
	}
}
accuracy: 1
time_ms: 80
memory_kb: 1280
final_rank: 13

code_snippet: #include <iostream>
#include <string>
using namespace std;
string s;
int n,b[10],cnt,sum,aox[8],ans,p,k;
void func(int);
int calc();

int main(){
  int a,len;
  while(1){
    getline(cin,s);
    if(s=="END") break;
    cin>>n;
    for(int i=1;i<=n;i++){
      b[i]=0;
      for(int j=0;j<4;j++) cin>>a,b[i]=(b[i]|(a<<j));
    }
    sum=0;
    len=s.size();
    for(int i=0;i<len;i++)
      if(s[i]=='(') sum++;
    ans=0;
    func(0);
    cout<<ans<<endl;
    getline(cin,s);
  }
  return 0;
}

void func(int d){
  if(d==sum){
    p=k=0;
    if(calc()==15) ans++;
    return;
  }
  for(int i=0;i<3;i++) aox[d]=i,func(d+1);
}

int calc(){
  int r,left,right;
  if(s[p]=='('){
    p++;
    left=calc();
    p++;
    right=calc();
    p++;
    if(aox[k]==0) r=left&right;
    if(aox[k]==1) r=left|right;
    if(aox[k]==2) r=left^right;
    k++;
  }else{
    int x=s[p]-'0';
    r=b[x];
    p++;
  }
  return r;
}
accuracy: 1
time_ms: 10
memory_kb: 1208
final_rank: 5

code_snippet: #include <bits/stdc++.h>
using namespace std;
struct P {
  int l,r,d;
}p[10000];

string s;
int k;
map<int,int> ma;
int a[11];

void dfs(int x) {
  while(k<s.size()) {
    if(s[k]=='(') {
      k++;
      p[x].l=x*2;
      dfs(x*2);
    }
    if(s[k]==' ') {
      k++;
      p[x].r=x*2+1;
      dfs(x*2+1);
    }
    if(isdigit(s[k])) {
      int y=0;
      while(k<s.size()&&isdigit(s[k])) {
        y*=10;
        y+=s[k]-'0';
        k++;
      }
      p[x].d=y;
      break;
    }
    if(s[k]==')') {
      k++;
      break;
    }
  }
}

int dfs2(int x) {
  if(p[x].l!=-1) {
    int ll=dfs2(p[x].l);
    int rr=dfs2(p[x].r);
    int r=ma[x];
    if(r==0) return ll&rr;
    else if(r==1) return ll|rr;
    return ll^rr;
  }
  return a[p[x].d-1];
}

int main() {
  while(1) {
    getline(cin,s);
    if(s=="END") break;
    int n;
    cin >> n;
    memset(a,0,sizeof(a));
    for(int i=0; i<n; i++) {
      for(int j=0,c; j<4; j++) {
        cin >> c;
        a[i]|=c<<j;
      }
    }
    for(int i=0; i<10000; i++) p[i]=(P){-1,-1,-1};
    k=0;
    dfs(1);
    vector<int> v;
    for(int i=0; i<10000; i++) {
      if(p[i].l!=-1) v.push_back(i);
    }
    int m=v.size(),ans=0;
    for(int t=0; t<(1<<(2*m)); t++) {
      bool f=1;
      ma.clear();
      for(int i=0; i<m; i++) {
        int b=(t>>(i*2))%4;
        if(b==3) f=0;
        else ma[v[i]]=b;
      }
      if(!f) continue;
      if(dfs2(1)==15) ans++;
    }
    cout << ans << endl;
    getline(cin,s);
  }
  return 0;
}
accuracy: 1
time_ms: 210
memory_kb: 1344
final_rank: 15

code_snippet: #include <bits/stdc++.h>

using namespace std;

int Tree[1<<12],m;
map<int,int> mp;
vector<int> order;
string::iterator it;

vector<int> getOrder(int x,int m){
    vector<int> res;
    while(x > 0){
        res.push_back(x%3);
        x /= 3;
    }
    while((int)res.size() != m){
        res.push_back(0);
    }
    return res;
}

int getBit(int v,vector<int> &bit){
    if(Tree[v] != -1){
        return bit[Tree[v]];
    }
    int S = -1;
    int l = getBit(2*v+1, bit);
    int r = getBit(2*v+2, bit);
    switch(order[mp[v]]){
    case 0:
        S = l & r;
        break;
    case 1:
        S = l | r;
        break;
    case 2:
        S = (l | r) - (l & r);
        break;
    };
    return S;
}

bool check(vector<int> &bit){
    return (getBit(0,bit) == (1<<4)-1);
}

int makeTree(int v){
    if(*it == '('){
        ++it;
        char cl = *it;
        int l = makeTree(2*v+1);
        if(isdigit(cl)){
            Tree[2*v+1] = l;
        }
        ++it;
        char cr = *it;
        int r = makeTree(2*v+2);
        if(isdigit(cr)){
            Tree[2*v+2] = r;
        }
        ++it;
    }else if(isdigit(*it)){
        int num = ((*it - '0') - 1);
        ++it;
        return num;
    }
    return -1;
}

void dfs(int v){
    if(Tree[v] == -1){
        mp[v] = m++;
        dfs(2*v+1);
        dfs(2*v+2);
    }
}

int main(){
    string s;
    while(getline(cin,s), s != "END"){
        int N,x;
        cin >> N;
        vector<int> bit(N, 0);
        for(int i = 0 ; i < N ; i++){
            for(int j = 0 ; j < 4 ; j++){
                cin >> x;
                if(x == 1) bit[i] |= 1<<j;
            }
        }
        int res = 0;
        memset(Tree,-1,sizeof(Tree));
        it = s.begin();
        makeTree(0); m = 0; dfs(0);
        int n = pow(3., m);
        for(int i = 0 ; i < n ; i++){
            order = getOrder(i, m);
            if(check(bit)) res++;
        }
        cout << res << endl;
        cin.ignore();
    }
    return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 1256
final_rank: 9

code_snippet: #define _USE_MATH_DEFINES
#define INF 0x3f3f3f3f
#include <iostream>
#include <sstream>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <limits>
#include <map>
#include <string>
#include <cstring>
#include <set>
#include <deque>
#include <bitset>
#include <list>
 
using namespace std;
 
typedef long long ll;
typedef pair <int,int> P;
 
static const double EPS = 1e-8;

static const int tx[] = {0,1,0,-1};
static const int ty[] = {-1,0,1,0};

// <TREE> := (<LEAF><OPE><LEAF>)
// <LEAF> := (<TREE>) | <NUMBER>

P tree(const string& expr,int sweets[16],int pos);
P leaf(const string& expr,int sweets[16],int pos);

P tree(const string& expr,int sweets[16],int pos){
  int set = 0;
  if(expr[pos] == '('){
    P r1 = leaf(expr,sweets,pos + 1);
    pos = r1.first;
    char ope = expr[pos];
    P r2 = leaf(expr,sweets,pos+1);
    if(ope == 'A'){
      set |= (r1.second & r2.second);
    }
    else if(ope == 'O'){
      set |= (r1.second | r2.second);
    }
    else if(ope == 'X'){
      set |= (r1.second ^ r2.second);
    }
    pos = r2.first;
  }
  return P(pos,set);
}

P leaf(const string& expr,int sweets[16], int pos){
  if(expr[pos] == '('){
    P r = tree(expr,sweets,pos);
    pos = r.first + 1;
    return P(pos,r.second);
  }
  else{
    return P(pos+1,sweets[expr[pos] - '0']);
  }
}

int dfs(string expr,int sweets[16],int pos,int N){
  if(pos >= expr.size()){
    P r = tree(expr,sweets,0);
    if(r.second == (1<<4) - 1){
      return 1;
    }
    else{
      return 0;
    }
  }

  int res = 0;
  const char ope[] = {'A','O','X'};
  if(expr[pos] == ' '){
    for(int i = 0; i < 3; i++){
      expr[pos] = ope[i];
      res += dfs(expr,sweets,pos+1,N);
    }
  }
  else{
    res += dfs(expr,sweets,pos+1,N);
  }
  return res;
}

int main(){
  string expr;
  while(getline(cin,expr)){
    if(expr == "END") break;
    int N;
    string N_str;
    getline(cin,N_str);
    N = atoi(N_str.c_str());
    int sweets[16];
    for(int i = 1; i <= N; i++){
      int bit = 0;
      string flag_str;
      getline(cin,flag_str);
      for(int j = 0; j <= 6; j+=2){
        if(flag_str[j] - '0'){
          bit |= (1<<(j/2));
        }
      }
      sweets[i] = bit;
    }
    printf("%d\n",dfs(expr,sweets,0,N));
  }
}
accuracy: 1
time_ms: 30
memory_kb: 1220
final_rank: 8

code_snippet: #include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
char str[110];
char w[5]="AOX";
int at[20];
int var[20];
int pow3[20];
int cur;
int expr(){
	if(str[cur]=='('){
		cur++;
		int L=expr();
		char ex=str[cur++];
		int R=expr();
		cur++;
		if(ex=='A')return L&R;
		if(ex=='X')return L^R;
		return L|R;
	}else{
		return var[str[cur++]-'1'];
	}
}
int main(){
	pow3[0]=1;
	for(int i=1;i<20;i++)pow3[i]=pow3[i-1]*3;
	while(1){
		gets(str);
		if(str[0]=='E')return 0;
		int sz=0;
		for(int i=0;str[i];i++)if(str[i]==' '){
			at[sz++]=i;
		}
		int n;scanf("%d",&n);
		for(int i=0;i<n;i++){
			var[i]=0;
			for(int j=0;j<4;j++){
				int p;scanf("%d",&p);
				if(p)var[i]+=(1<<j);
			}
		}
		int ret=0;
		for(int i=0;i<pow3[sz];i++){
			for(int j=0;j<sz;j++){
				str[at[j]]=w[i%pow3[j+1]/pow3[j]];
			}
		//	printf("%s\n",str);
			cur=0;
			if(expr()==15)ret++;
		}
		printf("%d\n",ret);gets(str);
	}
}
accuracy: 1
time_ms: 10
memory_kb: 1032
final_rank: 4

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <sstream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <numeric>
#include <cctype>
#include <tuple>

#ifdef _MSC_VER
#include <agents.h>
#endif

#define FOR(i, a, b) for(int i = (a); i < (int)(b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define REV(v) v.rbegin(), v.rend()
#define MEMSET(v, s) memset(v, s, sizeof(v))
#define X first
#define Y second

using namespace std;

typedef long long ll;
typedef pair<int, int> P;

int x[15];
int type[15];
string s;
int num;

int parse(int l, int r){
	if (r-l == 1) return x[s[l] - '1'];

	int sp = -1, nest = 0;
	for (int i = l+1; i < r-1; ++i){
		if (s[i] == '(') ++nest;
		if (s[i] == ')') --nest;
		if (!nest && s[i] == ' ') sp = i;
	}

	int res1 = parse(l + 1, sp);
	int res2 = parse(sp + 1, r - 1);

	int ret = -1;
	if (type[num] == 0) ret = res1 | res2;
	if (type[num] == 1) ret = res1 & res2;
	if (type[num] == 2) ret = res1 ^ res2;
	++num;
	return ret;
}


int ans;
int m;
void rec(int n){
	if (n == m){
		num = 0;
		//cout << parse(0, s.size()) << endl;
		ans += parse(0, s.size()) == 15;
		return;
	}
	rep(i, 3){
		type[n] = i;
		rec(n + 1);
	}
}

int main(){
	while (getline(cin, s), s != "END"){
		int n;
		cin >> n;

		MEMSET(x, 0);
		rep(i, n) rep(j, 4){
			int t;
			cin >> t;
			x[i] |= (t << j);
		}
		ans = 0;
		m = count(ALL(s), ' ');
		rec(0);
		cout << ans << endl;
		cin.ignore();
	}


	return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 1216
final_rank: 7

code_snippet: #include<iostream>
#include<vector>
#include<algorithm>
#include<bitset>
#include<cstdio>
#include<cassert>
#include<set>
#include<sstream>

#define REP(i,s,n) for(int i=s;i<n;i++)
#define rep(i,n) REP(i,0,n)
#define inf (1<<29)
#define MAX 20

using namespace std;

string parse;
char  cparse[MAX];
int N,sweets[MAX],n;

set<vector<char> > ans;
char opr[] = {'&','|','^'};

int exp(int&,int&,vector<char>&);

int fact(int &cur,int &ocnt,vector<char> &vec)
{
  int p;
  if(parse[cur] == '(')
    {
      cur++;
      p = exp(cur,ocnt,vec);
      cur++;
      return p;
    }
  else
    {
      p = parse[cur]-'0';
      cur++;
      return sweets[p-1];
    }
}

int exp(int &cur,int &ocnt,vector<char> &vec)
{
  int p;
  p = fact(cur,ocnt,vec);
  if(ocnt < vec.size())
    {
      int q = fact(cur,ocnt,vec);
      int ret;
      if(vec[ocnt] == '&')ret = p & q;
      if(vec[ocnt] == '|')ret = p | q;
      if(vec[ocnt] == '^')ret = p ^ q;
      ocnt++;
      return ret;
    }
  return p;
}


bool check(vector<char> &vec)
{
  int cur = 0;
  int ocnt = 0;
  int res = exp(cur,ocnt,vec);
  return res == (1<<4)-1;
}

void dfs(int cur,vector<char> &vec)
{
  if(cur >= vec.size())
    {
      if(check(vec))ans.insert(vec);
      return;
    }

  rep(i,3)
    {
      vec[cur] = opr[i];
      dfs(cur+1,vec);
    }

}

void change()
{
  stringstream ss;
  ss << parse;
  string content = "";
  while(!(ss>>parse).fail())content += parse;
  parse = content;
}

int main()
{
  while(getline(cin,parse),parse != "END")
    {
      n = 0;
      rep(i,parse.size())if(parse[i] == '(')n++;
      change();
      ans.clear();


      cin >> N;
      rep(i,N)
	{
	  sweets[i] = 0;
	  rep(j,4)
	    {
	      int bit;
	      cin >> bit;
	      if(bit)
		{
		  sweets[i] |= (1<<j);
		}
	    }
	}

      vector<char> vec(n);
      dfs(0,vec);

      cout << ans.size() << endl;
      cin.ignore();
    }
  return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 1412
final_rank: 10

code_snippet: #include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<cctype>

using namespace std;

vector<string> leaf;

vector<string> solve(string s){
  vector<string> a, b;
  if(s.size() == 5){
    a = vector<string>(1, leaf[s[1]-'0']);
    b = vector<string>(1, leaf[s[3]-'0']);
  }else if(isdigit(s[1])){
    a = vector<string>(1, leaf[s[1]-'0']);
    b = solve(string(s.begin()+3, s.end()-1));
  }else if(isdigit(s[s.size()-2])){
    a = solve(string(s.begin()+1, s.end()-3));
    b = vector<string>(1, leaf[s[s.size()-2]-'0']);
  }else{
    int x=0, k;
    for(int i=1; i<s.size(); ++i){
      if(s[i] == '(') ++x;
      if(s[i] == ')') --x;
      if(x == 0){ k=i+1; break; }
    }
    a = solve(string(s.begin()+1, s.begin()+k));
    b = solve(string(s.begin()+k+1, s.end()-1));
  }
  vector<string> data;
  for(int i=0; i<a.size(); ++i){
    for(int j=0; j<b.size(); ++j){
      string A(4,'0'), O(4,'0'), X(4,'0');
      for(int k=0; k<4; ++k){
	if(a[i][k]=='1' && b[j][k]=='1') A[k]='1';
	if(a[i][k]=='1' || b[j][k]=='1') O[k]='1';
	if(a[i][k] != b[j][k]) X[k]='1';
      }
      data.push_back(A);
      data.push_back(O);
      data.push_back(X);
    }
  }
  return data;
}

int main(){
  int n, x;
  string s;
  while(1){
    s.clear();
    leaf.clear();
    while(s.empty()) getline(cin, s);
    if(s == "END") break;
    cin >> n;
    leaf = vector<string>(n+1);
    for(int i=1; i<=n; ++i){
      string t(4,'0');
      for(int j=0; j<4; ++j){
	cin >> x;
	if(x == 1) t[j] = '1';
      }
      leaf[i] = t;
    }
    vector<string> data = solve(s);
    int ans=0;
    for(int i=0; i<data.size(); ++i){
      if(data[i] == "1111") ++ans;
    }
    cout << ans << endl;
  }
  return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 1464
final_rank: 11

code_snippet: #include <vector>
#include <list>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <deque>
#include <algorithm>
#include <utility>
#include <functional>
#include <sstream>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <climits>
#include <cassert>
using namespace std;
inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define EACH(t,i,c) for(t::iterator i=(c).begin(); i!=(c).end(); ++i)
const double EPS = 1e-10;
const double PI  = acos(-1.0);

int id_cnt=0;

struct node{
	node *left,*right;
	int n,id;
	node(node *left,node *right,int id):left(left),right(right),n(-1),id(id){}
	node(int n,int id):n(n),left(NULL),right(NULL),id(id){}

	vi search(vvi &masks,string &pattern){
		if(n!=-1){
			return masks[n];
		}
		vi l_mask=left->search(masks,pattern);
		vi r_mask=right->search(masks,pattern);
		vi ret(4);
		switch(pattern[id]){
		case 'A':
			REP(i,4){
				ret[i]=l_mask[i]&r_mask[i];
			}
			break;
		case 'O':
			REP(i,4){
				ret[i]=l_mask[i]|r_mask[i];
			}
			break;
		case 'X':
			REP(i,4){
				ret[i]=l_mask[i]^r_mask[i];
			}
			break;
		}
		return ret;
	}

	void make_initial_pattern(string &pattern){
		if(n!=-1){
			pattern[id]='.';
		}else{
			pattern[id]='A';
		}
		if(left){
			left->make_initial_pattern(pattern);
		}
		if(right){
			right->make_initial_pattern(pattern);
		}
	}
};


node *read(string &s, int &p){
	switch(s[p]){
	case '(':
		{
		p++;
		node *left=read(s,p);
		assert(s[p]==' ');
		p++;
		node *right=read(s,p);
		assert(s[p]==')');
		p++;
		return new node(left,right,id_cnt++);
		}
	default:
		assert(isdigit(s[p]));
		return new node(s[p++]-'0'-1,id_cnt++);
	}
}

bool increment(string &pattern,int n){
	if(n==pattern.size()){
		return false;
	}
	switch(pattern[n]){
	case 'A':
		pattern[n]='O';break;
	case 'O':
		pattern[n]='X';break;
	case 'X':
		pattern[n]='A';
		if(!increment(pattern,n+1)){
			return false;	
		}
		break;
	default:
		if(!increment(pattern,n+1)){
			return false;	
		}
	}
	return true;
}

int main(){
	string s;
	while(getline(cin,s),s!="END"){
		int p=0;
		node *root=read(s,p);
		int n;
		cin>>n;
		vvi masks(n);
		REP(i,n){
			REP(j,4){
				int b;
				cin>>b;
				masks[i].push_back(b);
			}
		}
		string pattern(id_cnt,' ');
		root->make_initial_pattern(pattern);
		int ans=0;
		while(1){
			vi m=root->search(masks,pattern);
			bool flag=true;
			REP(i,4){
				if(m[i]==0){
					flag=false;
					break;
				}
			}
			if(flag){
				ans++;
			}
			if(!increment(pattern,0)){
				break;
			}
		}
		cout<<ans<<endl;
		getline(cin,s);
	}
}
accuracy: 1
time_ms: 110
memory_kb: 1252
final_rank: 14

code_snippet: #include <iostream>
#include <string>
#include <vector>
#include <cctype>

using namespace std;

vector<int> g_sets;

struct Result {
        int value, p;
};

#define PARSER_DECL(name) \
        Result name (const string &s, int p)

PARSER_DECL(expr);
PARSER_DECL(factor);

PARSER_DECL(expr){
        Result r = factor(s, p);
        while(isupper(s[r.p])){
                Result q = factor(s, r.p + 1);
                switch(s[r.p]){
                        case 'A': r.value &= q.value; break;
                        case 'O': r.value |= q.value; break;
                        case 'X': r.value ^= q.value; break;
                }
                r.p = q.p;
        }
        return r;
}
PARSER_DECL(factor){
        Result r;
        if(s[p] == '('){
                r = expr(s, p + 1);
                ++r.p;
        }else{
                int index = 0;
                while(isdigit(s[p])){
                        index = (index * 10) + (s[p] - '0');
                        ++p;
                }
                r.value = g_sets[index];
                r.p = p;
        }
        return r;
}

int solve(string &s, const vector<int> &op, int i){
        int answer = 0;
        if(i == op.size()){
                answer = (expr(s, 0).value == 15 ? 1 : 0);
        }else{
                const char OPERATIONS[] = "AOX";
                for(int j = 0; j < 3; ++j){
                        s[op[i]] = OPERATIONS[j];
                        answer += solve(s, op, i + 1);
                }
        }
        return answer;
}

int main(){
        while(true){
                string s;
                getline(cin, s);
                if(s == "END"){ break; }
                vector<int> op;
                for(int i = 0; i < s.size(); ++i){
                        if(s[i] == ' '){ op.push_back(i); }
                }
                int n;
                cin >> n;
                g_sets = vector<int>(n + 1);
                for(int i = 1; i <= n; ++i){
                        int a, b, c, d;
                        cin >> a >> b >> c >> d;
                        g_sets[i] = (a << 3) | (b << 2) | (c << 1) | d;
                }
                cout << solve(s, op, 0) << endl;
                getline(cin, s);
        }
        return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 1240
final_rank: 6

code_snippet: #include<iostream>
#include<string>
#include<vector>
using namespace std;

int node[10];
string str;
int ans,a,n,cnt;
vector<int> v[10];

vector<int> parse(string s){
  if('1'<=s[0] && s[0]<= '9'){
    return v[s[0]-'0'-1];
  }

  int pos = 0;
  int k=0;
  while(pos<s.size()){
    if(s[pos]=='(')k++;
    if(s[pos]==')')k--;
    
    if(k==1 && s[pos] == ' '){
      vector<int> x = parse(s.substr(1,pos-1));
      vector<int> y = parse(s.substr(pos+1,(int)s.size()-2-pos));
      
      if(node[cnt] == 0){
	cnt++;
	vector<int> res;
	for(int i=0;i<4;i++)
	  res.push_back(x[i]&y[i]);	
	return res;
      }
      
      if(node[cnt] == 1){
	cnt++;
	vector<int> res;
	for(int i=0;i<4;i++)
	  res.push_back(x[i]|y[i]);	
	return res;
      }
      
      if(node[cnt] == 2){
	cnt++;
	vector<int> res;
	for(int i=0;i<4;i++)
	  res.push_back(x[i]!=y[i]);	
	return res;
      }
    }
    pos++;
  }
}

void rec(int s){
  if(s == a){
    cnt = 0;
    vector<int> tmp = parse(str);
    int pop=0;
 
    for(int i=0;i<4;i++)
      if(tmp[i])pop++;
    if(pop==4)ans++;
    
    return;
  }
  for(int i=0;i<3;i++){
    node[s] = i;
    rec(s+1);
  }
}

int main(){
  int c = 0;
  while(getline(cin,str)){
    if(str=="END")break;
    a = 0;
    for(int i=0;i<(int)str.size();i++)
      if(str[i] == ' ')a++;

    cin >> n;
    for(int i=0;i<n;i++){
      v[i].clear();
      int hoge;
      for(int j=0;j<4;j++){
	cin >> hoge;
	v[i].push_back(hoge);
      }
    }
    ans = 0;
    rec(0);
    cout << ans << endl;
    cin.ignore();
  }
}
accuracy: 1
time_ms: 580
memory_kb: 0
final_rank: 19

code_snippet: #include<stdio.h>


int cakeSets[10];//FXvZµÄÅãÉº4rbg¾¯ðg¤
int mask=15;//ºÊ4rbg}XN
int ans;

int calcNode(int& p,const char* nodeText,bool modeRe){
	int nowSet=0;
	p++;
	while(1){
		char t=nodeText[p];
		if('1'<=t && t<='9'){
			nowSet=cakeSets[t-'0'];
		}else if(t=='A'){
			nowSet&=calcNode(p,nodeText,true);
		}else if(t=='O'){
			nowSet|=calcNode(p,nodeText,true);
		}else if(t=='X'){
			nowSet^=calcNode(p,nodeText,true);
		}else if(t=='('){
			nowSet=calcNode(p,nodeText,false);
		}else{
			return nowSet;
		}
		if(modeRe==true) return nowSet;
		p++;
	}
	
}


void setNode(int p,char* nodeText){
	char t=nodeText[p];
	if(t=='\0'){
		int np=-1;
		//printf("%s=",nodeText);
		int k=calcNode(np,nodeText,true);
		//printf("%d %d %d %d\n",(k&1)!=0?1:0,(k&2)!=0?1:0,(k&4)!=0?1:0,(k&8)!=0?1:0);
		if(mask==(mask&k)){
			ans++;
		}
		
	}else if(t==' '){
		nodeText[p]='A';
		setNode(p+1,nodeText);
		nodeText[p]='O';
		setNode(p+1,nodeText);
		nodeText[p]='X';
		setNode(p+1,nodeText);
		nodeText[p]=' ';
	}else {
		setNode(p+1,nodeText);
	}
}




bool setData(){
	char nodeText[200],re;
	int p;

	ans=0;
	scanf("%[^\n]%c",nodeText,&re);
	if(nodeText[0]=='E')return false;
	int n,c;
	scanf("%d",&n);
	for(int i=1;i<=n;i++){
		cakeSets[i]=0;
		for(int j=0;j<4;j++){
			scanf("%d",&c);
			cakeSets[i]|=(c<<j);
		}
	}
	setNode(0,nodeText);
	printf("%d\n",ans);
	if(scanf("%c",&re)==EOF)return false;
	return true;
}


int main(){
	while(setData()){
	}
}
accuracy: 1
time_ms: 20
memory_kb: 0
final_rank: 1

code_snippet: #include <iostream>
#include <string>
#include <vector>
#include <cstdlib>
#include <cctype>
#include <map>

#define conv(c) (int)(c-'1')

using namespace std;

typedef pair<int, string> P; 

vector<string> vec;
string str;
map<P, string> mpp;

string func(string st)
{
  string s = "";
  for(int i = 0; i < st.size(); ++i)
    if(st[i] != ' ') s += st[i];

  return s;
}

void mkMap(string s, int d)
{
  if(d == 8){
    for(int i = 0; i < 3; ++i){
      string st = "";
      for(int j = 0; j < 4; ++j){
	if(i == 0){
	  st += ((s[j] == '1' || s[j+4] == '1')? "1" : "0"); 
	} else if(i == 1){
	  st += ((s[j] == '1' && s[j+4] == '1')? "1" : "0");
	} else {
	  st += ((s[j] != s[j+4])? "1" : "0");
	}
      }
      mpp[P(i, s)] = st;
    }
  } else {
    for(int i = 0; i < 2; ++i)
      mkMap(s+(char)('0'+i), d+1);
  }

  return ;
}

int p;
vector<string> rec()
{
  vector<string> v1, v2, res;
  if(isdigit(str[p])){
    v1.push_back(vec[conv(str[p])]);
    p+=2;
  } else {
    ++p;
    v1 = rec();
    ++p;
  }

  if(isdigit(str[p])){
    v2.push_back(vec[conv(str[p])]);
    p+=2;
  } else {
    ++p;
    v2 = rec();
    ++p;
  }

  for(int k = 0; k < v1.size(); ++k)
    for(int l = 0; l < v2.size(); ++l)
      for(int i = 0; i < 3; ++i)
	res.push_back(mpp[P(i, v1[k]+v2[l])]);

  return res;
}

int solve(string s)
{
  int cnt = 0;
  p = 1;
  vector<string> res = rec();

  for(int i = 0; i < res.size(); ++i)
    if(res[i] == s) cnt++;

  return cnt;
}

int main()
{
  int n;
  string s1, s2="";

  mkMap("", 0);
  for(int i = 0; i < 4; i++) s2 += "1";
  while(getline(cin, str) && str != "END"){
    getline(cin, s1);
    n = atoi(s1.c_str());

    while(n--){
      getline(cin, s1);
      vec.push_back(func(s1));
    }

    cout << solve(s2) << endl;
    vec.clear();
  }

  return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 0
final_rank: 3

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <cstdlib>
#include <stack>
#include <set>
#include <string>
#include <cmath>

using namespace std;

int sits[11];
int n;

multiset<int> analysis(string str){
  if(str[0]!='('){
	  multiset<int> si;
	  si.insert(sits[atoi(str.c_str())]);
	  return si;
  }
  else{
	  multiset<int> si;
    stack<char> s;

	for(int i = 0; i < str.size(); i++){
      if(str[i]=='('){
	s.push('(');
      }
      else if(str[i]==')'){
	s.pop();
      }
      else{
	if(s.size()==1&&str[i]==' '){
	  // ツ閉ェツ環つオツづ催帰
		string str1=str.substr(1,i-1);
		string str2=str.substr(i+1,str.size()-i-2);

		multiset<int> s1 = analysis(str1);
		multiset<int> s2 = analysis(str2);
	  // union
	  {
	    for(multiset<int>::iterator it = s1.begin(); it!=s1.end(); it++){
	      for(multiset<int>::iterator iit = s2.begin(); iit!=s2.end(); iit++){
		si.insert((*it)|(*iit));
	      }
	    }
	  }
	  // partition
	  {
	    for(multiset<int>::iterator it = s1.begin(); it!=s1.end(); it++){
	      for(multiset<int>::iterator iit = s2.begin(); iit!=s2.end(); iit++){
		si.insert((*it)&(*iit));
	      }
	    }
	  }
	  // diff
	  {
	    for(multiset<int>::iterator it = s1.begin(); it!=s1.end(); it++){
	      for(multiset<int>::iterator iit = s2.begin(); iit!=s2.end(); iit++){
		si.insert((*it)^(*iit));
	      }
	    }
	  }
	  return si;
	}
      }
    }
  }
}

int main(){

  string str;
  while(getline(cin,str)&&str!="END"){
	  cin>>n;
    for(int i = 0; i < n; i++){
      int s=0;
      for(int j = 0; j < 4; j++){
	int b;
	cin>>b;
	if(b==1)
	  s+=(int)pow(2.0,j);
      }
      sits[i+1]=s;
    }
	multiset<int> ms = analysis(str);
	int cnt=0;
	for(multiset<int>::iterator it = ms.begin(); it!= ms.end(); it++){
		if(*it==15)
			cnt++;
	}
	cout<<cnt<<endl;
	getline(cin,str);
  }
  

  return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 0
final_rank: 1

code_snippet: #include<algorithm>
#include<iostream>
#include<string>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
using namespace std;
struct bt{
  int a,b,c,d;
  int e;
  struct bt* l;
  struct bt* r;
};
int f(int a[][4],string s,struct bt* p,int e){
  int i,j;
  if(s[0]=='('){
    p->l=(struct bt*)malloc(sizeof(struct bt));
    p->r=(struct bt*)malloc(sizeof(struct bt));
    p->e=e;
    e++;
    if(s[1]=='('){
      j=1;
      for(i=2;j;i++){
	if(0){
	}else if(s[i]=='('){
	  j++;
	}else if(s[i]==')'){
	  j--;
	}
      }
      e=f(a,s.substr(1,i-1),p->l,e);
    }else{
      e=f(a,s.substr(1,1),p->l,e);
    }
    if(s[s.length()-2]==')'){
      j=1;
      for(i=s.length()-3;j;i--){
	if(0){
	}else if(s[i]==')'){
	  j++;
	}else if(s[i]=='('){
	  j--;
	}
      }
      e=f(a,s.substr(i+1,s.length()-i-2),p->r,e);
    }else{
      e=f(a,s.substr(s.length()-2,1),p->r,e);
    }
  }else{
    p->l=NULL;
    p->r=NULL;
    p->a=a[s[0]-'1'][0];
    p->b=a[s[0]-'1'][1];
    p->c=a[s[0]-'1'][2];
    p->d=a[s[0]-'1'][3];
  }
  return e;
}
struct bt g(struct bt* z,int i){
  struct bt a,b,c;
  if(z->l==NULL){
    return *z;
  }else{
    a=g(z->l,i);
    b=g(z->r,i);
    if(0){
    }else if(i/(int)pow(3,z->e)%3==0){
      c.a=a.a&b.a;
      c.b=a.b&b.b;
      c.c=a.c&b.c;
      c.d=a.d&b.d;
    }else if(i/(int)pow(3,z->e)%3==1){
      c.a=a.a|b.a;
      c.b=a.b|b.b;
      c.c=a.c|b.c;
      c.d=a.d|b.d;
    }else if(i/(int)pow(3,z->e)%3==2){
      c.a=a.a^b.a;
      c.b=a.b^b.b;
      c.c=a.c^b.c;
      c.d=a.d^b.d;
    }
    return c;
  }
}
int main(){
  int i,j;
  int a[9][4],n,e;
  int sm;
  string s;
  struct bt c;
  struct bt* z;
  while(getline(cin,s)&&s!="END"){
    cin>>n;
    for(i=0;i<n;i++)
      for(j=0;j<4;j++)
	cin>>a[i][j];
    z=(struct bt*)malloc(sizeof(struct bt));
    e=f(a,s,z,0);    
    sm=0;
    for(i=0;i<(int)pow(3,e);i++){
	c=g(z,i);
	sm+=c.a&c.b&c.c&c.d;
    }
    cout<<sm<<endl;
    getline(cin,s);
  } 
  return 0;
}
accuracy: 1
time_ms: 130
memory_kb: 964
final_rank: 12