Problem:
Problem Statement
You are given a rectangular board divided into square cells. The number of rows and columns in this board are $3$ and $3 M + 1$, respectively, where $M$ is a positive integer. The rows are numbered $1$ through $3$ from top to bottom, and the columns are numbered $1$ through $3 M + 1$ from left to right. The cell at the $i$-th row and the $j$-th column is denoted by $(i, j)$.
Each cell is either a floor cell or a wall cell. In addition, cells in columns $2, 3, 5, 6, \ldots, 3 M - 1, 3 M$ (numbers of form $3 k - 1$ or $3 k$, for $k = 1, 2, \ldots, M$) are painted in some color. There are $26$ colors which can be used for painting, and they are numbered $1$ through $26$. The other cells (in columns $1, 4, 7, \ldots, 3 M + 1$) are not painted and each of them is a floor cell.
You are going to play the following game. First, you put a token at cell $(2, 1)$. Then, you repeatedly move it to an adjacent floor cell. Two cells are considered adjacent if they share an edge. It is forbidden to move the token to a wall cell or out of the board. The objective of this game is to move the token to cell $(2, 3 M + 1)$.
For this game, $26$ magical switches are available for you! Switches are numbered $1$ through $26$ and each switch corresponds to the color with the same number. When you push switch $x$, each floor cell painted in color $x$ becomes a wall cell and each wall cell painted in color $x$ becomes a floor cell, simultaneously.
You are allowed to push some of the magical switches ONLY BEFORE you start moving the token. Determine whether there exists a set of switches to push such that you can achieve the objective of the game, and if there does, find such a set.
Input
The input is a sequence of at most $130$ datasets. Each dataset begins with a line containing an integer $M$ ($1 \le M \le 1{,}000$). The following three lines, each containing $3 M + 1$ characters, represent the board. The $j$-th character in the $i$-th of those lines describes the information of cell $(i, j)$, as follows:
The $x$-th uppercase letter indicates that cell $(i, j)$ is painted in color $x$ and it is initially a floor cell.
The $x$-th lowercase letter indicates that cell $(i, j)$ is painted in color $x$ and it is initially a wall cell.
A period (
.
) indicates that cell $(i, j)$ is not painted and so it is a floor cell.
Here you can assume that $j$ will be one of $1, 4, 7, \ldots, 3 M + 1$ if and only if that character is a period. 
The end of the input is indicated by a line with a single zero.
Output
For each dataset, output $-1$ if you cannot achieve the objective. Otherwise, output the set of switches you push in order to achieve the objective, in the following format:
$n$ $s_1$ $s_2$ ... $s_n$
Here, $n$ is the number of switches you push and $s_1, s_2, \ldots, s_n$ are uppercase letters corresponding to switches you push, where the $x$-th uppercase letter denotes switch $x$. These uppercase letters $s_1, s_2, \ldots, s_n$ must be distinct, while the order
...(truncated)

Submitted solutions:

code_snippet: #include <bits/stdc++.h>
using namespace std;

int T;
int N;
int dp1[1594323];
int dp2[1594323];
bool C[26][26][26][8];
char E[3][1 << 19];

bool check(int num, int cl, int cr) {
    for (int i = cl; i < cr; i++) {
        for (int j = i; j < cr; j++) {
            for (int k = j; k < cr; k++) {
                int parity = ((num >> i) & 1) + 2 * ((num >> j) & 1) + 4 * ((num >> k) & 1);
                if (C[i][j][k][parity] == false) return false;
            }
        }
    }
    return true;
}

void solve() {
    // Step 1. Randomize
    vector<int> Order;
    for (int i = 0; i < 26; i++) Order.push_back(i);
    for (int i = 0; i < 200; i++) swap(Order[rand() % 26], Order[rand() % 26]);
    int TotalMask = 0;
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3 * N + 1; j++) {
            if (E[i][j] == '.') continue;
            int idx = (E[i][j] - 'A');
            if (E[i][j] >= 'a') idx = (E[i][j] - 'a');
            TotalMask |= (1 << Order[idx]);
        }
    }
    
    // Step 2. Initialize
    for (int i = 0; i < 26; i++) {
        for (int j = 0; j < 26; j++) {
            for (int k = 0; k < 26; k++) {
                for (int l = 0; l < 8; l++) C[i][j][k][l] = true;
            }
        }
    }

    // Step 3. Initialize 2
    for (int i = 0; i < N; i++) {
        vector<vector<pair<int, int>>> vec(2, vector<pair<int, int>>(0));
        if (E[0][3 * i + 1] <= 'Z') vec[0].push_back(make_pair(E[0][3 * i + 1] - 'A', 1));
        if (E[0][3 * i + 1] >= 'a') vec[0].push_back(make_pair(E[0][3 * i + 1] - 'a', 0));
        if (E[0][3 * i + 2] <= 'Z') vec[1].push_back(make_pair(E[0][3 * i + 2] - 'A', 1));
        if (E[0][3 * i + 2] >= 'a') vec[1].push_back(make_pair(E[0][3 * i + 2] - 'a', 0));
        if (E[1][3 * i + 1] <= 'Z') vec[0].push_back(make_pair(E[1][3 * i + 1] - 'A', 1));
        if (E[1][3 * i + 1] >= 'a') vec[0].push_back(make_pair(E[1][3 * i + 1] - 'a', 0));
        if (E[1][3 * i + 2] <= 'Z') vec[1].push_back(make_pair(E[1][3 * i + 2] - 'A', 1));
        if (E[1][3 * i + 2] >= 'a') vec[1].push_back(make_pair(E[1][3 * i + 2] - 'a', 0));
        if (E[2][3 * i + 1] <= 'Z') vec[0].push_back(make_pair(E[2][3 * i + 1] - 'A', 1));
        if (E[2][3 * i + 1] >= 'a') vec[0].push_back(make_pair(E[2][3 * i + 1] - 'a', 0));
        if (E[2][3 * i + 2] <= 'Z') vec[1].push_back(make_pair(E[2][3 * i + 2] - 'A', 1));
        if (E[2][3 * i + 2] >= 'a') vec[1].push_back(make_pair(E[2][3 * i + 2] - 'a', 0));
        for (int j = 0; j < 8; j++) {
            vector<pair<int, int>> lis;
            int idx1 = Order[vec[(j / 1) % 2][0].first];
            int idx2 = Order[vec[(j / 2) % 2][1].first];
            int idx3 = Order[vec[(j / 4) % 2][2].first];
            int num1 = vec[(j / 1) % 2][0].second;
            int num2 = vec[(j / 2) % 2][1].second;
            int num3 = vec[(j / 4) % 2][2].second;
            lis.push_back(make_pair(idx1, num1));
            lis.push_back(make_pair(idx2, num2));
            lis.push_back(make_pair(idx3, num3));
            sort(lis.begin(), lis.end());
            C[lis[0].first][lis[1].first][lis[2].first][1 * lis[0].second + 2 * lis[1].second + 4 * lis[2].second] = false;
        }
    }

    // Step 4. Brute Force 1
    vector<int> mask1;
    vector<int> mask2;
    for (int i = 0; i < 8192; i++) {
        if (check(i <<  0,  0, 13) == true && ((i <<  0) | TotalMask) == TotalMask) mask1.push_back(i << 0);
        if (check(i << 13, 13, 26) == true && ((i << 13) | TotalMask) == TotalMask) mask2.push_back(i << 13);
    }
    for (int i = 0; i < mask1.size() * 4; i++) swap(mask1[rand() % mask1.size()], mask1[rand() % mask1.size()]);
    for (int i = 0; i < mask2.size() * 4; i++) swap(mask2[rand() % mask2.size()], mask2[rand() % mask2.size()]);

    // Step 5. Brute Force 2
    for (int i : mask1) {
        for (int j : mask2) {
            if (check(i + j, 0, 26) == true) {
                vector<int> Rev(26, 0);
                for (int k = 0; k < 26; k++) Rev[Order[k]] = k;
                vector<char> Answer;
                for (int k = 0; k < 26; k++) {
                    if (((i + j) >> k) & 1) Answer.push_back((char)('A' + Rev[k]));
                }
                sort(Answer.begin(), Answer.end());
                cout << Answer.size();
                for (int k = 0; k < (int)Answer.size(); k++) cout << " " << Answer[k];
                cout << endl;
                return;
            }
        }
    }

    // Step 6. Return
    cout << "-1" << endl;
}

int main() {
    while (true) {
        cin >> N; if (N == 0) break;
        for (int j = 0; j < 3; j++) {
            for (int k = 0; k < 3 * N + 1; k++) cin >> E[j][k];
        }
        solve();
    }
    return 0;
}
accuracy: 1
time_ms: 4330
memory_kb: 3376
final_rank: 13

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define mp make_pair
#define si(x) int(x.size())
const int mod=1000000007,MAX=300005,INF=1<<30;

