Problem:
パズル
1 〜 9 の数字を 14 個組み合わせて完成させるパズルがあります。与えられた 13 個の数字にもうひとつ数字を付け加えて完成させます。
パズルの完成条件は
同じ数字を２つ組み合わせたものが必ずひとつ必要です。
残りの12 個の数字は、３個の数字の組み合わせ４つです。
３個の数字の組み合わせ方は、同じ数字を３つ組み合わせたものか、または３つの連続する数字を組み合わせたものです。ただし、9 1 2 のような並びは連続する数字とは認められません。
同じ数字は4 回まで使えます。
13 個の数字からなる文字列を読み込んで、パズルを完成することができる数字を昇順に全て出力するプログラムを作成してください。なお、1〜9 のどの数字を付け加えてもパズルを完成させることができないときは 0 を出力してください。
例えば与えられた文字列が 3456666777999 の場合
「2」があれば、
234 567 666 77 999
「3」があれば、
33 456 666 777 999
「5」があれば、
345 567 666 77 999
「8」があれば、
345 666 678 77 999
というふうに、2 3 5 8 のいずれかの数字が付け加えられるとパズルは完成します。「6」でも整いますが、5 回目の使用になるので、この例では使えないことに注意してください。
Input
入力は複数のデータセットからなります。各データセットとして、13 個の数字が１行に与えられます。データセットの数は 50 を超えません。
Output
データセットごとに、パズルを完成させることができる数字を昇順に空白区切りで１行に出力します。
Sample Input
3649596966777
6358665788577
9118992346175
9643871425498
7755542764533
1133557799246
Output for the Sample Input
2 3 5 8
3 4
1 2 3 4 5 6 7 8 9
7 8 9
1 2 3 4 6 7 8
0

Submitted solutions:

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define FOR(i, a, b) for (int i = (a), i##_len = (b); i < i##_len; i++)
#define ALL(x) (x).begin(), (x).end()  // sortなどの引数を省略したい
#ifdef _DEBUG
#define PRE_COMMAND             \
    std::cin.rdbuf(in.rdbuf()); \
    std::cout << fixed << setprecision(15);
#else
#define PRE_COMMAND cout << fixed << setprecision(15);
#endif
const double EPS = 1e-10, PI = acos(-1.0);
template <class T> auto MAX(T& seq) {
    return *max_element(seq.begin(), seq.end());
}
template <class T> auto MIN(T& seq) {
    return *min_element(seq.begin(), seq.end());
}
template <class T> auto SUM(T& seq) {
    T temp{0};
    auto& temp2 = temp[0];
    return accumulate(seq.begin(), seq.end(), temp2);
}
template <class T> void SORT(T& seq) { sort(seq.begin(), seq.end()); }
template <class T, class S> void SORT(T& seq, S& sort_order) {
    sort(seq.begin(), seq.end(), sort_order);
}
template <class T> void SORTR(vector<T>& seq) {
    sort(seq.begin(), seq.end(), greater<T>());
}
template <class T, class S, class R> long long pow(T n_0, S k_0, R mod_0) {
    long long n = n_0 % mod_0, k = k_0, mod = mod_0, now = 1;
    while (k) {
        if (k & 1) now = (now * n) % mod;
        n = (n * n) % mod;
        k >>= 1;
    }
    return now;
}
template <typename T> istream& operator>>(istream& is, vector<T>& vec) {
    for (T& x : vec) is >> x;
    return is;
}
template <typename T> ostream& operator<<(ostream& os, const vector<T>& v) {
    if (!v.size()) return os;
    typename vector<T>::const_iterator ii = v.begin();
    os << *ii++;
    for (; ii != v.end(); ++ii) os << " " << *ii;
    return os;
}

// カンマ区切りのデータをvector<int>に変換
vector<int> comma_sep(string inp) {
    inp += ',';
    vector<int> v;
    string s = "";
    for (char c : inp) {
        if (c == ',') {
            v.push_back(stoi(s));
            s = "";
        } else {
            s += c;
        }
    }
    return v;
}

template <class S> map<S, int> Counter(vector<S>& v) {
    map<S, int> ret;
    for (auto i : v) { ret[i]++; }
    return ret;
}

int main() {
    PRE_COMMAND
    set<vector<int>> ok_list;
    vector<vector<int>> three_pair = {
        {1, 2, 3}, {2, 3, 4}, {3, 4, 5}, {4, 5, 6}, {5, 6, 7}, {6, 7, 8},
        {7, 8, 9}, {1, 1, 1}, {2, 2, 2}, {3, 3, 3}, {4, 4, 4}, {5, 5, 5},
        {6, 6, 6}, {7, 7, 7}, {8, 8, 8}, {9, 9, 9}};
    vector<vector<int>> two_pair = {{1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5},
                                    {6, 6}, {7, 7}, {8, 8}, {9, 9}};
    vector<int> deck;
    for (auto t1 : three_pair) {
        for (auto t2 : three_pair) {
            for (auto t3 : three_pair) {
                for (auto t4 : three_pair) {
                    for (auto t5 : two_pair) {
                        deck = {};
                        for (int i : t1) deck.push_back(i);
                        for (int i : t2) deck.push_back(i);
                        for (int i : t3) deck.push_back(i);
                        for (int i : t4) deck.push_back(i);
                        for (int i : t5) deck.push_back(i);
                        map<int, int> c = Counter(deck);
                        bool flag = false;
                        for (auto temp : c) {
                            if (get<1>(temp) > 4) flag = true;
                        }
                        if (flag) continue;
                        SORT(deck);
                        ok_list.insert(deck);
                    }
                }
            }
        }
    }
    string s;
    while (cin >> s) {
        vector<int> now, ans;
        for (int i = 0; i < 13; i++) now.push_back((int)(s[i] - '0'));
        for (int i = 1; i < 10; i++) {
            vector<int> now2 = now;
            now2.push_back(i);
            SORT(now2);
            if (ok_list.count(now2)) ans.push_back(i);
        }
        if (ans.size()) {
            cout << ans << endl;
        } else {
            cout << 0 << endl;
        }
    }
}
accuracy: 1
time_ms: 230
memory_kb: 4816
final_rank: 14

code_snippet: #include <iostream>
#include <algorithm>
#include <iomanip>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <bitset>
#include <cmath>

static const int MOD = 1000000007;
using ll = long long;
using u32 = uint32_t;
using namespace std;

template<class T> constexpr T INF = ::numeric_limits<T>::max()/32*15+208;

bool solve(string s){
    map<int, int> m;
    for (auto &&i : s) {
        m[i]++;
    }
    sort(s.begin(),s.end());
    vector<string> v, u;
    for (int i = 1; i <= 9; ++i) {
        v.emplace_back(3, (char)(i+'0'));
        u.emplace_back(2, (char)(i+'0'));
    }
    for (int i = 1; i < 8; ++i) {
        string t;
        for (int j = 0; j < 3; ++j) {
            t += (char)(i+j+'0');
        }
        v.emplace_back(t);
    }
    int n = v.size();

    for (int i = 0; i < n; ++i) {
        for (int j = i; j < n; ++j) {
            for (int k = j; k < n; ++k) {
                for (int l = k; l < n; ++l) {
                    for (int o = 0; o < 9; ++o) {
                        string t = v[i]+v[j]+v[k]+v[l]+u[o];
                        sort(t.begin(),t.end());
                        if(s == t) return true;
                    }
                }
            }
        }
    }
    return false;
}

int main() {
    string s;
    while(getline(cin, s)){
        sort(s.begin(),s.end());
        map<int, int> cnt;
        for (auto &&i : s) {
            cnt[i-'0']++;
        }
        int b = 0;
        for (int i = 1; i <= 9; ++i) {
            if(cnt[i] < 4){
                string t = s;
                t += char(i + '0');
                if(solve(t)) {
                    if(b++) cout << " ";
                    cout << i;
                }
            }
        }
        if(!b) printf("0");
        puts("");
    }
    return 0;
}
accuracy: 1
time_ms: 630
memory_kb: 3164
final_rank: 18

code_snippet: #include<iostream>
#include<string>
#include<vector>
#include<algorithm>
bool serch(std::vector<int>&nums)
{
	const int length = nums.size();
	static int found_hand = 0;
	switch (found_hand)
	{
	case 0:
		int pair[2];
		for (int i = 0; i < length - 1; i++)
		{
			for (int j = i + 1; j < length; j++)
			{
				if (nums[i] == nums[j])
				{
					pair[0] = nums[i];
					pair[1] = nums[j];
					nums.erase(nums.begin() + j);
					nums.erase(nums.begin() + i);
					found_hand++;
					if (serch(nums))
					{
						return true;
					}
					else
					{
						nums.push_back(pair[0]);
						nums.push_back(pair[1]);
						std::sort(nums.begin(), nums.end());
						found_hand--;
					}
				}
			}
		}
		break;
	default:
		int trio[3];
		for (int i = 0; i < length - 2; i++)
		{
			for (int j = i + 1; j < length - 1; j++)
			{
				for (int k = j + 1; k < length; k++)
				{
					if (nums[i] == nums[j] && nums[j] == nums[k] || nums[j] == nums[i] + 1 && nums[k] == nums[j] + 1)
					{
						trio[0] = nums[i];
						trio[1] = nums[j];
						trio[2] = nums[k];
						nums.erase(nums.begin() + k);
						nums.erase(nums.begin() + j);
						nums.erase(nums.begin() + i);
						found_hand++;
						if (found_hand == 5 || serch(nums))
						{
							found_hand = 0;
							return true;
						}
						else
						{
							nums.push_back(trio[0]);
							nums.push_back(trio[1]);
							nums.push_back(trio[2]);
							std::sort(nums.begin(), nums.end());
							found_hand--;
						}
					}
				}
			}
		}
		break;
	}
	return false;
}
int main()
{
	for (std::string str; std::cin >> str;)
	{
		std::vector<int>ans;
		ans.reserve(9);
		std::vector<int>nums;
		nums.reserve(14);
		std::vector<int>appearance_count(9, 0);
		for (auto i : str)
		{
			const int foo = static_cast<int>(i - '0');
			nums.push_back(foo);
			appearance_count[foo]++;
		}
		for (int i = 1; i <= 9; i++)
		{
			if (appearance_count[i] == 4)
			{
				continue;
			}
			auto nums_copy = nums;
			nums_copy.push_back(i);
			std::sort(nums_copy.begin(), nums_copy.end());
			if (serch(nums_copy))
			{
				ans.push_back(i);
			}
		}
		if (ans.size() == 0)
		{
			std::cout << '0';
		}
		else
		{
			for (int i = 0; i < ans.size(); i++)
			{
				if (i != 0)
				{
					std::cout << ' ';
				}
				std::cout << ans[i];
			}
		}
		std::cout << std::endl;
	}
	return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 3180
final_rank: 12

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

int d[14];
int memo[10][4][2][1 << 14];
bool solve(int p, int cnt, bool r,int u) {
	if (memo[p][cnt][r][u] != -1)return memo[p][cnt][r][u];
	bool ok = false;
	if (cnt == 3) {
		int f = -1, s = -1;
		rep(i, 14) {
			if (!(u>>i&1)) {
				if (f == -1)f = d[i];
				else if (s == -1)s = d[i];
				else ok = true;
			}
		}
		if (!ok) {
			if (f == s)return memo[p][cnt][r][u]=true;
			return memo[p][cnt][r][u]=false;
		}
		ok = true; cnt = 0;
	}
	rep(i, 14) {
		if (!(u>>i&1) && (ok || (r&&d[i] == p + 1) || (!r&&d[i] == p))) {
			u |= 1 << i;
			if (solve(d[i], cnt + 1, r,u))return memo[p][cnt][r][u]=true;
			if (ok) {
				if (solve(d[i], cnt + 1, !r,u))return memo[p][cnt][r][u]=true;
			}
			u ^= 1 << i;
		}
	}
	return memo[p][cnt][r][u]=false;
}
int main() {
	while (1) {
		int cnt[10]{};
		rep(i, 13) {
			if (!~scanf("%1d", &d[i]))return 0;
			cnt[d[i]]++;
		}
		bool flag = false;
		for (int i = 1; i <= 9; i++) {
			if (cnt[i] >= 4)continue;
			d[13] = i;
			memset(memo, -1, sizeof(memo));
			if (solve(0, 3, 0, 0)) {
				if (flag)printf(" ");
				printf("%d", i);
				flag = true;
			}
		}
		if (!flag)printf("0");
		printf("\n");
	}
}
accuracy: 1
time_ms: 40
memory_kb: 8312
final_rank: 17

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

int d[14];
bool used[14];
bool solve(int p, int cnt, bool r) {
	bool ok = false;
	if (cnt == 3) {
		int f = -1, s = -1;
		rep(i, 14) {
			if (!used[i]) {
				if (f == -1)f = d[i];
				else if (s == -1)s = d[i];
				else ok = true;
			}
		}
		if (!ok) {
			if (f == s)return true;
			return false;
		}
		ok = true; cnt = 0;
	}
	rep(i, 14) {
		if (!used[i] && (ok || (r&&d[i] == p + 1) || (!r&&d[i] == p))) {
			used[i] = true;
			if (solve(d[i], cnt + 1, r))return true;
			if (ok) {
				if (solve(d[i], cnt + 1, !r))return true;
			}
			used[i] = false;
		}
	}
	return false;
}
int main(){
	while (1) {
		int cnt[10]{};
		rep(i, 13) {
			if (!~scanf("%1d", &d[i]))return 0;
			cnt[d[i]]++;
		}
		bool flag = false;
		for (int i = 1; i <= 9; i++) {
			if (cnt[i] >= 4)continue;
			d[13] = i;
			memset(used, 0, sizeof(used));
			if (solve(0, 3, 0)) {
				if (flag)printf(" ");
				printf("%d", i);
				flag = true;
			}
		}
		if (!flag)printf("0");
		printf("\n");
	}
}
accuracy: 1
time_ms: 520
memory_kb: 3072
final_rank: 16

code_snippet: #define	_USE_MATH_DEFINES
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <functional>
#include <vector>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cfloat>
#include <map>
#include <queue>
#include <stack>
#include <list>
#include <string>
#include <set>
#include <complex>
#include <utility>
#include <numeric>
#define  rep(i,n) for(int i=0;i<(n);i++)
#define  REP(i,a,n) for(int i=a;i<(n);i++)
#define  rrep(i,n) for(int i=(n)-1;i>=0;i--)
#define  VI	vector<int>
#define	 VS vector<string>
#define  all(a) (a).begin(),(a).end()
#define  debug(x) cout<<#x<<": "<<x<<endl
using namespace std;
typedef long long ll;

const int INF=1e9;
char fi[101][101];
int day[12]={31,28,31,30,31,30,31,31,30,31,30,31};
double EPS = 1e-10;
int N,M;
int sx,sy;
int gx,gy;
int w,h;
int ans;
int dx[4]={0,0,-1,1};
int dy[4]={-1,1,0,0};
const int MAX_V=100;
const int MAX_N=100;
char o[3]={'+','-','*'};
#define md 1000003


int dp[353][353]={0};
int bow[353][353]={0};
double add(double a,double b){
	if(abs(a+b)<EPS*(abs(a)+abs(b)))
	return 0;
	return a+b;
}

struct P{
	double x,y;
	P(){}
		P(double x,double y):x(x),y(y){
		}
		P operator + (P p){
			return P(add(x,p.x),add(y,p.y));
		}
		P operator - (P p){
			return P(add(x,-p.x),add(y,-p.y));
		}
		P operator *(double d){
			return P(x*d,y*d);
		}
		double dot(P p){
			return add(x*p.x,y*p.y);
		}
		double det(P p){
			return add(x*p.y,-y*p.x);
		}
};

bool cmp_x(const P& p,const P& q){
	if(p.x!=q.x) return p.x<q.x;
	return p.y<q.y;
}

vector<P> convex_hull(P* ps, int n){
	sort(ps,ps+n,cmp_x);
	int k=0;
	vector<P> 	qs(n*2);
	
	rep(i,n){
		while(k>1&&(qs[k-1]-qs[k-2]).det(ps[i]-qs[k-1])<=0)
			k--;
		qs[k++]=ps[i];
	}
	for(int i=n-2,t=k;i>=0;i--){
		while(k>t&&(qs[k-1]-qs[k-2]).det(ps[i]-qs[k-1])<=0)
		k--;
		qs[k++]=ps[i];
	}
	qs.resize(k-1);
	return qs;
}

int n,m;
vector<double> p;
P ps[101];
char c[520][520];
long long mod=1000000007;
long long pow(ll i,ll j){
	ll tmp=1;
	while(j){
		if(j%2) tmp=tmp*i%mod;
		i=i*i%mod;
		j/=2;
	}
	return tmp;
}
int cards[10];
bool ok;
void saiki(int deep){
	if(deep==4){
		ok=true;
		return;
	}
	for(int i=1;i<=7;i++){
		if(cards[i]>0&&cards[i+1]>0&&cards[i+2]>0){
			cards[i]--;
			cards[i+1]--;
			cards[i+2]--;
			saiki(deep+1);
			cards[i]++;
			cards[i+1]++;
			cards[i+2]++;
		}
		for(int i=1;i<=9;i++){
			if(cards[i]>2){
				cards[i]-=3;
				saiki(deep+1);
				cards[i]+=3;
			}
		}
	}
	
}

int main(){
	
	string s;
	while(cin>>s){
		for(int i=0;i<=9;i++){
			cards[i]=0;
		}
		queue<int> a;
		bool f=false;
		
	rep(i,s.size()){
		cards[s[i]-'0']++;
		if(cards[s[i]-'0']>4){
			f=true;
			break;
		}
	}
		if(!f){
	for(int i=1;i<=9;i++){
		if(cards[i]<4){
			ok=false;
			cards[i]++;
			for(int j=1;j<=9;j++){
				if(cards[j]>1){
					cards[j]-=2;
					saiki(0);
					if(ok){
						a.push(i);
						cards[j]+=2;
						break;
					}
					cards[j]+=2;
				}
			}
			cards[i]--;
		}
			
	}
	}
		if(f||a.empty())
			cout<<0<<endl;
	else{
		cout<<a.front();
		a.pop();
		while(!a.empty()){
			cout<<" "<<a.front();
			a.pop();
		}
		cout<<endl;
		
	}
	}
}
accuracy: 1
time_ms: 20
memory_kb: 1196
final_rank: 4

code_snippet: #include<iostream>
#include<vector>
#include<queue>
#include<string>
using namespace std;
queue<vector<int> >Q;
string S; int p[14];
int main() {
	while (cin >> S) {
		vector<int>s; int g[10] = { 0,0,0,0,0,0,0,0,0,0 };
		for (int i = 0; i < 13; i++) {
			p[i] = S[i] - '0'; g[p[i]]++;
		}
		for (int i = 0; i < 10; i++)s.push_back(g[i]);
		s.push_back(1); int f[10] = { 0,0,0,0,0,0,0,0,0,0 };
		for (int h = 1; h < 10; h++) {
			s[h]++; Q.push(s); int OK = 0;
			while (!Q.empty()) {
				vector<int>t = Q.front(), u; Q.pop(); int sum = 0;
				for (int i = 0; i < 10; i++) sum += t[i];
				if (sum == 0)OK = 1;
				for (int i = 0; i < 10; i++) {
					if (t[i] >= 2 && t[10] >= 1) { u = t; u[i] -= 2; u[10] = 0; Q.push(u); }
					if (t[i] >= 3) { u = t; u[i] -= 3; Q.push(u); }
				}
				for (int i = 0; i < 8; i++) {
					if (t[i] >= 1 && t[i + 1] >= 1 && t[i + 2] >= 1) {
						u = t; u[i] -= 1; u[i + 1] -= 1; u[i + 2] -= 1; Q.push(u);
					}
				}
			}
			for (int i = 0; i < 10; i++) {
				if (s[h] >= 5)OK = 0;
			}
			f[h] = OK; s[h]--;
		}
		int V = 0;
		for (int i = 1; i < 10; i++) {
			if (f[i] == 1) {
				if (V >= 1) { cout << ' '; }
				cout << i; V++;
			}
		}
		if (V == 0)cout << '0';
		cout << endl;
	}
	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 1268
final_rank: 2

code_snippet: #include <iostream>
#include <cstdio>
#include <algorithm>

using namespace std;

int mentu[16][3] = {
	{1,1,1},{2,2,2},{3,3,3},{4,4,4},{5,5,5},{6,6,6},{7,7,7},{8,8,8},{9,9,9},
	{1,2,3},{2,3,4},{3,4,5},{4,5,6},{5,6,7},{6,7,8},{7,8,9}
};

int main(){
	
	string str;
	
	while(true){
		
		int num[10], num_1[10], dummy[10];
		
		bool ans[10];
		
		cin >> str;
		if(cin.eof()){
			break;
		}
		
		for(int i = 0; i < 10; i++){
			num[i] = 0;
			ans[i] = false;
		}
		
		for(int i = 0; i < str.size(); i++){
			num[str[i] - '0']++;
		}
		
		for(int rest = 1; rest <= 9; rest++){
			for(int i = 0; i < 10; i++){
				num_1[i] = num[i];
			}
			
			if(num_1[rest] >= 4){
				continue;
			}else{
				num_1[rest]++;
			}
			
			for(int i = 0; i < 16; i++){
				for(int j = i; j < 16; j++){
					for(int k = j; k < 16; k++){
						for(int l = k; l < 16; l++){
							for(int m = 1; m <= 9; m++){
								
								for(int b = 1; b <= 9; b++){
									dummy[b] = 0;
								}
								
								for(int a = 0; a < 3; a++){
									dummy[mentu[i][a]]++;
									dummy[mentu[j][a]]++;
									dummy[mentu[k][a]]++;
									dummy[mentu[l][a]]++;
								}
								dummy[m] += 2;
								
								for(int b = 1; b <= 9; b++){
									if(num_1[b] != dummy[b]){
										break;
									}
									if(b == 9){
										ans[rest] = true;
									}
								}
								
							}
						}
					}
				}
			}
			/*
			cout << " ";
			
			for(int i = 1; i <= 9; i++){
				cout << dummy[i];
			}
			*/
			
		}
		
		int loop;
		
		bool found;
		
		found = false;
		
		for(loop = 1; loop <= 9; loop++){
			if(ans[loop]){
				cout << loop;
				found = true;
				break;
			}
		}
		
		for(loop = loop + 1; loop <= 9; loop++){
			if(ans[loop]){
				cout << " " << loop;
			}
		}
		
		if(!found){
			cout << "0";
		}
		cout << endl;
		
	}
	
	return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 1184
final_rank: 7

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

using namespace std;

typedef unsigned long long ull;
#define loop(i,a,b) for(int i=(a);i<ull(b);++i)
#define rep(i,n) loop(i,0,n)
#define all(a) (a).begin(), (a).end()

bool ok(int i, int j){
	return i == j && i < 9;
}

int main(){
	string s;
	vector<vector<int> > a, b;
	for(int i=1; i < 10; i++){
		vector<int> v;
		for(int j=0; j< 2; j++) v.push_back(i);
		b.push_back(v);
		v.push_back(i);
		a.push_back(v);
	}
	for(int i=1; i <= 7; i++){
		vector<int> v;
		for(int j=0; j < 3; j++) v.push_back(i+j);
		a.push_back(v);
	}

	while(cin >> s){
		int c[10] = {0};
		for(int i=0; i < s.size(); i++) c[s[i]-'0']++;
		string t = "123456789";

		vector<int> ret;
		rep(q, t.size()){
			c[t[q]-'0']++;
			bool p = true;
			rep(i, 10) if(4 < c[i]){c[t[q]-'0']--; p = false; break;}
			if(!p) continue;
			loop(i, 0, a.size())loop(j, 0, a.size()){
				if(ok(i, j)) continue;
				loop(k, 0, a.size()){
					if(ok(j, k) || ok(i, k)) continue;
					loop(l, 0, a.size()){
						if(ok(i, l) || ok(j, l) || ok(k, l)) continue;
						rep(m, b.size()){
							if(i == m || j == m || k == m || l == m) continue;
							int d[10] = {0};
							rep(n, 3){d[a[i][n]]++; d[a[j][n]]++; d[a[k][n]]++; d[a[l][n]]++;}
							rep(n, 2) d[b[m][n]]++;
							bool possible = true;
							rep(n, 10) if(c[n] != d[n]){possible = false; break;}
							if(possible) ret.push_back(q+1);
						}
					}
				}
			}
			c[t[q]-'0']--;
		}
		int n = -1;
		rep(i, ret.size()){
			if(n != ret[i]){cout << (i == 0 ? "" : " ") << ret[i]; n = ret[i];}
		}
		if(n == -1) cout << 0;
		cout << endl;
	}
}
accuracy: 1
time_ms: 690
memory_kb: 1228
final_rank: 15

code_snippet: #include <iostream>
#include <iomanip>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <bitset>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>

using namespace std;

typedef map<int, int> Num;

int check2(Num a) {
	int sum = 0;
	for (int i = 1; i <= 9; i++)
		sum += a[i];
	return sum;
}

void check1(Num a, int n, int *ans) {
	if (check2(a) == 0) {
		*ans |= (0x01 << n);
		return;
	}

	for (int i = 1; i <= 9; i++) {
		if (a[i] >= 3) {
			a[i] -= 3;
			check1(a, n, ans);
			a[i] += 3;
		}
	}

	for (int i = 3; i <= 9; i++) {
		if (a[i-2] && a[i-1] && a[i]) {
			a[i-2] -= 1;
			a[i-1] -= 1;
			a[i] -= 1;
			check1(a, n, ans);
			a[i-2] += 1;
			a[i-1] += 1;
			a[i] += 1;
		}
	}
}

int solve(Num a) {
	int ans = 0;
	for (int i = 1; i <= 9; i++) {
		if (a[i] == 4) continue;

		a[i]++;
		for (int j = 1; j <= 9; j++)
			if (a[j] > 1) {
				a[j] -= 2;
				check1(a, i, &ans);
				a[j] += 2;
			}
		a[i]--;
	}
	return ans;
}

int main() {
	string str;
	while (cin >> str) {
		Num a;
		for (int i = 1; i <= 9; i++)
			a[i] = 0;
		for (int i = 0; i < str.size(); i++) {
			int s = str[i] - '0';
			a[s]++;
		}
		int ans = solve(a);
		if (ans == 0)
			cout << 0 << endl;
		else {
			int ifs = 0;
			for (int i = 1; i <= 9; i++) {
				if (ans & (0x01 << i))
					cout << ((ifs++) ? " " : "") << i;
			}
			cout << endl;
		}
	}
	return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 1188
final_rank: 3

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

string str;

bool c (map<char,int> m) {
    for (auto i = m.begin(); i !=  m.end(); i++) {
        if (i->second != 0) return false;
    }
    return true;
}

bool dfs(map<char,int> m) {
    if (c(m)) return true;
    for (char i = '1'; i <= '9'; i++) {
        if (m[i] >= 3) {
            m[i] -= 3;
            if (dfs (m)) return true;;
            m[i] += 3;
        }
        if (i + 2 <= '9' && m[i] > 0 && m[i+1] > 0 &&  m[i+2] > 0) {
            m[i]--; m[i + 1]--; m[i + 2]--;
            if (dfs(m)) return true;
            m[i]++; m[i + 1]++; m[i + 2]++;
        }
    }
    return false;
}

int main ()
{
    while (cin >> str) {
        map<char,int> m;
        for (char i = '1' ; i <= '9'; i++) m[i] = 0;
        for (int i = 0; i < str.size(); i++) {
            m[str[i]]++;
        }

        vector<char> res;
        for (char i = '1'; i <= '9'; i++) {
            if (m[i] >= 4) continue;
            m[i]++;
            for (char j = '1'; j <= '9'; j++) {
                if (m[j] >= 2) {
                    m[j] -= 2;
                    if (dfs(m)) {
                        res.push_back(i);
                        m[j] += 2;
                        break;
                    }
                    m[j] += 2;
                }
            }
            m[i]--;
        }
        if (res.size()) {
            cout << res[0];
            for (int i = 1; i < res.size(); i++) cout << " " << res[i];
        } else cout << 0;
        cout << endl;
    }

    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 1204
final_rank: 1

code_snippet: #include <iostream>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cstring>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <bitset>
#include <numeric>
#include <utility>
#include <iomanip>
#include <algorithm>
#include <functional>
using namespace std;

typedef long long ll;
typedef vector<int> vint;
typedef vector<long long> vll;
typedef pair<int,int> pint;
typedef pair<long long, long long> pll;

#define MP make_pair
#define PB push_back
#define ALL(s) (s).begin(),(s).end()
#define EACH(i, s) for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i)
#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
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 << endl; }
template<class T1, class T2> ostream& operator << (ostream &s, map<T1,T2> P) 
{ EACH(it, P) { s << "<" << it->first << "->" << it->second << "> "; } return s << endl; }


string str;
set<vint> se;

int main() {
    se.clear();
    
    int j[4];
    for (int i = 1; i <= 9; ++i) {
        for (j[0] = 1; j[0] <= 16; ++j[0]) {
            for (j[1] = 1; j[1] <= 16; ++j[1]) {
                for (j[2] = 1; j[2] <= 16; ++j[2]) {
                    for (j[3] = 1; j[3] <= 16; ++j[3]) {
                        vint tmp(9, 0);
                        tmp[i-1] = 2;
                        for (int k = 0; k < 4; ++k) {
                            if (j[k] <= 9) tmp[j[k]-1] += 3;
                            else {
                                tmp[j[k]-10]++;
                                tmp[j[k]-9]++;
                                tmp[j[k]-8]++;
                            }
                        }
                        bool check = true;
                        for (int l = 0; l < 9; ++l) if (tmp[l] > 4) check = false;
                        if (check) se.insert(tmp);
                    }
                }
            }
        }
    }
    
    while (cin >> str) {
        vint vec(9, 0);
        for (int i = 0; i < str.size(); ++i) vec[ str[i]-'1' ]++;
        vint res;
        for (int add = 1; add <= 9; ++add) {
            vint tmp = vec;
            tmp[add-1]++;
            if (se.count(tmp)) res.PB(add);
        }
        
        if (res.size() == 0) cout << "0";
        else {
            for (int i = 0; i < res.size(); ++i) {
                cout << res[i];
                if (i != res.size()-1) cout << " ";
            }
        }
        cout << endl;
    }
    
    return 0;
}
accuracy: 1
time_ms: 70
memory_kb: 2660
final_rank: 13

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

typedef map<char, int> State;

bool finished(State &m){
  for(char c = '1' ; c <= '9' ; c++){
    if(m[c]) return false;
  }
  return true;
}

bool bfs(string s){
  State start;
  for(int i = 0 ; i < (int)s.size() ; i++){
    start[s[i]]++;
    if(start[s[i]] > 4) return false;
  }
  queue<State> que;
  for(char c = '1' ; c <= '9' ; c++){
    if(start[c] >= 2){
      State nex = start;
      nex[c] -= 2;
      que.push(nex);     
    }
  }
  
  while(!que.empty()){
    State q = que.front(); que.pop();
    
    if(finished(q)) return true;
    
    for(char c = '1' ; c <= '9' ; c++){
      if(q[c] >= 3){
	State nex = q;
	nex[c] -= 3;
	que.push(nex);
      }
    }
    for(char c = '1' ; c <= '7' ; c++){
      if(q[c] >= 1 && q[c+1] >= 1 && q[c+2] >= 1){
	State nex = q;
	nex[c] -= 1, nex[c+1] -= 1, nex[c+2] -= 1;
	que.push(nex);
      }
    }
  }
  return false;
}

int main(){
  string s;
  while(cin >> s){
    vector<char> ans;
    for(char c = '1' ; c <= '9' ; c++){
      if(bfs(s + c)) ans.push_back(c);
    }
    if(ans.size() == 0) cout << 0 << endl;
    else{
      for(int i = 0 ; i < (int)ans.size() ; i++){
	cout << ans[i];
	if(i == (int)ans.size()-1) cout << endl;
	else cout << ' ';
      }
    }
  }
  
  return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 1288
final_rank: 6

code_snippet: #include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
vector<int> a(14);
bool solve(int x, int y);
int main(){
  string s;
  while(cin >>s){
    vector<int> ans;
    for(int i=1; i<10; i++){
      int n[10]={};
      bool f = true;
      for(int j=0; j<s.size(); j++) a[j] = s[j]-'0';
      a[13] = i;
      sort(a.begin(),a.end());
      for(int j=0; j<a.size(); j++) n[a[j]]++;
      for(int j=1; j<10; j++) if(n[j]>4) f = false;
      if(f) if(solve(1,4)) ans.push_back(i);
    }
    if(ans.size() == 0) cout <<"0"<<endl;
    else{
      for(int i=0; i<ans.size(); i++){
	if(i != 0) cout <<" ";
	cout <<ans[i];
      }
      cout <<endl;
    }
  }
}
bool solve(int x, int y){
  int ans = false;
  if(x == 0 && y == 0) return true;
  if(x == 1){
    for(int i=0; i<13; i++){
      if(a[i] == -1) continue;
      if(a[i] == a[i+1]){
	int sc = a[i];
	a[i] = a[i+1] = -1;
	ans = ans|solve(0,y);
	a[i] = a[i+1] = sc;
      }
    }
  }
  if(y != 0){
    for(int i=0; i<12; i++){
      if(a[i] == -1) continue;
      if(a[i] == a[i+1] && a[i+1] == a[i+2]){
	int sc = a[i];
	a[i] = a[i+1] = a[i+2] = -1;
	ans = ans|solve(x,y-1);
	a[i] = a[i+1] = a[i+2] = sc;
      }
      for(int j=i+1; j<13; j++){
	if(a[j]!=a[i]+1) continue;
	for(int k=j+1; k<14; k++){
	  if(a[k]!=a[j]+1) continue;
	  int sc = a[i];
	  a[i] = a[j] = a[k] = -1;
	  ans = ans|solve(x,y-1);
	  a[i] = sc; a[j] = sc+1; a[k] = sc+2;
	}
      }
    }
  }
  return ans;
}
accuracy: 1
time_ms: 170
memory_kb: 1204
final_rank: 11

code_snippet: #include<stdio.h>
#include<string.h>
#include<set>
#include<vector>

std::set<std::vector<int> > all;

void search(std::vector<int> count,int p,int cPerm[2]){
	if(p==10&&(cPerm[0]+cPerm[1]==0)){
		all.insert(count);		
	}else{
		if(count[p]<=2&&cPerm[0]>0){
			cPerm[0]=0;
			count[p]+=2;
			search(count,p,cPerm);
			count[p]-=2;
			cPerm[0]=1;
		}
		if(count[p]<=1&&cPerm[1]>0){
			cPerm[1]--;
			count[p]+=3;
			search(count,p,cPerm);
			count[p]-=3;
			cPerm[1]++;
		}
		if(p<=7&&count[p]<4&&cPerm[1]>0){
			cPerm[1]--;
			count[p]++;
			count[p+1]++;
			count[p+2]++;
			search(count,p,cPerm);
			count[p]--;
			count[p+1]--;
			count[p+2]--;
			cPerm[1]++;
		}
		if(count[p]<=4)search(count,p+1,cPerm);
	}
}

int main(){
	char text[15];
	std::vector<int> count;
	int cPerm[2]={1,4};
	for(int i=0;i<=9;i++)count.push_back(0);
	search(count,1,cPerm);
	while(scanf("%s",text)!=EOF){
		for(int i=0;i<=9;i++)count[i]=0;
		for(int i=0;text[i]!='\0';i++)count[text[i]-'0']++;
		bool isFirst=true;
		
		for(int i=1;i<=9;i++){
			count[i]++;
			if(all.find(count)!=all.end()){
				if(isFirst==true){
					isFirst=false;
				}else{
					printf(" ");
				}
				printf("%d",i);
			}
			count[i]--;
		}
		printf("%s",isFirst?"0\n":"\n");
	}
}
accuracy: 1
time_ms: 10
memory_kb: 2480
final_rank: 10

code_snippet: #include<iostream>
#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
vector<int> a(14),b(14);
bool five();
bool tempy(int x, int y);
int main(){
  for(;;){
    vector<int> ans;
    if(scanf("%1d",&a[0]) == EOF) break;
    for(int i=1; i<13; i++) scanf("%1d",&a[i]);
    for(int i=1; i<=9; i++){
      a[13] = i;
      for(int j=0; j<14; j++) b[j] = a[j];
      sort(b.begin(),b.end());
      if(five()) continue;
      if(tempy(1,4)) ans.push_back(i); 
    }
    if(ans.empty()) ans.push_back(0);
    for(int i=0; i<ans.size(); i++){
      if(i != 0) cout <<" ";
      cout <<ans[i];
    }
    cout <<endl;
  }
  return 0;
}
bool five(){
  for(int i=0,j=0,k=0; i<14; i++){
    if(b[i] != j){j=b[i];k = 1;}
    else k++;
    if(k == 5) return true;
  }
  return false;
}
bool tempy(int x, int y){
  if(x == 0 && y == 0) return true;
  for(int i=0; i<14; i++){
    if(b[i] == 0) continue;
    if(x != 0){
      for(int j=i+1; j<14; j++){
	if(b[i] != b[j]) continue;
	int q = b[i];
	b[i] = b[j] = 0;
	if(tempy(0,y)) return true;
	b[i] = b[j] = q;
	break;
      }
    }
    if(y != 0){
      for(int j=i+1; j<14; j++){
	if(b[i] != b[j]) continue;
	for(int k=j+1; k<14; k++){
	  if(b[i] != b[k]) continue;
	  int q = b[i];
	  b[i] = b[j] = b[k] = 0;
	  if(tempy(x,y-1)) return true;
	  b[i] = b[j] = b[k] = q;
	  goto flag_pon;
	}
      }
    flag_pon:;
      for(int j=i+1; j<14; j++){
	if(b[i]+1 != b[j]) continue;
	for(int k=j+1; k<14; k++){
	  if(b[i]+2 != b[k]) continue;
	  int q = b[i];
	  b[i] = b[j] = b[k] = 0;
	  if(tempy(x,y-1)) return true;
	  b[i] = q;b[j] = q+1;b[k] = q+2;
	  goto flag_chi;
	}
      }
    flag_chi:;
    }
  }
  return false;
}
accuracy: 1
time_ms: 20
memory_kb: 1232
final_rank: 5

code_snippet: #include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
vector<int> h(14),a(13);
vector<bool> hf(14);
bool ten(int x, int y);
int main(){
  string s;
  while(cin >>s){
    bool f = false;
    for(int i=0; i<13; i++) a[i] = s[i]-'0';
    for(int i=1; i<=9; i++){
      int lot[10]={0};
      bool ax = true;
      for(int j=0; j<13; j++) h[j] = a[j];
      h[13] = i;
      for(int j=0; j<14; j++){hf[j] = true;lot[h[j]]++;}
      for(int j=1; j<10; j++) if(lot[j]>4) ax = false;
      if(ax){
	sort(h.begin(),h.end());
	if(ten(4,1)){
	  if(f) cout <<" ";
	  cout <<i;
	  f = true;
	}
      }
    }
    if(!f) cout <<"0";
    cout <<endl;
  }
  return 0;
}
bool ten(int x, int y){
  bool f=false;
  if(x == 0 && y == 0) return true;
  for(int i=0; i<14; i++){
    if(hf[i]){
      if(y!=0){
	for(int j=i+1; j<14; j++){
	  if(hf[j] && h[i] == h[j]){
	    hf[i] = hf[j] = false;
	    f|=ten(x,0);
	    if(f) return f;
	    hf[i] = hf[j] = true;
	    break;
	  }
	  else if(h[i]!=h[j]) break;
	}
      }
      if(x!=0){
	for(int j=i+1; j<14; j++){
	  if(hf[j] && h[i] == h[j]){
	    for(int k=j+1; k<14; k++){
	      if(hf[k] && h[i] == h[k]){
		hf[i] =	hf[j] = hf[k] = false;
		f|=ten(x-1,y);
		if(f) return f;
		hf[i] = hf[j] = hf[k] = true;
		break;
	      }
	      else if(h[i]!=h[k]) break;
	    }
	  }
	  else if(h[i]!=h[j]) break;
	}
	for(int j=i+1; j<14; j++){
	  if(hf[j] && h[i]+1 == h[j]){
	    for(int k=j+1; k<14; k++){
	      if(hf[k] && h[i]+2 == h[k]){
		hf[i] =	hf[j] = hf[k] = false;
		f|=ten(x-1,y);
		if(f) return f;
		hf[i] = hf[j] = hf[k] = true;
		break;
	      }
	      else if(h[i]+2<h[k]) break;
	    }
	  }
	  else if(h[i]+1<h[j]) break;
	}
      }
    }
  }
  return f;
}
accuracy: 1
time_ms: 40
memory_kb: 1200
final_rank: 8

code_snippet: #include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
vector<int> h(14);
vector<bool> hf(14);
bool ten(int x, int y);
int main(){
  string s;
  while(cin >>s){
    bool f = false;
    vector<int> a(13);
    for(int i=0; i<13; i++) a[i] = s[i]-'0';
    for(int i=1; i<=9; i++){
      int lot[10]={0};
      bool ax = true;
      for(int j=0; j<13; j++) h[j] = a[j];
      h[13] = i;
      for(int j=0; j<14; j++){
	hf[j] = true;
	lot[h[j]]++;
      }
      for(int j=1; j<10; j++) if(lot[j]>4) ax = false;
      if(ax){
	sort(h.begin(),h.end());
	if(ten(4,1)){
	  if(f) cout <<" ";
	  cout <<i;
	  f = true;
	}
      }
    }
    if(!f) cout <<"0";
    cout <<endl;
  }
  return 0;
}
bool ten(int x, int y){
  bool f=false;
  if(x == 0 && y == 0) return true;
  for(int i=0; i<14; i++){
    if(hf[i]){
      if(y!=0){
	for(int j=i+1; j<14; j++){
	  if(hf[j] && h[i] == h[j]){
	    hf[i] = false;
	    hf[j] = false;
	    f|=ten(x,0);
	    if(f) return f;
	    hf[i] = true;
	    hf[j] = true;
	    break;
	  }
	  else if(h[i]!=h[j]) break;
	}
      }
      if(x!=0){
	for(int j=i+1; j<14; j++){
	  if(hf[j] && h[i] == h[j]){
	    for(int k=j+1; k<14; k++){
	      if(hf[k] && h[i] == h[k]){
		hf[i] = false;
		hf[j] = false;
		hf[k] = false;
		f|=ten(x-1,y);
		if(f) return f;
		hf[i] = true;
		hf[j] = true;
		hf[k] = true;
		break;
	      }
	      else if(h[i]!=h[k]) break;
	    }
	  }
	  else if(h[i]!=h[j]) break;
	}
	for(int j=i+1; j<14; j++){
	  if(hf[j] && h[i]+1 == h[j]){
	    for(int k=j+1; k<14; k++){
	      if(hf[k] && h[i]+2 == h[k]){
		hf[i] = false;
		hf[j] = false;
		hf[k] = false;
		f|=ten(x-1,y);
		if(f) return f;
		hf[i] = true;
		hf[j] = true;
		hf[k] = true;
		break;
	      }
	      else if(h[i]+2<h[k]) break;
	    }
	  }
	  else if(h[i]+1<h[j]) break;
	}
      }
    }
  }
  return f;
}
accuracy: 1
time_ms: 50
memory_kb: 1200
final_rank: 9