int main(){
    
    std::ifstream in("text.txt");
    std::cin.rdbuf(in.rdbuf());
    cin.tie(0);
    ios::sync_with_stdio(false);
    
    mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
    
    while(1){
        int N;cin>>N;
        if(N==0) break;
        vector<string> S(3);
        for(int i=0;i<3;i++) cin>>S[i];
        vector<array<int,3>> E;
        for(int j=1;j<3*N;j+=3){
            for(int bit=0;bit<8;bit++){
                string T;
                for(int i=0;i<3;i++){
                    if(bit&(1<<i)) T+=S[i][j+1];
                    else T+=S[i][j];
                }
                array<int,3> pu;
                for(int i=0;i<3;i++){
                    if('a'<=T[i]&&T[i]<='z'){
                        pu[i]=(T[i]-'a');
                    }else{
                        pu[i]=~(T[i]-'A');
                    }
                }
                sort(all(pu));
                bool f=false;
                for(int i=0;i<3;i++){
                    for(int j=i+1;j<3;j++){
                        if(pu[i]==(~pu[j])) f=true;
                    }
                }
                if(!f) E.push_back(pu);
            }
        }
        
        sort(all(E));
        E.erase(unique(all(E)),E.end());
        
        if(si(E)){
            for(int q=0;q<10000;q++){
                int a=rng()%si(E),b=rng()%si(E);
                swap(E[a],E[b]);
            }
        }
        
        bool ans=false;
        string res;
        
        for(int q=0;q<10000;q++){
            int X=rng()%(1<<26);
            int la=0;
            for(int t=0;t<3*26;t++){
                bool ok=true;
                for(int ii=la;ii<la+si(E);ii++){
                    int i=ii;
                    if(i>=si(E)) i-=si(E);
                    int cn=0;
                    for(int j=0;j<3;j++){
                        if(E[i][j]<0){
                            int k=~E[i][j];
                            if(!(X&(1<<k))) cn++;
                        }else{
                            int k=E[i][j];
                            if(X&(1<<k)) cn++;
                        }
                    }
                    if(cn==0){
                        ok=false;
                        int j=rng()%3;
                        if(E[i][j]<0){
                            int k=~E[i][j];
                            X^=(1<<k);
                        }else{
                            int k=E[i][j];
                            X^=(1<<k);
                        }
                        la=i;
                        break;
                    }
                }
                if(ok){
                    ans=true;
                    for(int i=0;i<26;i++){
                        if(X&(1<<i)) res+=char('A'+i);
                    }
                    break;
                }
            }
            if(ans) break;
        }
        
        if(ans){
            cout<<si(res);
            for(char c:res) cout<<" "<<c;
            cout<<"\n";
        }else{
            cout<<-1<<"\n";
        }
    }
}
accuracy: 1
time_ms: 6110
memory_kb: 3516
final_rank: 16

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define mp make_pair
#define si(x) int(x.size())
const int mod=1000000007,MAX=300005,INF=1<<30;

int main(){
    
    std::ifstream in("text.txt");
    std::cin.rdbuf(in.rdbuf());
    cin.tie(0);
    ios::sync_with_stdio(false);
    
    mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
    
    while(1){
        int N;cin>>N;
        if(N==0) break;
        vector<string> S(3);
        for(int i=0;i<3;i++) cin>>S[i];
        vector<array<int,3>> E;
        for(int j=1;j<3*N;j+=3){
            for(int bit=0;bit<8;bit++){
                string T;
                for(int i=0;i<3;i++){
                    if(bit&(1<<i)) T+=S[i][j+1];
                    else T+=S[i][j];
                }
                array<int,3> pu;
                for(int i=0;i<3;i++){
                    if('a'<=T[i]&&T[i]<='z'){
                        pu[i]=(T[i]-'a');
                    }else{
                        pu[i]=~(T[i]-'A');
                    }
                }
                sort(all(pu));
                bool f=false;
                for(int i=0;i<3;i++){
                    for(int j=i+1;j<3;j++){
                        if(pu[i]==(~pu[j])) f=true;
                    }
                }
                if(!f) E.push_back(pu);
            }
        }
        
        sort(all(E));
        E.erase(unique(all(E)),E.end());
        
        if(si(E)){
            for(int q=0;q<10000;q++){
                int a=rng()%si(E),b=rng()%si(E);
                swap(E[a],E[b]);
            }
        }
        
        bool ans=false;
        string res;
        
        for(int q=0;q<10000;q++){
            int X=rng()%(1<<26);
            for(int t=0;t<3*26;t++){
                bool ok=true;
                for(int i=0;i<si(E);i++){
                    int cn=0;
                    for(int j=0;j<3;j++){
                        if(E[i][j]<0){
                            int k=~E[i][j];
                            if(!(X&(1<<k))) cn++;
                        }else{
                            int k=E[i][j];
                            if(X&(1<<k)) cn++;
                        }
                    }
                    if(cn==0){
                        ok=false;
                        int j=rng()%3;
                        if(E[i][j]<0){
                            int k=~E[i][j];
                            X^=(1<<k);
                        }else{
                            int k=E[i][j];
                            X^=(1<<k);
                        }
                        break;
                    }
                }
                if(ok){
                    ans=true;
                    for(int i=0;i<26;i++){
                        if(X&(1<<i)) res+=char('A'+i);
                    }
                    break;
                }
            }
            if(ans) break;
        }
        
        if(ans){
            cout<<si(res);
            for(char c:res) cout<<" "<<c;
            cout<<"\n";
        }else{
            cout<<-1<<"\n";
        }
    }
}
accuracy: 1
time_ms: 7780
memory_kb: 3300
final_rank: 18

code_snippet: #pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<unordered_set>
#include<utility>
#include<cassert>
#include<complex>
#include<numeric>
#include<array>
#include<chrono>
using namespace std;

//#define int long long
typedef long long ll;

typedef unsigned long long ul;
typedef unsigned int ui;
constexpr ll mod = 998244353;
//constexpr ll mod = 1000000007;
const ll INF = mod * mod;
typedef pair<int, int>P;

#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
#define all(v) (v).begin(),(v).end()
typedef pair<ll, ll> LP;

template<typename T>
void chmin(T& a, T b) {
	a = min(a, b);
}
template<typename T>
void chmax(T& a, T b) {
	a = max(a, b);
}
template<typename T>
void cinarray(vector<T>& v) {
	rep(i, v.size())cin >> v[i];
}
template<typename T>
void coutarray(vector<T>& v) {
	rep(i, v.size()) {
		if (i > 0)cout << " "; cout << v[i];
	}
	cout << "\n";
}
ll mod_pow(ll x, ll n, ll m = mod) {
	if (n < 0) {
		ll res = mod_pow(x, -n, m);
		return mod_pow(res, m - 2, m);
	}
	if (abs(x) >= m)x %= m;
	if (x < 0)x += m;
	//if (x == 0)return 0;
	ll res = 1;
	while (n) {
		if (n & 1)res = res * x % m;
		x = x * x % m; n >>= 1;
	}
	return res;
}
struct modint {
	int n;
	modint() :n(0) { ; }
	modint(ll m) {
		if (m < 0 || mod <= m) {
			m %= mod; if (m < 0)m += mod;
		}
		n = m;
	}
	operator int() { return n; }
};
bool operator==(modint a, modint b) { return a.n == b.n; }
bool operator<(modint a, modint b) { return a.n < b.n; }
modint operator+=(modint& a, modint b) { a.n += b.n; if (a.n >= mod)a.n -= mod; return a; }
modint operator-=(modint& a, modint b) { a.n -= b.n; if (a.n < 0)a.n += mod; return a; }
modint operator*=(modint& a, modint b) { a.n = ((ll)a.n * b.n) % mod; return a; }
modint operator+(modint a, modint b) { return a += b; }
modint operator-(modint a, modint b) { return a -= b; }
modint operator*(modint a, modint b) { return a *= b; }
modint operator^(modint a, ll n) {
	if (n == 0)return modint(1);
	modint res = (a * a) ^ (n / 2);
	if (n % 2)res = res * a;
	return res;
}

ll inv(ll a, ll p) {
	return (a == 1 ? 1 : (1 - p * inv(p % a, a)) / a + p);
}
modint operator/(modint a, modint b) { return a * modint(inv(b, mod)); }
modint operator/=(modint& a, modint b) { a = a / b; return a; }
const int max_n = 1 << 20;
modint fact[max_n], factinv[max_n];
void init_f() {
	fact[0] = modint(1);
	for (int i = 0; i < max_n - 1; i++) {
		fact[i + 1] = fact[i] * modint(i + 1);
	}
	factinv[max_n - 1] = modint(1) / fact[max_n - 1];
	for (int i = max_n - 2; i >= 0; i--) {
		factinv[i] = factinv[i + 1] * modint(i + 1);
	}
}
modint comb(int a, int b) {
	if (a < 0 || b < 0 || a < b)return 0;
	return fact[a] * factinv[b] * factinv[a - b];
}
modint combP(int a, int b) {
	if (a < 0 || b < 0 || a < b)return 0;
	return fact[a] * factinv[a - b];
}

ll gcd(ll a, ll b) {
	a = abs(a); b = abs(b);
	if (a < b)swap(a, b);
	while (b) {
		ll r = a % b; a = b; b = r;
	}
	return a;
}
typedef long double ld;
typedef pair<ld, ld> LDP;
const ld eps = 1e-8;
const ld pi = acosl(-1.0);
template<typename T>
void addv(vector<T>& v, int loc, T val) {
	if (loc >= v.size())v.resize(loc + 1, 0);
	v[loc] += val;
}
/*const int mn = 100005;
bool isp[mn];
vector<int> ps;
void init() {
	fill(isp + 2, isp + mn, true);
	for (int i = 2; i < mn; i++) {
		if (!isp[i])continue;
		ps.push_back(i);
		for (int j = 2 * i; j < mn; j += i) {
			isp[j] = false;
		}
	}
}*/

//[,val)
template<typename T>
auto prev_itr(set<T>& st, T val) {
	auto res = st.lower_bound(val);
	if (res == st.begin())return st.end();
	res--; return res;
}

//[val,)
template<typename T>
auto next_itr(set<T>& st, T val) {
	auto res = st.lower_bound(val);
	return res;
}
using mP = pair<modint, modint>;


int dx[4] = { 1,0,-1,0 };
int dy[4] = { 0,1,0,-1 };
//-----------------------------------------

void init() {
	ll f[27];
	f[26] = 1;
	f[25] = 1;
	f[24] = 2;
	for (int i = 23; i > 0; i--) {
		f[i] = 0;
		rep1(j, 3)f[i] += f[i + j];
	}
	ll sum = 0;
	rep1(i, 26) {
		ll coef = 0;
		coef += i * (i - 1) * (i - 2) / 6 * 8;
		coef += i * (i - 1) / 2 * 4;
		coef += 2 * i;
		sum += f[i] * coef;
		cout << i << " " << f[i] << "\n";
	}
	cout << sum << "\n";
}
int n;
void solve() {
	vector<string> s(3);
	rep(i, 3)cin >> s[i];

	vector<bool> res;
	vector<bool> chk(26);
	vector<bool> visited(26);
	function<void(vector<vector<P>>)>dfs = [&](vector<vector<P>> cs) {
		sort(all(cs));
		cs.erase(unique(all(cs)), cs.end());
		//cout << "? " << cs.size() << "\n";
		if (cs.empty()) {
			res = chk; return;
		}
		for (int i = 1; i < cs.size(); i++) {
			if (cs[i].size() < cs[0].size())swap(cs[i], cs[0]);
		}
		rep(j, cs[0].size()) {
			rep(k, j) {
				chk[cs[0][k].first] = !cs[0][k].second;
			}
			chk[cs[0][j].first] = cs[0][j].second;
			visited[cs[0][j].first] = true;
			vector<vector<P>> ncs;
			bool valid = true;
			rep(i, cs.size()) {
				bool ald = false;
				vector<P> nex;
				rep(x, cs[i].size()) {
					if (visited[cs[i][x].first]) {
						if (chk[cs[i][x].first] == cs[i][x].second)ald = true;
					}
					else {
						nex.push_back(cs[i][x]);
					}
				}
				if (ald)continue;
				if (nex.empty()) {
					valid = false; break;
				}
				ncs.push_back(nex);
			}
			if (valid) {
				dfs(ncs);
			}
		}
		rep(j, cs[0].size()) {
			visited[cs[0][j].first] = false;
		}
	};

	vector<vector<P>> ori;
	rep(i, n) {
		rep(j, 8) {
			vector<P> cur;
			rep(k, 3) {
				char c = s[k][3 * i + 1];
				if (j & (1 << k))c = s[k][3 * i + 2];
				if ('A' <= c && c <= 'Z') {
					cur.push_back({ c - 'A',0 });
				}
				else {
					cur.push_back({ c - 'a',1 });
				}
			}
			sort(all(cur));
			bool ald = false;
			if (cur[1].first == cur[2].first) {
				if (cur[1].second != cur[2].second) {
					ald = true;
				}
				cur.pop_back();
			}
			if (cur[0].first == cur[1].first) {
				if (cur[0].second != cur[1].second) {
					ald = true;
				}
				cur.erase(cur.begin() + 1);
			}
			if (!ald) {
				ori.push_back(cur);
			}
		}
	}
	//cout << ori.size() << "\n";
	dfs(ori);
	if (res.empty()) {
		cout << -1 << "\n";
	}
	else {
		vector<char> ans;
		rep(i, 26)if (res[i])ans.push_back('A' + i);
		cout << ans.size() << " ";
		coutarray(ans);
	}
}

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout << fixed << setprecision(10);
	//init_f();
	//init();
	//while(true)
	//expr();
	//int t; cin >> t; rep(i, t)
	while(cin>>n,n)
	solve();
	return 0;
}
accuracy: 1
time_ms: 660
memory_kb: 17196
final_rank: 17

code_snippet: #include <bits/stdc++.h>
using namespace std;
class SatSolver
{
private:
    vector<vector<pair<int, bool>>> cl;
    map<pair<int, bool>, vector<int>> w;
    vector<int> reason, level, que, activity;
    int n, qi;

    void enqueue(int v, bool a, int r = -1)
    {
        assigns[v] = a;
        reason[v]  = r;
        level[v]   = (que.empty() ? 1 : level[que.back()]);
        que.emplace_back(v);
    }

    void add(const vector<pair<int, bool>>& clause)
    {
        for (auto l : clause) w[l].emplace_back(cl.size());
        cl.emplace_back(clause);
    }

    void analyze(int confl)
    {
        int i = que.size(), lv = 1;
        unordered_set<int> used;
        vector<pair<int, bool>> learnt;
        for (int cnt = 0; cnt || used.empty(); --cnt) {
            for (auto q : cl[confl]) {
                if (!used.emplace(q.first).second) continue;
                activity[q.first] += 1e5;
                if (level[q.first] == level[que.back()]) {
                    ++cnt;
                } else {
                    learnt.emplace_back(q);
                    lv = max(lv, level[q.first]);
                }
            }
            while (!used.count(que[--i]))
                ;
            confl = reason[que[i]];
        }
        learnt.emplace_back(que[i], !assigns[que[i]]);
        for (; !que.empty() && level[que.back()] > lv; que.pop_back()) level[que.back()] = 0;
        qi = que.size();
        enqueue(learnt.back().first, learnt.back().second, cl.size());
        add(learnt);
    }

    int propagate()
    {
        for (; qi < int(que.size()); ++qi) {
            for (int cr : w[make_pair(que[qi], !assigns[que[qi]])]) {
                int cnt = 0;
                for (auto& lit : cl[cr]) {
                    if (level[lit.first] == 0) {
                        activity[lit.first] += 1e3;
                        swap(lit, cl[cr][0]);
                        ++cnt;
                    } else if (assigns[lit.first] == lit.second) {
                        swap(lit, cl[cr][0]);
                        cnt = -1;
                        break;
                    }
                }
                if (cnt == 0) return cr;
                if (cnt == 1) enqueue(cl[cr][0].first, cl[cr][0].second, cr);
            }
        }
        return -1;
    }

public:
    vector<bool> assigns;

    SatSolver(int n, const vector<vector<pair<int, bool>>>& clauses) : reason(n), level(n), activity(n), n(n), qi(0), assigns(n)
    {
        for (const auto& clause : clauses) add(clause);
    }

    bool solve()
    {
        while (true) {
            int confl = propagate();
            if (confl != -1) {
                if (level[que.back()] == 1u) { return false; }
                for (auto& a : activity) a /= 1.05;
                analyze(confl);
            } else {
                int k = -1;
                for (int i = 0; i < n; ++i) {
                    if (level[i] == 0 && (k == -1 || activity[k] < activity[i])) k = i;
                }
                if (k == -1) return true;
                enqueue(k, assigns[k]);
                ++level[k];
            }
        }
    }
};

int main()
{
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);
    while (true) {
        int M;
        std::cin >> M;
        if (M == 0) { break; }
        std::vector<std::string> css(3);
        for (int i = 0; i < 3; i++) { std::cin >> css[i]; }
        std::vector<std::vector<std::pair<int, bool>>> clauses;
        for (int i = 0; i < M; i++) {
            for (int fmask = 0; fmask < 8; fmask++) {
                std::vector<std::pair<int, bool>> clause;
                for (int j = 0; j < 3; j++) {
                    if ((fmask >> j) & 1) {
                        const char c = css[j][i * 3 + 1];
                        if (std::islower(c)) {
                            clause.emplace_back(c - 'a', true);
                        } else {
                            clause.emplace_back(c - 'A', false);
                        }
                    } else {
                        const char c = css[j][i * 3 + 2];
                        if (std::islower(c)) {
                            clause.emplace_back(c - 'a', true);
                        } else {
                            clause.emplace_back(c - 'A', false);
                        }
                    }
                }
                clauses.push_back(clause);
            }
        }
        SatSolver solver(26, clauses);
        const bool ok = solver.solve();
        if (not ok) {
            std::cout << "-1\n";
            continue;
        }
        std::vector<char> ans;
        for (int i = 0; i < 26; i++) {
            if (solver.assigns[i]) { ans.push_back('A' + i); }
        }
        std::cout << ans.size();
        for (const auto c : ans) { std::cout << " " << c; }
        std::cout << std::endl;
    }
    return 0;
}
accuracy: 1
time_ms: 240
memory_kb: 4008
final_rank: 5

code_snippet: #include<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 99999999999999999
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;



enum Type{
	YES,
	NO,
	UNDEFINED,
};


int M;
int index_table;
int NUM[3][8005];
char INPUT[3][3005],table[3][8005];
Type type[26],ans_type[26];
bool is_OK,IS_UPPER[3][8005];


bool is_upper(char ch){

	return ch <= 'Z';
}

int get_num(char ch){

	if(is_upper(ch)){

		return ch-'A';
	}else{

		return ch-'a';
	}
}

bool check(int index){

	for(int row = 0; row < 3; row++){

		int tmp_ch = get_num(table[row][index]);

		if((IS_UPPER[row][index] == true && type[tmp_ch] == NO) ||
				(IS_UPPER[row][index] == false && type[tmp_ch] == YES)){

			return true;
		}
	}

	int ch[3];
	for(int row = 0; row < 3; row++){

		ch[row] = NUM[row][index];
	}

	//同種で大小が異なる文字がある
	for(int i = 0; i < 3; i++){
		if(ch[i] == ch[(i+1)%3] && table[i][index] != table[(i+1)%3][index]){
			return true;
		}
	}

	return false;
}

bool recursive(int index){

	if(is_OK)return true;

	if(index == index_table){

		for(int i = 0; i < 26; i++){

			ans_type[i] = type[i];
		}
		is_OK = true;
		return true;
	}

	//分岐不要
	if(check(index)){

		return recursive(index+1);
	}

	int undef[3];
	int undef_index = 0;
	for(int row = 0; row < 3; row++){
		if(type[NUM[row][index]] == UNDEFINED){

			undef[undef_index++] = row;
		}
	}

	if(undef_index == 0)return false;

	int ch;

	//このままでは通れない
	for(int i = 0; i < undef_index; i++){

		ch = NUM[undef[i]][index];
		if(type[ch] == UNDEFINED){
			if(IS_UPPER[undef[i]][index]){

				type[ch] = NO;

			}else{

				type[ch] = YES;
			}
			if(recursive(index+1)){

				return true;
			}
			if(IS_UPPER[undef[i]][index]){

				type[ch] = YES;

			}else{

				type[ch] = NO;
			}
		}
	}
	for(int i = 0 ; i < undef_index; i++){

		type[NUM[undef[i]][index]] = UNDEFINED;
	}

	return false;
}

void func(){

	for(int row = 0; row < 3; row++){

		scanf("%s",INPUT[row]);
	}

	is_OK = false;
	for(int i = 0; i < 26; i++){

		type[i] = UNDEFINED;
	}

	index_table = 0;
	for(int i = 0; i < M; i++){

		int base_col = 3*i;

		for(int state = 0; state < 8; state++){
			for(int row = 0; row < 3; row++){
				if(state & (1 << row)){

					table[row][index_table] = INPUT[row][base_col+1];

				}else{

					table[row][index_table] = INPUT[row][base_col+2];
				}
				IS_UPPER[row][index_table] = is_upper(table[row][index_table]);
				NUM[row][index_table] = get_num(table[row][index_table]);
			}
			index_table++;
		}
	}

	recursive(0);

	if(!is_OK){

		printf("-1\n");

	}else{

		vector<int> ANS;

		for(int loop = 0; loop < 26; loop++){
			if(ans_type[loop] == YES){
				ANS.push_back(loop);
			}
		}

		printf("%lld",ANS.size());

		for(int i = 0; i < ANS.size(); i++){

			printf(" %c",'A'+ANS[i]);
		}
		printf("\n");
	}
}

int main(){

	while(true){

		scanf("%d",&M);

		if(M == 0)break;

		func();
	}

	return 0;
}
accuracy: 1
time_ms: 4290
memory_kb: 3304
final_rank: 10

code_snippet: #include<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 99999999999999999
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;



enum Type{
	YES,
	NO,
	UNDEFINED,
};


int M;
int index_table;
char INPUT[3][3005],table[3][8005];
Type type[26],ans_type[26];
bool is_OK;


bool is_upper(char ch){

	return ch <= 'Z';
}

int get_num(char ch){

	if(is_upper(ch)){

		return ch-'A';
	}else{

		return ch-'a';
	}
}

bool check(int index){

	for(int row = 0; row < 3; row++){

		int tmp_ch = get_num(table[row][index]);

		if((is_upper(table[row][index]) == true && type[tmp_ch] == NO) ||
				(is_upper(table[row][index]) == false && type[tmp_ch] == YES)){

			return true;
		}
	}

	int ch[3];
	for(int row = 0; row < 3; row++){

		ch[row] = get_num(table[row][index]);
	}

	//同種で大小が異なる文字がある
	for(int i = 0; i < 3; i++){
		if(ch[i] == ch[(i+1)%3] && table[i][index] != table[(i+1)%3][index]){
			return true;
		}
	}

	return false;
}

bool recursive(int index){

	if(is_OK)return true;

	if(index == index_table){

		for(int i = 0; i < 26; i++){

			ans_type[i] = type[i];
		}
		is_OK = true;
		return true;
	}

	//分岐不要
	if(check(index)){

		return recursive(index+1);
	}

	int undef[3];
	int undef_index = 0;
	for(int row = 0; row < 3; row++){
		if(type[get_num(table[row][index])] == UNDEFINED){

			undef[undef_index++] = row;
		}
	}

	if(undef_index == 0)return false;

	int ch;

	//このままでは通れない
	for(int i = 0; i < undef_index; i++){

		ch = get_num(table[undef[i]][index]);
		if(type[ch] == UNDEFINED){
			if(is_upper(table[undef[i]][index])){

				type[ch] = NO;

			}else{

				type[ch] = YES;
			}
			if(recursive(index+1)){

				return true;
			}
			if(is_upper(table[undef[i]][index])){

				type[ch] = YES;

			}else{

				type[ch] = NO;
			}
		}
	}
	for(int i = 0 ; i < undef_index; i++){

		type[get_num(table[undef[i]][index])] = UNDEFINED;
	}

	return false;
}

void func(){

	for(int row = 0; row < 3; row++){

		scanf("%s",INPUT[row]);
	}

	is_OK = false;
	for(int i = 0; i < 26; i++){

		type[i] = UNDEFINED;
	}

	index_table = 0;
	for(int i = 0; i < M; i++){

		int base_col = 3*i;

		for(int state = 0; state < 8; state++){
			for(int row = 0; row < 3; row++){
				if(state & (1 << row)){

					table[row][index_table] = INPUT[row][base_col+1];

				}else{

					table[row][index_table] = INPUT[row][base_col+2];
				}
			}
			index_table++;
		}
	}

	recursive(0);

	if(!is_OK){

		printf("-1\n");

	}else{

		vector<int> ANS;

		for(int loop = 0; loop < 26; loop++){
			if(ans_type[loop] == YES){
				ANS.push_back(loop);
			}
		}

		printf("%lld",ANS.size());

		for(int i = 0; i < ANS.size(); i++){

			printf(" %c",'A'+ANS[i]);
		}
		printf("\n");
	}
}

int main(){

	while(true){

		scanf("%d",&M);

		if(M == 0)break;

		func();
	}

	return 0;
}
accuracy: 1
time_ms: 4410
memory_kb: 3204
final_rank: 12

code_snippet: #include<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 99999999999999999
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;



enum Type{
	YES,
	NO,
	UNDEFINED,
};


int M;
int index_table;
char INPUT[3][3005],table[3][8005];
Type type[26],ans_type[26];
bool is_OK;


bool is_upper(char ch){

	return ch <= 'Z';
}

int get_num(char ch){

	if(is_upper(ch)){

		return ch-'A';
	}else{

		return ch-'a';
	}
}

bool check(int index){

	for(int row = 0; row < 3; row++){

		int tmp_ch = get_num(table[row][index]);

		if((is_upper(table[row][index]) == true && type[tmp_ch] == NO) ||
				(is_upper(table[row][index]) == false && type[tmp_ch] == YES)){

			return true;
		}
	}

	int ch[3];
	for(int row = 0; row < 3; row++){

		ch[row] = get_num(table[row][index]);
	}

	//同種で大小が異なる文字がある
	for(int i = 0; i < 3; i++){
		if(ch[i] == ch[(i+1)%3] && table[i][index] != table[(i+1)%3][index]){
			return true;
		}
	}

	return false;
}

bool recursive(int index){

	if(is_OK)return true;

	if(index == index_table){

		for(int i = 0; i < 26; i++){

			ans_type[i] = type[i];
		}
		is_OK = true;
		return true;
	}

	//分岐不要
	if(check(index)){

		return recursive(index+1);
	}

	int undef[3];
	int undef_index = 0;
	for(int row = 0; row < 3; row++){
		if(type[get_num(table[row][index])] == UNDEFINED){

			undef[undef_index++] = row;
		}
	}

	if(undef_index == 0)return false;

	int work[3];
	int tmp_index,ch;

	//このままでは通れない
	for(int i = 0; i < undef_index; i++){

		tmp_index = 0;

		for(int k = 0; k < i; k++){
			ch = get_num(table[undef[k]][index]);
			if(type[ch] != UNDEFINED)continue;

			if(is_upper(table[undef[k]][index])){

				type[ch] = YES;

			}else{

				type[ch] = NO;
			}
			work[tmp_index++] = ch;
		}
		ch = get_num(table[undef[i]][index]);
		if(type[ch] == UNDEFINED){
			if(is_upper(table[undef[i]][index])){

				type[ch] = NO;

			}else{

				type[ch] = YES;
			}
			work[tmp_index++] = ch;

			if(recursive(index+1)){

				return true;
			}
		}
		for(int i = 0; i < tmp_index; i++){

			type[work[i]] = UNDEFINED;
		}

	}
	return false;
}

void func(){

	for(int row = 0; row < 3; row++){

		scanf("%s",INPUT[row]);
	}

	is_OK = false;
	for(int i = 0; i < 26; i++){

		type[i] = UNDEFINED;
	}

	index_table = 0;
	for(int i = 0; i < M; i++){

		int base_col = 3*i;

		for(int state = 0; state < 8; state++){
			for(int row = 0; row < 3; row++){
				if(state & (1 << row)){

					table[row][index_table] = INPUT[row][base_col+1];

				}else{

					table[row][index_table] = INPUT[row][base_col+2];
				}
			}
			index_table++;
		}
	}

	recursive(0);

	if(!is_OK){

		printf("-1\n");

	}else{

		vector<int> ANS;

		for(int loop = 0; loop < 26; loop++){
			if(ans_type[loop] == YES){
				ANS.push_back(loop);
			}
		}

		printf("%lld",ANS.size());

		for(int i = 0; i < ANS.size(); i++){

			printf(" %c",'A'+ANS[i]);
		}
		printf("\n");
	}
}

int main(){

	while(true){

		scanf("%d",&M);

		if(M == 0)break;

		func();
	}

	return 0;
}
accuracy: 1
time_ms: 4470
memory_kb: 3060
final_rank: 11

code_snippet: #include<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 99999999999999999
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;



enum Type{
	YES,
	NO,
	UNDEFINED,
};


int M;
int index_table;
char INPUT[3][3005],table[3][8005];
Type type[26],ans_type[26];
bool is_OK;


bool is_upper(char ch){

	return ch <= 'Z';
}

int get_num(char ch){

	if(is_upper(ch)){

		return ch-'A';
	}else{

		return ch-'a';
	}
}

bool check(int index){

	for(int row = 0; row < 3; row++){

		int tmp_ch = get_num(table[row][index]);

		if((is_upper(table[row][index]) == true && type[tmp_ch] == NO) ||
				(is_upper(table[row][index]) == false && type[tmp_ch] == YES)){

			return true;
		}
	}

	int ch[3];
	for(int row = 0; row < 3; row++){

		ch[row] = get_num(table[row][index]);
	}

	//同種で大小が異なる文字がある
	for(int i = 0; i < 3; i++){
		if(ch[i] == ch[(i+1)%3] && table[i][index] != table[(i+1)%3][index]){
			return true;
		}
	}

	return false;
}

bool recursive(int index){

	if(is_OK)return true;

	if(index == index_table){

		for(int i = 0; i < 26; i++){

			ans_type[i] = type[i];
		}
		is_OK = true;
		return true;
	}

	//分岐不要
	if(check(index)){

		return recursive(index+1);
	}

	bool not_FLG = true;

	for(int row = 0; row < 3; row++){
		int ch = get_num(table[row][index]);
		if(type[ch] == UNDEFINED){

			not_FLG = false;
			break;
		}
	}

	if(not_FLG)return false;

	int work[3];
	int tmp_index,ch;

	//このままでは通れない
	for(int row = 0; row < 3; row++){

		tmp_index = 0;

		for(int i = 0; i < row; i++){
			ch = get_num(table[i][index]);
			if(type[ch] != UNDEFINED)continue;

			if(is_upper(table[i][index])){

				type[ch] = YES;

			}else{

				type[ch] = NO;
			}
			work[tmp_index++] = ch;
		}
		ch = get_num(table[row][index]);
		if(type[ch] == UNDEFINED){
			if(is_upper(table[row][index])){

				type[ch] = NO;

			}else{

				type[ch] = YES;
			}
			work[tmp_index++] = ch;

			if(recursive(index+1)){

				return true;
			}
		}

		for(int i = 0; i < tmp_index; i++){

			type[work[i]] = UNDEFINED;
		}

	}
	return false;
}

void func(){

	for(int row = 0; row < 3; row++){

		scanf("%s",INPUT[row]);
	}

	is_OK = false;
	for(int i = 0; i < 26; i++){

		type[i] = UNDEFINED;
	}

	index_table = 0;
	for(int i = 0; i < M; i++){

		int base_col = 3*i;

		for(int state = 0; state < 8; state++){
			for(int row = 0; row < 3; row++){
				if(state & (1 << row)){

					table[row][index_table] = INPUT[row][base_col+1];

				}else{

					table[row][index_table] = INPUT[row][base_col+2];
				}
			}
			index_table++;
		}
	}

	recursive(0);

	if(!is_OK){

		printf("-1\n");

	}else{

		vector<int> ANS;

		for(int loop = 0; loop < 26; loop++){
			if(ans_type[loop] == YES){
				ANS.push_back(loop);
			}
		}

		printf("%lld",ANS.size());

		for(int i = 0; i < ANS.size(); i++){

			printf(" %c",'A'+ANS[i]);
		}
		printf("\n");
	}
}

int main(){

	while(true){

		scanf("%d",&M);

		if(M == 0)break;

		func();
	}

	return 0;
}
accuracy: 1
time_ms: 4100
memory_kb: 3080
final_rank: 9

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

// xor128による乱数生成、周期は2^128-1
unsigned int randInt() {
    static unsigned int tx = 123456789, ty=362436069, tz=521288629, tw=88675123;
    unsigned int tt = (tx^(tx<<11));
    tx = ty; ty = tz; tz = tw;
    return ( tw=(tw^(tw>>19))^(tt^(tt>>8)) );
}

int randInt(int minv, int maxv) {
    return randInt() % (maxv - minv + 1) + minv;
}

// ランダムシャッフル
template<class T> void shuffle(vector<T>& vec) {
    int n = vec.size();
    for (int i = n - 1; i > 0; --i) {
        int k = randInt() % (i + 1);
        swap(vec[i], vec[k]);
    }
}

using CL = vector<pair<int,int> >;

bool rec(vector<int> &res, int k, const vector<CL> &cls) {
    if (k == cls.size()) return true;
    vector<pair<int,int> > rem;
    bool ok = false;
    for (int i = 0; i < cls[k].size(); ++i) {
        if (res[cls[k][i].first] == cls[k][i].second) {
            ok = true;
            break;
        }
        else if (res[cls[k][i].first] == -1) rem.push_back(cls[k][i]);
    }
    if (ok) return rec(res, k+1, cls);
    if (!ok && rem.empty()) return false;
    auto copy_res = res;
    for (int i = 0; i < rem.size(); ++i) {
        for (int j = 0; j < i; ++j) res[rem[j].first] = 1 - rem[j].second;
        res[rem[i].first] = rem[i].second;
        if (rec(res, k+1, cls)) return true;
    }
    res = copy_res;
    return false;
}

vector<int> solveSAT(int N, vector<CL> &cls) {
    for (int i = 0; i < (int)cls.size(); ++i) {
        map<int, set<int> > so;
        for (int j = 0; j < (int)cls[i].size(); ++j) {
            so[cls[i][j].first].insert(cls[i][j].second);
        }
        CL ncl;
        bool already = false;
        for (auto it : so) {
            if (it.second.size() == 2) already = true;
            ncl.push_back({it.first, *(it.second.begin())});
        }
        if (already) cls.erase(cls.begin() + i--);
        else cls[i] = ncl;
    }
    sort(cls.begin(), cls.end());
    for (int i = 0; i+1 < (int)cls.size(); ++i) {
        if (cls[i] == cls[i+1]) cls.erase(cls.begin() + i--);
    }
    vector<int> res(N, -1);
    if (rec(res, 0, cls)) return res;
    else return vector<int>();
}

int main() {
    int M;
    while (cin >> M, M) {
        vector<string> v(3);
        for (int i = 0; i < 3; ++i) cin >> v[i];
        
        vector<CL> cls;
        int iter = 0;
        for (int i = 0; i < M; ++i) {
            vector<int> pos({i*3+1, i*3+2});
            for (int bit = 0; bit < (1<<3); ++bit) {
                CL cl(3);
                for (int j = 0; j < 3; ++j) {
                    char c = (bit & (1<<j)) ? v[j][pos[1]] : v[j][pos[0]]; 
                    if (c >= 'a' && c <= 'z') cl[j] = {c - 'a', 1};
                    else cl[j] = {c - 'A', 0};
                }
                cls.push_back(cl);
            }
        }
        //shuffle(cls);

        //COUT(cls);
        
        auto res = solveSAT(26, cls);
        if (res.empty()) cout << -1 << endl;
        else {
            vector<char> ans;
            for (int i = 0; i < 26; ++i) {
                if (res[i] == 1) ans.push_back('A' + i);
            }
            cout << ans.size() << endl;
            for (auto c : ans) cout << c << endl;
        }
        
    }
}
accuracy: 1
time_ms: 5340
memory_kb: 4204
final_rank: 14

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


#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl
template<class T1, class T2> ostream& operator << (ostream &s, pair<T1,T2> P)
{ return s << '<' << P.first << ", " << P.second << '>'; }
template<class T> ostream& operator << (ostream &s, vector<T> P)
{ for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << " "; } s << P[i]; } return s; }
template<class T> ostream& operator << (ostream &s, vector<vector<T> > P)
{ for (int i = 0; i < P.size(); ++i) { s << endl << P[i]; } return s << endl; }



// xor128による乱数生成、周期は2^128-1
unsigned int randInt() {
    static unsigned int tx = 123456789, ty=362436069, tz=521288629, tw=88675123;
    unsigned int tt = (tx^(tx<<11));
    tx = ty; ty = tz; tz = tw;
    return ( tw=(tw^(tw>>19))^(tt^(tt>>8)) );
}

int randInt(int minv, int maxv) {
    return randInt() % (maxv - minv + 1) + minv;
}

// ランダムシャッフル
template<class T> void shuffle(vector<T>& vec) {
    int n = vec.size();
    for (int i = n - 1; i > 0; --i) {
        int k = randInt() % (i + 1);
        swap(vec[i], vec[k]);
    }
}

using CL = vector<pair<int,int> >;

bool rec(vector<int> &res, int k, const vector<CL> &cls) {
    if (k == cls.size()) return true;
    vector<pair<int,int> > rem;
    bool ok = false;
    for (int i = 0; i < cls[k].size(); ++i) {
        if (res[cls[k][i].first] == cls[k][i].second) {
            ok = true;
            break;
        }
        else if (res[cls[k][i].first] == -1) rem.push_back(cls[k][i]);
    }
    if (ok) return rec(res, k+1, cls);
    if (!ok && rem.empty()) return false;
    auto copy_res = res;
    for (int i = 0; i < rem.size(); ++i) {
        for (int j = 0; j < i; ++j) res[rem[j].first] = 1 - rem[j].second;
        res[rem[i].first] = rem[i].second;
        if (rec(res, k+1, cls)) return true;
    }
    res = copy_res;
    return false;
}

vector<int> solveSAT(int N, vector<CL> &cls) {
    for (int i = 0; i < (int)cls.size(); ++i) {
        map<int, set<int> > so;
        for (int j = 0; j < (int)cls[i].size(); ++j) {
            so[cls[i][j].first].insert(cls[i][j].second);
        }
        CL ncl;
        bool already = false;
        for (auto it : so) {
            if (it.second.size() == 2) already = true;
            ncl.push_back({it.first, *(it.second.begin())});
        }
        if (already) cls.erase(cls.begin() + i--);
        else cls[i] = ncl;
    }
    sort(cls.begin(), cls.end());
    for (int i = 0; i+1 < (int)cls.size(); ++i) {
        if (cls[i] == cls[i+1]) cls.erase(cls.begin() + i--);
    }
    vector<int> res(N, -1);
    if (rec(res, 0, cls)) return res;
    else return vector<int>();
}

int main() {
    int M;
    while (cin >> M, M) {
        vector<string> v(3);
        for (int i = 0; i < 3; ++i) cin >> v[i];
        
        vector<CL> cls;
        int iter = 0;
        for (int i = 0; i < M; ++i) {
            vector<int> pos({i*3+1, i*3+2});
            for (int bit = 0; bit < (1<<3); ++bit) {
                CL cl(3);
                for (int j = 0; j < 3; ++j) {
                    char c = (bit & (1<<j)) ? v[j][pos[1]] : v[j][pos[0]]; 
                    if (c >= 'a' && c <= 'z') cl[j] = {c - 'a', 1};
                    else cl[j] = {c - 'A', 0};
                }
                cls.push_back(cl);
            }
        }
        shuffle(cls);

        //COUT(cls);
        
        auto res = solveSAT(26, cls);
        if (res.empty()) cout << -1 << endl;
        else {
            vector<char> ans;
            for (int i = 0; i < 26; ++i) {
                if (res[i] == 1) ans.push_back('A' + i);
            }
            cout << ans.size() << endl;
            for (auto c : ans) cout << c << endl;
        }
        
    }
}
accuracy: 1
time_ms: 5380
memory_kb: 4152
final_rank: 15

code_snippet: #pragma GCC optimize "O3"

#define ALL(x) x.begin(), x.end()

#include<bits/stdc++.h>

using namespace std;

constexpr int N = 26;

typedef long long LL;

int m;
char tab[3][3010];

vector<LL> cls;

inline bool dfs(const int& lev, LL st) {
  if(st & (st >> N))
    return false;
  if(lev == (int)cls.size()) {
    vector<char> ans;
    for(int i = 0; i < N; ++i)
      if(st & (1LL << i))
        ans.push_back('A' + i);
    printf("%lu", ans.size());
    for(char c : ans)
      printf(" %c", c);
    puts("");
    return true;
  }
  LL cl = cls[lev];
  if(st & cl) return dfs(lev + 1, st);
  while(cl) {
    LL lb = (cl & (-cl));
    cl -= lb;
    if(dfs(lev + 1, st | lb))
      return true;
    lb = (lb < (1LL << N) ? (lb << N) : (lb >> N));
    st |= lb;
  }
  return false;
}
inline void solve() {
  cls.clear();
  for(int i = 0; i < 3; ++i)
    scanf("%s", tab[i]);
  for(int i = 0; i < m; ++i)
    for(int j = 0; j < 8; ++j) {
      LL var = 0;
      for(int k = 0; k < 3; ++k) {
        char c = tab[k][3 * i + 1 + ((j >> k) & 1)];
        var |= (1LL << (islower(c) ? (c - 'a') : (c - 'A' + N)));
      }
      if((var & (var >> N)) == 0)
        cls.push_back(var);
    }
  sort(ALL(cls));
  cls.resize(unique(ALL(cls)) - cls.begin());
  if(!dfs(0, 0))
    puts("-1");
}
int main() {
  while(scanf("%d", &m) && m)
    solve();
  return 0;
}
accuracy: 1
time_ms: 250
memory_kb: 4172
final_rank: 6

code_snippet: #include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;

const int N = 1005 * 3;

int n;

char map[3][N];

pair<int, bool> getVar(char ch) {
	if (ch >= 'A' && ch <= 'Z') {
		return make_pair(ch - 'A', false);
	} else {
		return make_pair(ch - 'a', true);
	}
}

struct SAT3 {
	pair<int, bool> v[3];
	
	SAT3(char a, char b, char c) {
		v[0] = getVar(a), v[1] = getVar(b), v[2] = getVar(c);
		sort(v, v + 3);
	}

	bool operator < (const SAT3 &b) const {
		for (int i = 0; i < 3; ++i) {
			if (v[i] != b.v[i]) {
				return v[i] < b.v[i];
			}
		}
		return false;
	}

	bool operator == (const SAT3 &b) const {
		for (int i = 0; i < 3; ++i) {
			if (v[i] != b.v[i]) {
				return false;
			}
		}
		return true;
	}
};

const int C = 26;

int val[C];

vector<SAT3> cl;

bool go(int d) {
	if (d == cl.size()) {
		vector<char> ans;
		for (int i = 0; i < C; ++i) {
			if (val[i] == 1) {
				ans.push_back('A' + i);
			}
		}
		printf("%d", (int)ans.size());
		for (int i = 0; i < (int)ans.size(); ++i) {
			printf(" %c", ans[i]);
		}
		printf("\n");
		return true;
	}
	SAT3 &cur = cl[d];
	bool good = false;
	int bad = 0;
	for (int i = 0; i < 3 && !good; ++i) {
		int v = cur.v[i].first;
		if (val[v] == -1) {
			continue;
		}
		if (val[v] == cur.v[i].second) {
			good = true;
		} else {
			++bad;
		}
	}
	if (good) {
		return go(d + 1);
	}
	if (bad == 3) {
		return false;
	}
	bool vis[3] = {0, 0, 0};
	for (int i = 0; i < 3; ++i) {
		int v = cur.v[i].first, c = cur.v[i].second;
		if (val[v] != -1) {
			continue;
		}
		vis[i] = true;
		val[v] = c;
		if (go(d + 1)) {
			return true;
		}
		val[v] = !c;
	}
	for (int i = 0; i < 3; ++i) {
		if (vis[i]) {
			val[cur.v[i].first] = -1;
		}
	}
	return false;
}

int main() {
	while (scanf("%d", &n) == 1 && n) {
		for (int i = 0; i < 3; ++i) {
			scanf("%s", map[i]);
		}
		cl.clear();
		for (int i = 0; i < n; ++i) {
			int d = i * 3 + 1;
			for (int a = 0; a < 2; ++a) {
				for (int b = 0; b < 2; ++b) {
					for (int c = 0; c < 2; ++c) {
						cl.push_back(SAT3(map[0][d + a], map[1][d + b], map[2][d + c]));
					}
				}
			}
		}
		sort(cl.begin(), cl.end());
		cl.erase(unique(cl.begin(), cl.end()), cl.end());
		vector<SAT3> clt;
		for (int i = 0; i < (int)cl.size(); ++i) {
			bool flag = false;
			for (int j = 0; j < 2; ++j) {
				if (cl[i].v[j].first == cl[i].v[j + 1].first && cl[i].v[j].second != cl[i].v[j + 1].second) {
					flag = true;
					break;
				}
			}
			if (!flag) {
				clt.push_back(cl[i]);
			}
		}
		cl = clt;
		memset(val, -1, sizeof(val));
		if (!go(0)) {
			printf("-1\n");
		}
	}
	return 0;
}
accuracy: 1
time_ms: 120
memory_kb: 2276
final_rank: 1

code_snippet: /*
(a&b)|(c&d)|(e&f)
(a|c|e)&(a|c|f)&(a|d|e)&(a|d|f)&(b|c|e)&(b|c|f)&(b|d|e)&(b|d|f)
*/
#include<stdio.h>
#include<algorithm>
using namespace std;
char str[3][10000];
char fi[10000];
char se[10000];
char la[10000];
int sz=0;
int t[26];
int solve(int a){
	if(a==sz)return 1;
	if((fi[a]&32)&&(t[fi[a]-'a']==1))return solve(a+1);
	if(!(fi[a]&32)&&(t[fi[a]-'A']==0))return solve(a+1);
	if((se[a]&32)&&(t[se[a]-'a']==1))return solve(a+1);
	if(!(se[a]&32)&&(t[se[a]-'A']==0))return solve(a+1);
	if((la[a]&32)&&(t[la[a]-'a']==1))return solve(a+1);
	if(!(la[a]&32)&&(t[la[a]-'A']==0))return solve(a+1);
	if((fi[a]^32)==se[a])return solve(a+1);
	if((la[a]^32)==se[a])return solve(a+1);
	if((fi[a]^32)==la[a])return solve(a+1);
	bool ff=false;
	bool sf=false;
	bool lf=false;
	if(!~t[(fi[a]|32)-'a']){
		if(fi[a]>='a')t[fi[a]-'a']=1;
		else t[fi[a]-'A']=0;
		if(solve(a+1))return 1;
		if(fi[a]>='a')t[fi[a]-'a']=0;
		else t[fi[a]-'A']=1;
		ff=true;
	}
	if(!~t[(se[a]|32)-'a']){
		if(se[a]>='a')t[se[a]-'a']=1;
		else t[se[a]-'A']=0;
		if(solve(a+1))return 1;
		if(se[a]>='a')t[se[a]-'a']=0;
		else t[se[a]-'A']=1;
		sf=true;
	}
	if(!~t[(la[a]|32)-'a']){
		if(la[a]>='a')t[la[a]-'a']=1;
		else t[la[a]-'A']=0;
		if(solve(a+1))return 1;
		if(la[a]>='a')t[la[a]-'a']=0;
		else t[la[a]-'A']=1;
		lf=true;
	}
	if(ff)t[(fi[a]|32)-'a']=-1;
	if(sf)t[(se[a]|32)-'a']=-1;
	if(lf)t[(la[a]|32)-'a']=-1;
	
	return 0;
}
int main(){
	int a;
	while(scanf("%d",&a),a){
		for(int i=0;i<3;i++)scanf("%s",str[i]);
		sz=a*8;
		for(int i=0;i<a;i++){
			for(int j=0;j<8;j++){
				if(j&1){
					fi[i*8+j]=str[0][i*3+1];
				}else fi[i*8+j]=str[0][i*3+2];
				if(j&2){
					se[i*8+j]=str[1][i*3+1];
				}else se[i*8+j]=str[1][i*3+2];
				if(j&4){
					la[i*8+j]=str[2][i*3+1];
				}else la[i*8+j]=str[2][i*3+2];
			}
		}
		for(int i=0;i<26;i++)t[i]=-1;
		int res=solve(0);
		if(res==0){
			printf("-1\n");
		}else{
			int ans=0;
			for(int i=0;i<26;i++)if(t[i]==1)ans++;
			printf("%d",ans);
			for(int i=0;i<26;i++)if(t[i]==1)printf(" %c",'A'+i);
			printf("\n");
		}
	}
}
accuracy: 1
time_ms: 2380
memory_kb: 1096
final_rank: 7

code_snippet: #include <cstdio>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <climits>
#include <ctime>
#include <queue>
#include <stack>
#include <algorithm>
#include <list>
#include <vector>
#include <set>
#include <map>
#include <iostream>
#include <deque>
#include <complex>
#include <string>
#include <iomanip>
#include <sstream>
#include <bitset>
#include <valarray>
#include <unordered_map>
#include <iterator>
#include <functional>
#include <cassert>
#include <ctime>
using namespace std;
typedef long long int ll;
typedef unsigned int uint;
typedef unsigned char uchar;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;

#define REP(i,x) for(int i=0;i<(int)(x);i++)
#define REPS(i,x) for(int i=1;i<=(int)(x);i++)
#define RREP(i,x) for(int i=((int)(x)-1);i>=0;i--)
#define RREPS(i,x) for(int i=((int)(x));i>0;i--)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();i++)
#define RFOR(i,c) for(__typeof((c).rbegin())i=(c).rbegin();i!=(c).rend();i++)
#define ALL(container) (container).begin(), (container).end()
#define RALL(container) (container).rbegin(), (container).rend()
#define SZ(container) ((int)container.size())
#define mp(a,b) make_pair(a, b)
#define pb push_back
#define eb emplace_back
#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() );

template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }
template<class T> ostream& operator<<(ostream &os, const vector<T> &t) {
os<<"["; FOR(it,t) {if(it!=t.begin()) os<<","; os<<*it;} os<<"]"; return os;
}
template<class T> ostream& operator<<(ostream &os, const set<T> &t) {
os<<"{"; FOR(it,t) {if(it!=t.begin()) os<<","; os<<*it;} os<<"}"; return os;
}
template<class S, class T> ostream& operator<<(ostream &os, const pair<S,T> &t) { return os<<"("<<t.first<<","<<t.second<<")";}
template<class S, class T> pair<S,T> operator+(const pair<S,T> &s, const pair<S,T> &t){ return pair<S,T>(s.first+t.first, s.second+t.second);}
template<class S, class T> pair<S,T> operator-(const pair<S,T> &s, const pair<S,T> &t){ return pair<S,T>(s.first-t.first, s.second-t.second);}

const int INF = 1<<28;
const double EPS = 1e-8;
const int MOD = 1000000007;

int T, n, m;

int solve(ll b, int i, const vector<ll> &sat){
	if(b & (b >> 26)) return 0;
//	cout << i << ", " << bitset<26>(b) << " " << bitset<26>(b>>26) << endl;
	if(i == sat.size()){
		vi ans;
		REP(i, 26) if((b >> i) & (1ll<<26)) ans.pb(i);
		cout << ans.size();
		for(int v : ans) cout << " " << (char)('A'+v);
		cout << endl;
		return 1;
	}
	if(b & sat[i]) return solve(b, i+1, sat);
	ll t = sat[i];
	while(t){
		int p = __builtin_ctzll(t);
		t -= 1ll<<p;
		if(solve(b | (1ll<<p), i+1, sat)) return 1;
		p = (p < 26) ? (p + 26) : (p - 26);
		b |= 1ll << p;
	}
	return 0;
}

int main(int argc, char *argv[]){
	ios::sync_with_stdio(false);
	vector<vector<ll>> sat;
	clock_t endc = clock() + CLOCKS_PER_SEC*60;
	while(cin >> n, n){
		vector<string> s(3);
		REP(i, 3) cin >> s[i];
		vector<ll> sat;
		REP(i, n){
			REP(j, 8){
				ll x = 0;
				REP(k, 3){
					char c = s[k][3*i+1+(1&(j>>k))];
					x |= 1ll << (tolower(c)-'a' + (isupper(c) ? 0 : 26));
				}
				if((x & (x >> 26)) == 0) sat.pb(x);
			}
		}
		sort(ALL(sat)); UNIQUE(sat);
//		cout << sat << endl;
//		for(ll b : sat) cout << bitset<26>(b) << " " << bitset<26>(b>>26) << endl;
		if(!solve(0, 0, sat)) cout << -1 << endl;
	}
	return 0;
}
accuracy: 1
time_ms: 260
memory_kb: 2028
final_rank: 2

code_snippet: #include<cstdio>
#include<utility>
#include<vector>
#include<algorithm>

using namespace std;

typedef pair<int,bool> P;
typedef vector<P> Closure;

bool isupper(char ch){
	return 'A'<=ch&&ch<='Z';
}
int getNum(char ch){
	if(isupper(ch)) return ch-'A';
	else return ch-'a';
}

Closure cs[8080];
int cnt;

bool cmp(const Closure &c1,const Closure &c2){
	if(c1.size()!=c2.size()) return c1.size()<c2.size();
	return c1<c2;
}

char ch[3][3030];
int M;

int which[26];//1:true,0:false,-1:unknown
bool suc;

void input(){
	scanf("%d",&M);
	if(M==0) exit(0);
	for(int i=0;i<3;i++) scanf("%s",ch[i]);
}

void init(){
	for(int i=0;i<26;i++){
		which[i]=-1;
	}
	suc=false;
	cnt=0;
}

void get(){
	for(int i=0;i<3*M;i+=3){
		for(int stat=0;stat<8;stat++){
			Closure c;
			for(int j=0;j<3;j++){
				int a=(stat>>j)&1;
				char ch=::ch[j][i+1+a];
				P p=P(getNum(ch),!isupper(ch));
				c.push_back(p);
			}
			sort(c.begin(),c.end());
			c.erase(unique(c.begin(),c.end()),c.end());
			bool flg=false;
			for(int i=0;i+1<c.size();i++){
				if(c[i].first==c[i+1].first){
					flg=true;
				}
			}
			if(flg) continue;
	//		if(c[0].first==c[1].first||c[1].first==c[2].first){
	//			continue;
	//		}else{
			cs[cnt++]=c;
	//		}
		}
	}
	sort(cs,cs+cnt,cmp);
	cnt=unique(cs,cs+cnt)-cs;
}

void dfs(int id){
	if(cnt==id){
		suc=true;
		return;
	}
	Closure c=cs[id];
	bool changed[3]={0,0,0};
	for(int i=0;i<c.size();i++){
		int x=c[i].first;
		bool t=c[i].second;
		int num=t?1:0;
		if(which[x]!=-1&&which[x]!=t){
			continue;
		}
		if(which[x]==t){
			dfs(id+1);
			if(suc) return;
			else{
				for(int j=0;j<3;j++){
					if(changed[j]){
						which[c[j].first]=-1;
					}
				}
				return;
			}
		}
		changed[i]=true;
		which[x]=t;
		dfs(id+1);
		if(suc) return;
		which[x]=t^1;
	}
	for(int i=0;i<c.size();i++){
		if(changed[i]){
			which[c[i].first]=-1;
		}
	}
}

void output(){
	if(suc==false){
		printf("-1\n");
	}else{
		int num=0;
		for(int i=0;i<26;i++){
			if(which[i]==1) num++;
		}
		printf("%d",num);
		for(int i=0;i<26;i++){
			if(which[i]==1) printf(" %c",'A'+i);
		}
		printf("\n");
	}
}

void solve(){
	dfs(0);
	output();
}

int main(){
	while(true){
		input();
		init();
		get();
		solve();
	/*	for(int i=0;i<cnt;i++){
			printf("(");
			for(int j=0;j<cs[i].size();j++){
				printf("%c %c",'a'+cs[i][j].first,cs[i][j].second?'t':'f');
				printf(" ");
			}
			printf(")");
			printf("\n");
		}
		printf("\n");*/
	}
	return 0;
}
accuracy: 1
time_ms: 360
memory_kb: 2184
final_rank: 3

code_snippet: #include <cstdio>

#define rep(i,n) for(int i=0;i<(n);i++)

using namespace std;

// なぜか cctype のものを使うと 3 倍くらい遅くなる...
#define isupper(c) (c<='Z')
#define islower(c) (c> 'Z')
#define toupper(c) (c<='Z'?c:c-'a'+'A')

int sw[128];
enum{ON,OFF,UNKNOWN};

int m;
char clause[8000][3]; // clause[i][j] := ( i 個目の clause の j 個目の literal ( lowercase なら押す, uppercase なら押さない ) )

bool is_tautology(int i){
	rep(j,3){
		char c=clause[i][j];
		if(sw[toupper(c)]==ON  && islower(c)) return true;
		if(sw[toupper(c)]==OFF && isupper(c)) return true;
	}

	char c0=clause[i][0];
	char c1=clause[i][1];
	char c2=clause[i][2];
	if(toupper(c0)==toupper(c1) && c0!=c1) return true;
	if(toupper(c1)==toupper(c2) && c1!=c2) return true;
	if(toupper(c2)==toupper(c0) && c2!=c0) return true;

	return false;
}

bool dfs(int i){
	if(i==m) return true;

	if(is_tautology(i)) return dfs(i+1);

	rep(j,3){
		int n=0;
		char a[3],c;
		bool ok=true;
		rep(k,j){
			c=clause[i][k];
			if(sw[toupper(c)]==UNKNOWN){
				sw[toupper(c)]=(isupper(c)?ON:OFF);
				a[n++]=toupper(c);
			}
		}
		c=clause[i][j];
		if(sw[toupper(c)]==UNKNOWN){
			sw[toupper(c)]=(isupper(c)?OFF:ON);
			a[n++]=toupper(c);

			if(dfs(i+1)) return true;
		}

		rep(k,n) sw[a[k]]=UNKNOWN;
	}
	return false;
}

int main(){
	for(int n;scanf("%d",&n),n;){
		char B[3][3010];
		rep(i,3) scanf("%s",B[i]);

		// set clauses
		m=0;
		rep(j,n) rep(S,8) {
			rep(i,3){
				int x=3*j+((S&1<<i)?1:2);
				clause[m][i]=B[i][x];
			}
			m++;
		}

		rep(c,128) sw[c]=UNKNOWN;
		bool res=dfs(0);
		if(!res) puts("-1");
		else{
			int m=0;
			rep(c,128) if(sw[c]==ON) m++;
			printf("%d",m);
			rep(c,128) if(sw[c]==ON) printf(" %c",c);
			puts("");
		}
	}

	return 0;
}
accuracy: 1
time_ms: 4940
memory_kb: 1076
final_rank: 8

code_snippet: #include <bits/stdc++.h>
#define REP(i,n) for(int i=0; i<(int)(n); ++i)

using namespace std;

struct clasure{
    int x[3];
    int y[3];
    bool operator < (const clasure& c) const {
        REP(i, 3) if(x[i] != c.x[i]) return x[i] < c.x[i];
        REP(i, 3) if(y[i] != c.y[i]) return y[i] < c.y[i];
        return false;
    }
};

int to_id(char c){
    if(isupper(c)) return c - 'A';
    else return c - 'a' + 26;
}

struct SAT3{
    vector<clasure> cls;
    static const int N = 26;
    int state[26];
    SAT3(vector<clasure> cls_) : cls(cls_)
        {
            memset(state, -1, sizeof(state));
        }
    vector<int> solve(){
        if(!dfs(0)) return vector<int>(100, -1);
        vector<int> res;
        REP(i, N) if(state[i] == 1) {
            res.push_back(i);
        }
        return res;
    }
    bool dfs(int k){
        clasure& c = cls[k];
        if(k == cls.size()) return true;
        int memo[26];
        memcpy(memo, state, sizeof(state));
        for(int i = 0; i < 3; i++){
            bool ok = true;
            for(int j = 0; j <= i; j++){
                int ny = c.y[j] ^ (j != i);
                if(state[c.x[j]] != -1 && state[c.x[j]] != ny) ok = false;
                state[c.x[j]] = ny;
            }
            if(ok) {
                if(dfs(k + 1)) return true;
            }
            memcpy(state, memo, sizeof(state));
        }
        return false;
    }
};

int main(){
    int N;
    while(cin >> N && N){
        string s[3];
        REP(i, 3) cin >> s[i];
        set<clasure> cls;
        REP(i, N){
            REP(S, 1 << 3){
                clasure c;
                REP(j, 3){
                    c.x[j] = to_id(s[j][3 * i + 1 + (S >> j & 1)]) % 26;
                    c.y[j] = to_id(s[j][3 * i + 1 + (S >> j & 1)]) / 26;
                }
                REP(j, 3) REP(i, j) if(make_pair(c.x[i], c.y[i]) > make_pair(c.x[j], c.y[j])){
                    swap(c.x[i], c.x[j]);
                    swap(c.y[i], c.y[j]);
                }
                cls.insert(c);
            }
        }

        SAT3 solver(vector<clasure>(cls.begin(), cls.end()));
        vector<int> res = solver.solve();
        if(res.size() > 30){
            cout << -1 << endl;
        }else{
            cout << res.size();
            REP(i, res.size()) cout << " " << (char)(res[i] + 'A');
            cout << endl;
        }
    }
    return 0;
}
accuracy: 1
time_ms: 170
memory_kb: 2780
final_rank: 4