Problem:
サイコロパズル
各面にアルファベット一文字(a 〜 z、A 〜 Z)が描かれたサイコロがあります。
このようなサイコロを8つ組み合わせて 2 × 2 × 2 の立方体を作ることを考えます。
組み合わせ方には条件があり、各サイコロの向き合う面は同じアルファベットでかつ一方が小文字、も
う一方が大文字でなければなりません。例えば、a と描かれた面に接することができるのは A と描か
れた面です。ただし、接するときの文字の向きは問いません。
このルールに従い、８つのサイコロの情報を入力とし、立方体を作れるか否かを判定するプログラムを作成してください。立方体を作れる場合は YES（半角英大文字）、作れない場合は NO（半角英大文字）と出力してください。
なお、サイコロの各面の文字を次の図にあるように
c1
〜
c6
と表すことにします。


  また、1つのサイコロに同じ文字が複数回描かれていることは無いものとします（同じアルファベットの大文字と小文字はその限りではありません）。
Input
複数のデータセットの並びが入力として与えられます。入力の終わりはゼロひとつの行で示されます。
各データセットは以下の形式で与えられます。
s
1
s
2
:
s
8
i
行目に
i
個目のサイコロの情報
s
i
が与えられます。
s
i
は長さ 6 の文字列であり
j
文字目がサイコロの各面
cj
に対応します。
データセットの数は 50 を超えません。
Output
データセットごとに判定結果（半角英大文字）を１行に出力します。
Sample Input
zabZNq
BCxmAi
ZcbBCj
aizXCm
QgmABC
JHzMop
ImoXGz
MZTOhp
zabZnQ
BCxmAi
ZcbBCj
aizXCm
QgmABC
JHzMop
ImoXGz
MZTOhp
abcdef
ABDCFE
FBDCAE
abcdef
BEACDF
bfcaed
fabcde
DEABCF
UnivOf
AizuaH
zTXZYW
piglIt
GRULNP
higGtH
uAzIXZ
FizmKZ
UnivOf
AizuaH
piglIt
higGtH
GRULNP
uAzIXZ
FizmKZ
ZTXzYW
0
Output for the Sample Input
YES
NO
YES
YES
NO

Submitted solutions:

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

#define READ_FROM_FILE 0

#if READ_FROM_FILE
#include <fstream>
#endif

using namespace std;
const int num_alphabets = 27;
const int num_dices = 8;
const int num_coordinates = 8;
const int num_states = 24;
string puzzle[2][2][2];
string dice_states[num_dices][num_states];
bool is_used[num_dices];
unordered_map<char, char> face_to_face_map;

string roll_west(string dice_str, int num_rolls) {
	char tmp;
	for (int i = 0; i < num_rolls; i++) {
		tmp = dice_str[3];
		dice_str[3] = dice_str[0];
		dice_str[0] = dice_str[2];
		dice_str[2] = dice_str[5];
		dice_str[5] = tmp;
	}
	return dice_str;
}

string roll_north(string dice_str, int num_rolls) {
	char tmp;
	for (int i = 0; i < num_rolls; i++) {
		tmp = dice_str[5];
		dice_str[5] = dice_str[4];
		dice_str[4] = dice_str[0];
		dice_str[0] = dice_str[1];
		dice_str[1] = tmp;
	}
	return dice_str;
}

string rotate_right(string dice_str, int num_rotates) {
	char tmp;
	for (int i = 0; i < num_rotates; i++) {
		tmp = dice_str[1];
		dice_str[1] = dice_str[2];
		dice_str[2] = dice_str[4];
		dice_str[4] = dice_str[3];
		dice_str[3] = tmp;
	}
	return dice_str;
}

bool input() {
	string buf, str0, str1;
	for (int i = 0; i < num_dices; i++) {
		cin >> buf;
		if (buf == "0") {
			return false;
		}
		int state_idx = 0;
		for (int j = 0; j < 6; j++) {
			if (j < 4) {
				str0 = roll_west(buf, j);
			} else if (j == 4) {
				str0 = roll_north(buf, 1);
			} else {
				str0 = roll_north(buf, 3);
			}
			for (int k = 0; k < 4; k++) {
				str1 = rotate_right(str0, k);
				dice_states[i][state_idx++] = str1;
			}
		}
	}
	return true;
}

bool backtrack(int coordinate_idx) {
	if (coordinate_idx == num_coordinates) {
		return true;
	}
	int z = coordinate_idx / 4;
	int y = (coordinate_idx % 4) / 2;
	int x = (coordinate_idx % 4) % 2;

	for (int dice_idx = 0; dice_idx < num_dices; dice_idx++) {
		if (is_used[dice_idx]) {
			continue;
		}
		for (int state_idx = 0; state_idx < num_states; state_idx++) {
			if (x > 0) {
				char face0 = puzzle[x - 1][y][z][2];
				char face1 = dice_states[dice_idx][state_idx][3];
				if (face_to_face_map[face0] != face1) {
					continue;
				}
			}
			if (y > 0) {
				char face0 = puzzle[x][y - 1][z][5];
				char face1 = dice_states[dice_idx][state_idx][0];
				if (face_to_face_map[face0] != face1) {
					continue;
				}
			}
			if (z > 0) {
				char face0 = puzzle[x][y][z - 1][4];
				char face1 = dice_states[dice_idx][state_idx][1];
				if (face_to_face_map[face0] != face1) {
					continue;
				}
			}

			is_used[dice_idx] = true;
			puzzle[x][y][z] = dice_states[dice_idx][state_idx];
			if (backtrack(coordinate_idx + 1)) {
				return true;
			}
			is_used[dice_idx] = false;
		}
	}

	return false;
}

int main() {
#if READ_FROM_FILE
	std::ifstream ifs("test0.txt");
	std::cin.rdbuf(ifs.rdbuf());
#endif
	for (int i = 0; i < num_alphabets; i++) {
		face_to_face_map[i + 'a'] = i + 'A';
		face_to_face_map[i + 'A'] = i + 'a';
	}
	while (input()) {
		for (int i = 0; i < 8; i++) {
			is_used[i] = false;
		}

		if (backtrack(0)) {
			cout << "YES" << endl;
		} else {
			cout << "NO" << endl;
		}
	}
	return 0;
}
accuracy: 1
time_ms: 780
memory_kb: 3104
final_rank: 7

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

#define READ_FROM_FILE 0

#if READ_FROM_FILE
#include <fstream>
#endif

using namespace std;
const int num_alphabets = 27;
const int num_dices = 8;
const int num_coordinates = 8;
const int num_states = 24;
string puzzle[2][2][2];
string dice_states[num_dices][num_states];
bool is_used[num_dices];
unordered_map<char, char> face_to_face_map;

string roll_west(string dice_str, int num_rolls) {
	char tmp;
	for (int i = 0; i < num_rolls; i++) {
		tmp = dice_str[3];
		dice_str[3] = dice_str[0];
		dice_str[0] = dice_str[2];
		dice_str[2] = dice_str[5];
		dice_str[5] = tmp;
	}
	return dice_str;
}

string roll_north(string dice_str, int num_rolls) {
	char tmp;
	for (int i = 0; i < num_rolls; i++) {
		tmp = dice_str[5];
		dice_str[5] = dice_str[4];
		dice_str[4] = dice_str[0];
		dice_str[0] = dice_str[1];
		dice_str[1] = tmp;
	}
	return dice_str;
}

string rotate_right(string dice_str, int num_rotates) {
	char tmp;
	for (int i = 0; i < num_rotates; i++) {
		tmp = dice_str[1];
		dice_str[1] = dice_str[2];
		dice_str[2] = dice_str[4];
		dice_str[4] = dice_str[3];
		dice_str[3] = tmp;
	}
	return dice_str;
}

bool input() {
	string buf, str0, str1;
	for (int i = 0; i < num_dices; i++) {
		cin >> buf;
		if (buf == "0")
			return false;
		int state_idx = 0;
		for (int j = 0; j < 6; j++) {
			if (j < 4)
				str0 = roll_west(buf, j);
			else if (j == 4)
				str0 = roll_north(buf, 1);
			else
				str0 = roll_north(buf, 3);
			for (int k = 0; k < 4; k++) {
				str1 = rotate_right(str0, k);
				dice_states[i][state_idx++] = str1;
			}
		}
	}
	return true;
}

bool backtrack(int coordinate_idx){
	if(coordinate_idx == num_coordinates){
		return true;
	}
	int z = coordinate_idx / 4;
	int y = (coordinate_idx % 4) / 2;
	int x = (coordinate_idx % 4) % 2;

	for(int dice_idx = 0; dice_idx < num_dices; dice_idx++){
		if(is_used[dice_idx]){
			continue;
		}
		for(int state_idx = 0; state_idx < num_states; state_idx++){
			if(x > 0){
				char face0 = puzzle[x - 1][y][z][2];
				char face1 = dice_states[dice_idx][state_idx][3];
				if(face_to_face_map[face0] != face1){
					continue;
				}
			}
			if(y > 0){
				char face0 = puzzle[x][y - 1][z][5];
				char face1 = dice_states[dice_idx][state_idx][0];
				if(face_to_face_map[face0] != face1){
					continue;
				}
			}
			if(z > 0){
				char face0 = puzzle[x][y][z - 1][4];
				char face1 = dice_states[dice_idx][state_idx][1];
				if(face_to_face_map[face0] != face1){
					continue;
				}
			}

			is_used[dice_idx] = true;
			puzzle[x][y][z] = dice_states[dice_idx][state_idx];
			if(backtrack(coordinate_idx + 1)){
				return true;
			}
			is_used[dice_idx] = false;
		}
	}

	return false;
}

bool dfs(int coordinate_idx) {
	if (coordinate_idx == num_coordinates) {
		return true;
	}
	int z = coordinate_idx / 4;
	int y = (coordinate_idx % 4) / 2;
	int x = (coordinate_idx % 4) % 2;

	for (int dice_idx = 0; dice_idx < num_dices; dice_idx++) {
		if (is_used[dice_idx])
			continue;
		for (int state_idx = 0; state_idx < num_states; state_idx++) {
			if (x > 0) {
				char face0 = puzzle[x - 1][y][z][2];
				char face1 = dice_states[dice_idx][state_idx][3];
				if (face_to_face_map[face0] != face1) {
					continue;
				}
			}
			if (y > 0) {
				char face0 = puzzle[x][y - 1][z][5];
				char face1 = dice_states[dice_idx][state_idx][0];
				if (face_to_face_map[face0] != face1) {
					continue;
				}
			}
			if (z > 0) {
				char face0 = puzzle[x][y][z - 1][4];
				char face1 = dice_states[dice_idx][state_idx][1];
				if (face_to_face_map[face0] != face1) {
					continue;
				}
			}

			is_used[dice_idx] = true;
			puzzle[x][y][z] = dice_states[dice_idx][state_idx];
			if (dfs(coordinate_idx + 1)) {
				return true;
			}
			is_used[dice_idx] = false;
		}
	}
	return false;
}

int main() {
#if READ_FROM_FILE
	std::ifstream ifs("test0.txt");
	std::cin.rdbuf(ifs.rdbuf());
#endif
	for (int i = 0; i < num_alphabets; i++) {
		face_to_face_map[i + 'a'] = i + 'A';
		face_to_face_map[i + 'A'] = i + 'a';
	}
	while (input()) {
		for (int i = 0; i < 8; i++)
			is_used[i] = false;

		if (backtrack(0))
			cout << "YES" << endl;
		else
			cout << "NO" << endl;
	}
	return 0;
}
accuracy: 1
time_ms: 760
memory_kb: 3220
final_rank: 15

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

#define READ_FROM_FILE 0

#if READ_FROM_FILE
#include <fstream>
#endif

using namespace std;
const int num_alphabets = 27;
const int num_dices = 8;
const int num_coordinates = 8;
const int num_states = 24;
string puzzle[2][2][2];
string dice_states[num_dices][num_states];
bool is_used[num_dices];
unordered_map<char, char> face_to_face_map;

string roll_west(string dice_str, int num_rolls) {
	char tmp;
	for (int i = 0; i < num_rolls; i++) {
		tmp = dice_str[3];
		dice_str[3] = dice_str[0];
		dice_str[0] = dice_str[2];
		dice_str[2] = dice_str[5];
		dice_str[5] = tmp;
	}
	return dice_str;
}

string roll_north(string dice_str, int num_rolls) {
	char tmp;
	for (int i = 0; i < num_rolls; i++) {
		tmp = dice_str[5];
		dice_str[5] = dice_str[4];
		dice_str[4] = dice_str[0];
		dice_str[0] = dice_str[1];
		dice_str[1] = tmp;
	}
	return dice_str;
}

string rotate_right(string dice_str, int num_rotates) {
	char tmp;
	for (int i = 0; i < num_rotates; i++) {
		tmp = dice_str[1];
		dice_str[1] = dice_str[2];
		dice_str[2] = dice_str[4];
		dice_str[4] = dice_str[3];
		dice_str[3] = tmp;
	}
	return dice_str;
}

bool input() {
	string buf, str0, str1;
	for (int i = 0; i < num_dices; i++) {
		cin >> buf;
		if (buf == "0")
			return false;
		int state_idx = 0;
		for (int j = 0; j < 6; j++) {
			if (j < 4)
				str0 = roll_west(buf, j);
			else if (j == 4)
				str0 = roll_north(buf, 1);
			else
				str0 = roll_north(buf, 3);
			for (int k = 0; k < 4; k++) {
				str1 = rotate_right(str0, k);
				dice_states[i][state_idx++] = str1;
			}
		}
	}
	return true;
}

bool backtrack(int coordinate_idx){
	if(coordinate_idx == num_coordinates){
		return true;
	}
	int z = coordinate_idx / 4;
	int y = (coordinate_idx % 4) / 2;
	int x = (coordinate_idx % 4) % 2;

	for(int dice_idx = 0; dice_idx < num_dices; dice_idx++){
		if(is_used[dice_idx]){
			continue;
		}
		for(int state_idx = 0; state_idx < num_states; state_idx++){
			std::string dice1 = dice_states[dice_idx][state_idx];

			if(x > 0){
				std::string dice0 = puzzle[x - 1][y][z];
				int face0 = 2;
				int face1 = 3;
				char str_face0 = dice0[face0];
				char str_face1 = dice1[face1];
				if(face_to_face_map[str_face0] != str_face1){
					continue;
				}
			}
			if(y > 0){
				std::string dice0 = puzzle[x][y - 1][z];
				int face0 = 5;
				int face1 = 0;
				char str_face0 = dice0[face0];
				char str_face1 = dice1[face1];
				if(face_to_face_map[str_face0] != str_face1){
					continue;
				}
			}
			if(z > 0){
				std::string dice0 = puzzle[x][y][z - 1];
				int face0 = 4;
				int face1 = 1;
				char str_face0 = dice0[face0];
				char str_face1 = dice1[face1];
				if(face_to_face_map[str_face0] != str_face1){
					continue;
				}
			}

			puzzle[x][y][z] = dice1;
			is_used[dice_idx] = true;
			if(backtrack(coordinate_idx + 1)){
				return true;
			}
			is_used[dice_idx] = false;
		}
	}

	return false;
}

bool dfs(int coordinate_idx) {
	if (coordinate_idx == num_coordinates) {
		return true;
	}
	int z = coordinate_idx / 4;
	int y = (coordinate_idx % 4) / 2;
	int x = (coordinate_idx % 4) % 2;

	for (int dice_idx = 0; dice_idx < num_dices; dice_idx++) {
		if (is_used[dice_idx])
			continue;
		for (int state_idx = 0; state_idx < num_states; state_idx++) {
			if (x > 0) {
				char face0 = puzzle[x - 1][y][z][2];
				char face1 = dice_states[dice_idx][state_idx][3];
				if (face_to_face_map[face0] != face1) {
					continue;
				}
			}
			if (y > 0) {
				char face0 = puzzle[x][y - 1][z][5];
				char face1 = dice_states[dice_idx][state_idx][0];
				if (face_to_face_map[face0] != face1) {
					continue;
				}
			}
			if (z > 0) {
				char face0 = puzzle[x][y][z - 1][4];
				char face1 = dice_states[dice_idx][state_idx][1];
				if (face_to_face_map[face0] != face1) {
					continue;
				}
			}

			is_used[dice_idx] = true;
			puzzle[x][y][z] = dice_states[dice_idx][state_idx];
			if (dfs(coordinate_idx + 1)) {
				return true;
			}
			is_used[dice_idx] = false;
		}
	}
	return false;
}

int main() {
#if READ_FROM_FILE
	std::ifstream ifs("test0.txt");
	std::cin.rdbuf(ifs.rdbuf());
#endif
	for (int i = 0; i < num_alphabets; i++) {
		face_to_face_map[i + 'a'] = i + 'A';
		face_to_face_map[i + 'A'] = i + 'a';
	}
	while (input()) {
		for (int i = 0; i < 8; i++)
			is_used[i] = false;

		if (dfs(0))
			cout << "YES" << endl;
		else
			cout << "NO" << endl;
	}
	return 0;
}
accuracy: 1
time_ms: 780
memory_kb: 3056
final_rank: 4

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

#define READ_FROM_FILE 0

#if READ_FROM_FILE
#include <fstream>
#endif

using namespace std;
const int num_alphabets = 27;
const int num_dices = 8;
const int num_coordinates = 8;
const int num_states = 24;
string puzzle[2][2][2];
string dice_states[num_dices][num_states];
bool is_used[8];
unordered_map<char, char> face_to_face_map;

string roll_west(string dice_str, int num_rolls) {
	char tmp;
	for (int i = 0; i < num_rolls; i++) {
		tmp = dice_str[3];
		dice_str[3] = dice_str[0];
		dice_str[0] = dice_str[2];
		dice_str[2] = dice_str[5];
		dice_str[5] = tmp;
	}
	return dice_str;
}

string roll_north(string dice_str, int num_rolls) {
	char tmp;
	for (int i = 0; i < num_rolls; i++) {
		tmp = dice_str[5];
		dice_str[5] = dice_str[4];
		dice_str[4] = dice_str[0];
		dice_str[0] = dice_str[1];
		dice_str[1] = tmp;
	}
	return dice_str;
}

string rotate_right(string dice_str, int num_rotates) {
	char tmp;
	for (int i = 0; i < num_rotates; i++) {
		tmp = dice_str[1];
		dice_str[1] = dice_str[2];
		dice_str[2] = dice_str[4];
		dice_str[4] = dice_str[3];
		dice_str[3] = tmp;
	}
	return dice_str;
}

bool input() {
	string buf, str0, str1;
	for (int i = 0; i < num_dices; i++) {
		cin >> buf;
		if (buf == "0")
			return false;
		int state_idx = 0;
		for (int j = 0; j < 6; j++) {
			if (j < 4)
				str0 = roll_west(buf, j);
			else if (j == 4)
				str0 = roll_north(buf, 1);
			else
				str0 = roll_north(buf, 3);
			for (int k = 0; k < 4; k++) {
				str1 = rotate_right(str0, k);
				dice_states[i][state_idx++] = str1;
			}
		}
	}
	return true;
}

bool dfs(int coordinate_idx) {
	if (coordinate_idx == num_coordinates) {
		return true;
	}
	int z = coordinate_idx / 4;
	int y = (coordinate_idx % 4) / 2;
	int x = (coordinate_idx % 4) % 2;

	for (int dice_idx = 0; dice_idx < num_dices; dice_idx++) {
		if (is_used[dice_idx])
			continue;
		for (int state_idx = 0; state_idx < num_states; state_idx++) {
			if (x > 0) {
				char face0 = puzzle[x - 1][y][z][2];
				char face1 = dice_states[dice_idx][state_idx][3];
				if (face_to_face_map[face0] != face1) {
					continue;
				}
			}
			if (y > 0) {
				char face0 = puzzle[x][y - 1][z][5];
				char face1 = dice_states[dice_idx][state_idx][0];
				if (face_to_face_map[face0] != face1) {
					continue;
				}
			}
			if (z > 0) {
				char face0 = puzzle[x][y][z - 1][4];
				char face1 = dice_states[dice_idx][state_idx][1];
				if (face_to_face_map[face0] != face1) {
					continue;
				}
			}

			is_used[dice_idx] = true;
			puzzle[x][y][z] = dice_states[dice_idx][state_idx];
			if (dfs(coordinate_idx + 1)) {
				return true;
			}
			is_used[dice_idx] = false;
		}
	}
	return false;
}

int main() {
#if READ_FROM_FILE
	std::ifstream ifs("test0.txt");
	std::cin.rdbuf(ifs.rdbuf());
#endif
	for (int i = 0; i < num_alphabets; i++) {
		face_to_face_map[i + 'a'] = i + 'A';
		face_to_face_map[i + 'A'] = i + 'a';
	}
	while (input()) {
		for (int i = 0; i < 8; i++)
			is_used[i] = false;

		if (dfs(0))
			cout << "YES" << endl;
		else
			cout << "NO" << endl;
	}
	return 0;
}
accuracy: 1
time_ms: 780
memory_kb: 3236
final_rank: 16

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

#define READ_FROM_FILE 0

#if READ_FROM_FILE
#include <fstream>
#endif

using namespace std;
const int num_alphabets = 27;
const int num_dices = 8;
const int num_coordinates = 8;
const int num_states_per_dice = 24;
string puzzle[2][2][2];
string dice_states[num_dices][num_states_per_dice];
bool is_used[8];
unordered_map<char, char> face_to_face_map;

string roll_west(string dice_str, int num_rolls) {
	char tmp;
	for (int i = 0; i < num_rolls; i++) {
		tmp = dice_str[3];
		dice_str[3] = dice_str[0];
		dice_str[0] = dice_str[2];
		dice_str[2] = dice_str[5];
		dice_str[5] = tmp;
	}
	return dice_str;
}

string roll_north(string dice_str, int num_rolls) {
	char tmp;
	for (int i = 0; i < num_rolls; i++) {
		tmp = dice_str[5];
		dice_str[5] = dice_str[4];
		dice_str[4] = dice_str[0];
		dice_str[0] = dice_str[1];
		dice_str[1] = tmp;
	}
	return dice_str;
}

string rotate_right(string dice_str, int num_rotates) {
	char tmp;
	for (int i = 0; i < num_rotates; i++) {
		tmp = dice_str[1];
		dice_str[1] = dice_str[2];
		dice_str[2] = dice_str[4];
		dice_str[4] = dice_str[3];
		dice_str[3] = tmp;
	}
	return dice_str;
}

bool input() {
	string buf, str0, str1;
	for (int i = 0; i < num_dices; i++) {
		cin >> buf;
		if (buf == "0")
			return false;
		int state_idx = 0;
		for (int j = 0; j < 6; j++) {
			if (j < 4)
				str0 = roll_west(buf, j);
			else if (j == 4)
				str0 = roll_north(buf, 1);
			else
				str0 = roll_north(buf, 3);
			for (int k = 0; k < 4; k++) {
				str1 = rotate_right(str0, k);
				dice_states[i][state_idx++] = str1;
			}
		}
	}
	return true;
}

bool dfs(int coordinate_idx) {
	if (coordinate_idx == num_coordinates) {
		return true;
	}
	int z = coordinate_idx / 4;
	int y = (coordinate_idx % 4) / 2;
	int x = (coordinate_idx % 4) % 2;

	for (int dice_idx = 0; dice_idx < num_dices; dice_idx++) {
		if (is_used[dice_idx])
			continue;
		for (int state_idx = 0; state_idx < num_states_per_dice; state_idx++) {
			if (x > 0) {
				char face0 = puzzle[x - 1][y][z][2];
				char face1 = dice_states[dice_idx][state_idx][3];
				if (face_to_face_map[face0] != face1) {
					continue;
				}
			}
			if (y > 0) {
				char face0 = puzzle[x][y - 1][z][5];
				char face1 = dice_states[dice_idx][state_idx][0];
				if (face_to_face_map[face0] != face1) {
					continue;
				}
			}
			if (z > 0) {
				char face0 = puzzle[x][y][z - 1][4];
				char face1 = dice_states[dice_idx][state_idx][1];
				if (face_to_face_map[face0] != face1) {
					continue;
				}
			}

			is_used[dice_idx] = true;
			puzzle[x][y][z] = dice_states[dice_idx][state_idx];
			if (dfs(coordinate_idx + 1)) {
				return true;
			}
			is_used[dice_idx] = false;
		}
	}
	return false;
}

int main() {
#if READ_FROM_FILE
	std::ifstream ifs("test0.txt");
	std::cin.rdbuf(ifs.rdbuf());
#endif
	for (int i = 0; i < num_alphabets; i++) {
		face_to_face_map[i + 'a'] = i + 'A';
		face_to_face_map[i + 'A'] = i + 'a';
	}
	while (input()) {
		for (int i = 0; i < 8; i++)
			is_used[i] = false;

		if (dfs(0))
			cout << "YES" << endl;
		else
			cout << "NO" << endl;
	}
	return 0;
}
accuracy: 1
time_ms: 780
memory_kb: 3104
final_rank: 7

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

#define READ_FROM_FILE 0

#if READ_FROM_FILE
#include <fstream>
#endif

using namespace std;
const int num_alphabets = 27;
const int num_dices = 8;
const int num_coordinates = 8;
const int num_states_per_dice = 24;
string puzzle[2][2][2];
string dice_states[num_dices][num_states_per_dice];
bool is_used[8];
unordered_map<char, char> face_to_face_map;


string roll_west(string dice_str, int num_rolls) {
	char tmp;
	for (int i = 0; i < num_rolls; i++) {
		tmp = dice_str[3];
		dice_str[3] = dice_str[0];
		dice_str[0] = dice_str[2];
		dice_str[2] = dice_str[5];
		dice_str[5] = tmp;
	}
	return dice_str;
}

string roll_north(string dice_str, int num_rolls) {
	char tmp;
	for (int i = 0; i < num_rolls; i++) {
		tmp = dice_str[5];
		dice_str[5] = dice_str[4];
		dice_str[4] = dice_str[0];
		dice_str[0] = dice_str[1];
		dice_str[1] = tmp;
	}
	return dice_str;
}

string rotate_right(string dice_str, int num_rotates) {
	char tmp;
	for (int i = 0; i < num_rotates; i++) {
		tmp = dice_str[1];
		dice_str[1] = dice_str[2];
		dice_str[2] = dice_str[4];
		dice_str[4] = dice_str[3];
		dice_str[3] = tmp;
	}
	return dice_str;
}

bool input() {
	string in, str, str2;
	for (int i = 0; i < num_dices; i++) {
		cin >> in;
		if (in == "0")
			return false;
		int n = 0;
		for (int j = 0; j < 6; j++) {
			if (j < 4)
				str = roll_west(in, j);
			else if (j == 4)
				str = roll_north(in, 1);
			else
				str = roll_north(in, 3);
			for (int k = 0; k < 4; k++) {
				str2 = rotate_right(str, k);
				dice_states[i][n++] = str2;
			}
		}
	}
	return true;
}

bool dfs(int coordinate_idx) {
	if(coordinate_idx == num_coordinates){
		return true;
	}
	int z = coordinate_idx / 4;
	int y = (coordinate_idx % 4) / 2;
	int x = (coordinate_idx % 4) % 2;


	for (int dice_idx = 0; dice_idx < num_dices; dice_idx++) {
		if (is_used[dice_idx])
			continue;
		for (int state_idx = 0; state_idx < num_states_per_dice; state_idx++) {
			if (x > 0){
				char face0 = puzzle[x - 1][y][z][2];
				char face1 = dice_states[dice_idx][state_idx][3];
				if (face_to_face_map[face0] != face1){
					continue;
				}
			}
			if (y > 0){
				char face0 = puzzle[x][y - 1][z][5];
				char face1 = dice_states[dice_idx][state_idx][0];
				if (face_to_face_map[face0] != face1){
					continue;
				}
			}
			if (z > 0){
				char face0 = puzzle[x][y][z - 1][4];
				char face1 = dice_states[dice_idx][state_idx][1];
				if (face_to_face_map[face0] != face1){
					continue;
				}
			}

			is_used[dice_idx] = true;
			puzzle[x][y][z] = dice_states[dice_idx][state_idx];
			if(dfs(coordinate_idx + 1)){
				return true;
			}
			is_used[dice_idx] = false;
		}
	}
	return false;
}

int main() {
#if READ_FROM_FILE
	std::ifstream ifs("test0.txt");
	std::cin.rdbuf(ifs.rdbuf());
#endif
	for(int i = 0; i < num_alphabets; i++){
		face_to_face_map[i + 'a'] = i + 'A';
		face_to_face_map[i + 'A'] = i + 'a';
	}
	while (input()) {
		for (int i = 0; i < 8; i++)
			is_used[i] = false;

		if (dfs(0))
			cout << "YES" << endl;
		else
			cout << "NO" << endl;
	}
	return 0;
}
accuracy: 1
time_ms: 780
memory_kb: 3256
final_rank: 17

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

#define READ_FROM_FILE 0

#if READ_FROM_FILE
#include <fstream>
#endif

using namespace std;
const int num_alphabets = 27;
const int num_dices = 8;
const int num_coordinates = 8;
const int num_states_per_dice = 24;
string puzzle[2][2][2];
string dice_states[192];
bool is_used[8];
unordered_map<char, char> face_to_face_map;


string roll_west(string dice_str, int num_rolls) {
	char tmp;
	for (int i = 0; i < num_rolls; i++) {
		tmp = dice_str[3];
		dice_str[3] = dice_str[0];
		dice_str[0] = dice_str[2];
		dice_str[2] = dice_str[5];
		dice_str[5] = tmp;
	}
	return dice_str;
}

string roll_north(string dice_str, int num_rolls) {
	char tmp;
	for (int i = 0; i < num_rolls; i++) {
		tmp = dice_str[5];
		dice_str[5] = dice_str[4];
		dice_str[4] = dice_str[0];
		dice_str[0] = dice_str[1];
		dice_str[1] = tmp;
	}
	return dice_str;
}

string rotate_right(string dice_str, int num_rotates) {
	char tmp;
	for (int i = 0; i < num_rotates; i++) {
		tmp = dice_str[1];
		dice_str[1] = dice_str[2];
		dice_str[2] = dice_str[4];
		dice_str[4] = dice_str[3];
		dice_str[3] = tmp;
	}
	return dice_str;
}

bool input() {
	string in, str, str2;
	int n = 0;
	for (int i = 0; i < num_dices; i++) {
		cin >> in;
		if (in == "0")
			return false;
		for (int j = 0; j < 6; j++) {
			if (j < 4)
				str = roll_west(in, j);
			else if (j == 4)
				str = roll_north(in, 1);
			else
				str = roll_north(in, 3);
			for (int k = 0; k < 4; k++) {
				str2 = rotate_right(str, k);
				dice_states[n++] = str2;
			}
		}
	}
	return true;
}

bool dfs(int coordinate_idx) {
	if(coordinate_idx == num_coordinates){
		return true;
	}
	int z = coordinate_idx / 4;
	int y = (coordinate_idx % 4) / 2;
	int x = (coordinate_idx % 4) % 2;


	for (int j = 0; j < num_dices; j++) {
		if (is_used[j])
			continue;
		for (int i = j * num_states_per_dice; i < j * num_states_per_dice + num_states_per_dice; i++) {
			if (x > 0){
				char face0 = puzzle[x - 1][y][z][2];
				char face1 = dice_states[i][3];
				if (face_to_face_map[face0] != face1){
					continue;
				}
			}
			if (y > 0){
				char face0 = puzzle[x][y - 1][z][5];
				char face1 = dice_states[i][0];
				if (face_to_face_map[face0] != face1){
					continue;
				}
			}
			if (z > 0){
				char face0 = puzzle[x][y][z - 1][4];
				char face1 = dice_states[i][1];
				if (face_to_face_map[face0] != face1){
					continue;
				}
			}

			is_used[j] = true;
			puzzle[x][y][z] = dice_states[i];
			if(dfs(coordinate_idx + 1)){
				return true;
			}
			is_used[j] = false;
		}
	}
	return false;
}

int main() {
#if READ_FROM_FILE
	std::ifstream ifs("test0.txt");
	std::cin.rdbuf(ifs.rdbuf());
#endif
	for(int i = 0; i < num_alphabets; i++){
		face_to_face_map[i + 'a'] = i + 'A';
		face_to_face_map[i + 'A'] = i + 'a';
	}
	while (input()) {
		for (int i = 0; i < 8; i++)
			is_used[i] = false;

		if (dfs(0))
			cout << "YES" << endl;
		else
			cout << "NO" << endl;
	}
	return 0;
}
accuracy: 1
time_ms: 810
memory_kb: 3188
final_rank: 12

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

#define READ_FROM_FILE 0

#if READ_FROM_FILE
#include <fstream>
#endif

using namespace std;
const int num_alphabets = 27;
const int num_dices = 8;
const int num_coordinates = 8;
const int num_states_per_dice = 24;
string puzzle[2][2][2];
string dice_states[192];
bool is_used[8];
unordered_map<char, char> face_to_face_map;

bool input();
string roll_west(string, int);
string roll_north(string, int);
string rotate_right(string, int);
bool dfs(int);


int main() {
#if READ_FROM_FILE
	std::ifstream ifs("test0.txt");
	std::cin.rdbuf(ifs.rdbuf());
#endif
	for(int i = 0; i < num_alphabets; i++){
		face_to_face_map[i + 'a'] = i + 'A';
		face_to_face_map[i + 'A'] = i + 'a';
	}
	while (input()) {
		for (int i = 0; i < 8; i++)
			is_used[i] = false;

		if (dfs(0))
			cout << "YES" << endl;
		else
			cout << "NO" << endl;
	}
	return 0;
}

bool dfs(int coordinate_idx) {
	if(coordinate_idx == num_coordinates){
		return true;
	}
	int z = coordinate_idx / 4;
	int y = (coordinate_idx % 4) / 2;
	int x = (coordinate_idx % 4) % 2;


	for (int j = 0; j < num_dices; j++) {
		if (is_used[j])
			continue;
		for (int i = j * num_states_per_dice; i < j * num_states_per_dice + num_states_per_dice; i++) {
			if (x > 0){
				char face0 = puzzle[x - 1][y][z][2];
				char face1 = dice_states[i][3];
				if (face_to_face_map[face0] != face1){
					continue;
				}
			}
			if (y > 0){
				char face0 = puzzle[x][y - 1][z][5];
				char face1 = dice_states[i][0];
				if (face_to_face_map[face0] != face1){
					continue;
				}
			}
			if (z > 0){
				char face0 = puzzle[x][y][z - 1][4];
				char face1 = dice_states[i][1];
				if (face_to_face_map[face0] != face1){
					continue;
				}
			}

			is_used[j] = true;
			puzzle[x][y][z] = dice_states[i];
			if(dfs(coordinate_idx + 1)){
				return true;
			}
			is_used[j] = false;
		}
	}
	return false;
}

bool input() {
	string in, str, str2;
	int n = 0;
	for (int i = 0; i < num_dices; i++) {
		cin >> in;
		if (in == "0")
			return false;
		for (int j = 0; j < 6; j++) {
			if (j < 4)
				str = roll_west(in, j);
			else if (j == 4)
				str = roll_north(in, 1);
			else
				str = roll_north(in, 3);
			for (int k = 0; k < 4; k++) {
				str2 = rotate_right(str, k);
				dice_states[n++] = str2;
			}
		}
	}
	return true;
}

string roll_west(string str, int x) {
	char tmp;
	for (int i = 0; i < x; i++) {
		tmp = str[3];
		str[3] = str[0];
		str[0] = str[2];
		str[2] = str[5];
		str[5] = tmp;
	}
	return str;
}
string roll_north(string str, int x) {
	char tmp;
	for (int i = 0; i < x; i++) {
		tmp = str[5];
		str[5] = str[4];
		str[4] = str[0];
		str[0] = str[1];
		str[1] = tmp;
	}
	return str;
}
string rotate_right(string str, int x) {
	char tmp;
	for (int i = 0; i < x; i++) {
		tmp = str[1];
		str[1] = str[2];
		str[2] = str[4];
		str[4] = str[3];
		str[3] = tmp;
	}
	return str;
}
accuracy: 1
time_ms: 810
memory_kb: 3144
final_rank: 11

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

#define READ_FROM_FILE 0

#if READ_FROM_FILE
#include <fstream>
#endif

using namespace std;
string puzzle[2][2][2];
string dice_states[192];
bool is_used[8];
unordered_map<char, char> face_to_face_map;

const int num_alphabet = 27;
int checkch = 'A' - 'a';

bool input();
string roll_west(string, int);
string roll_north(string, int);
string rotate_right(string, int);
bool dfs(int);

inline bool check_ch(char x, char y) {
	return (x + checkch == y || x - checkch == y);
}

int main() {
#if READ_FROM_FILE
	std::ifstream ifs("test0.txt");
	std::cin.rdbuf(ifs.rdbuf());
#endif
	for(int i = 0; i < num_alphabet; i++){
		face_to_face_map[i + 'a'] = i + 'A';
		face_to_face_map[i + 'A'] = i + 'a';
	}
	while (input()) {
		for (int i = 0; i < 8; i++)
			is_used[i] = false;

		if (dfs(0))
			cout << "YES" << endl;
		else
			cout << "NO" << endl;
	}
	return 0;
}

bool dfs(int coordinate_idx) {
	if(coordinate_idx == 8){
		return true;
	}
	int z = coordinate_idx / 4;
	int y = (coordinate_idx % 4) / 2;
	int x = (coordinate_idx % 4) % 2;


	for (int j = 0; j < 8; j++) {
		if (is_used[j])
			continue;
		for (int i = j * 24; i < j * 24 + 24; i++) {
			if (x > 0){
				char face0 = puzzle[x - 1][y][z][2];
				char face1 = dice_states[i][3];
				if (face_to_face_map[face0] != face1){
					continue;
				}
			}
			if (y > 0){
				char face0 = puzzle[x][y - 1][z][5];
				char face1 = dice_states[i][0];
				if (face_to_face_map[face0] != face1){
					continue;
				}
			}
			if (z > 0){
				char face0 = puzzle[x][y][z - 1][4];
				char face1 = dice_states[i][1];
				if (face_to_face_map[face0] != face1){
					continue;
				}
			}

			is_used[j] = true;
			puzzle[x][y][z] = dice_states[i];
			if(dfs(coordinate_idx + 1)){
				return true;
			}
			is_used[j] = false;
		}
	}
	return false;
}

bool input() {
	string in, str, str2;
	int n = 0;
	for (int i = 0; i < 8; i++) {
		cin >> in;
		if (in == "0")
			return false;
		for (int j = 0; j < 6; j++) {
			if (j < 4)
				str = roll_west(in, j);
			else if (j == 4)
				str = roll_north(in, 1);
			else
				str = roll_north(in, 3);
			for (int k = 0; k < 4; k++) {
				str2 = rotate_right(str, k);
				dice_states[n++] = str2;
			}
		}
	}
	return true;
}

string roll_west(string str, int x) {
	char tmp;
	for (int i = 0; i < x; i++) {
		tmp = str[3];
		str[3] = str[0];
		str[0] = str[2];
		str[2] = str[5];
		str[5] = tmp;
	}
	return str;
}
string roll_north(string str, int x) {
	char tmp;
	for (int i = 0; i < x; i++) {
		tmp = str[5];
		str[5] = str[4];
		str[4] = str[0];
		str[0] = str[1];
		str[1] = tmp;
	}
	return str;
}
string rotate_right(string str, int x) {
	char tmp;
	for (int i = 0; i < x; i++) {
		tmp = str[1];
		str[1] = str[2];
		str[2] = str[4];
		str[4] = str[3];
		str[3] = tmp;
	}
	return str;
}
accuracy: 1
time_ms: 810
memory_kb: 3052
final_rank: 3

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

#define READ_FROM_FILE 0

#if READ_FROM_FILE
#include <fstream>
#endif

using namespace std;
string puzzle[2][2][2];
string dice_states[192];
bool is_used[8];
unordered_map<char, char> face_to_face_map;

const int num_alphabet = 27;
int checkch = 'A' - 'a';

bool input();
string roll_west(string, int);
string roll_north(string, int);
string rotate_right(string, int);
bool dfs(int);

inline bool check_ch(char x, char y) {
	return (x + checkch == y || x - checkch == y);
}

int main() {
#if READ_FROM_FILE
	std::ifstream ifs("test0.txt");
	std::cin.rdbuf(ifs.rdbuf());
#endif
	for(int i = 0; i < num_alphabet; i++){
		face_to_face_map[i + 'a'] = i + 'A';
		face_to_face_map[i + 'A'] = i + 'a';
	}
	while (input()) {
		for (int i = 0; i < 8; i++)
			is_used[i] = false;

		if (dfs(0))
			cout << "YES" << endl;
		else
			cout << "NO" << endl;
	}
	return 0;
}

bool dfs(int coordinate_idx) {
	if(coordinate_idx == 8){
		return true;
	}
	int z = coordinate_idx / 4;
	int y = (coordinate_idx % 4) / 2;
	int x = (coordinate_idx % 4) % 2;


	for (int j = 0; j < 8; j++) {
		if (is_used[j])
			continue;
		for (int i = j * 24; i < j * 24 + 24; i++) {
			if (x > 0){
				char face0 = puzzle[x - 1][y][z][2];
				char face1 = dice_states[i][3];
				if (face_to_face_map[face0] != face1){
					continue;
				}
			}
			if (y > 0){
				char face0 = puzzle[x][y - 1][z][4];
				char face1 = dice_states[i][1];
				if (face_to_face_map[face0] != face1){
					continue;
				}
			}
			if (z > 0){
				char face0 = puzzle[x][y][z - 1][0];
				char face1 = dice_states[i][5];
				if (face_to_face_map[face0] != face1){
					continue;
				}
			}

			is_used[j] = true;
			puzzle[x][y][z] = dice_states[i];
			if(dfs(coordinate_idx + 1)){
				return true;
			}
			is_used[j] = false;
		}
	}
	return false;
}

bool input() {
	string in, str, str2;
	int n = 0;
	for (int i = 0; i < 8; i++) {
		cin >> in;
		if (in == "0")
			return false;
		for (int j = 0; j < 6; j++) {
			if (j < 4)
				str = roll_west(in, j);
			else if (j == 4)
				str = roll_north(in, 1);
			else
				str = roll_north(in, 3);
			for (int k = 0; k < 4; k++) {
				str2 = rotate_right(str, k);
				dice_states[n++] = str2;
			}
		}
	}
	return true;
}

string roll_west(string str, int x) {
	char tmp;
	for (int i = 0; i < x; i++) {
		tmp = str[3];
		str[3] = str[0];
		str[0] = str[2];
		str[2] = str[5];
		str[5] = tmp;
	}
	return str;
}
string roll_north(string str, int x) {
	char tmp;
	for (int i = 0; i < x; i++) {
		tmp = str[5];
		str[5] = str[4];
		str[4] = str[0];
		str[0] = str[1];
		str[1] = tmp;
	}
	return str;
}
string rotate_right(string str, int x) {
	char tmp;
	for (int i = 0; i < x; i++) {
		tmp = str[1];
		str[1] = str[2];
		str[2] = str[4];
		str[4] = str[3];
		str[3] = tmp;
	}
	return str;
}
accuracy: 1
time_ms: 810
memory_kb: 3088
final_rank: 5

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

#define READ_FROM_FILE 0

#if READ_FROM_FILE
#include <fstream>
#endif

using namespace std;
string puzzle[2][2][2];
string dice_states[192];
bool is_used[8];
unordered_map<char, char> face_to_face_map;

const int num_alphabet = 27;
int checkch = 'A' - 'a';

bool input();
string roll_west(string, int);
string roll_north(string, int);
string rotate_right(string, int);
bool dfs(int);

inline bool check_ch(char x, char y) {
	return (x + checkch == y || x - checkch == y);
}

int main() {
#if READ_FROM_FILE
	std::ifstream ifs("test0.txt");
	std::cin.rdbuf(ifs.rdbuf());
#endif
	for(int i = 0; i < num_alphabet; i++){
		face_to_face_map[i + 'a'] = i + 'A';
		face_to_face_map[i + 'A'] = i + 'a';
	}
	while (input()) {
		for (int i = 0; i < 8; i++)
			is_used[i] = false;

		if (dfs(0))
			cout << "YES" << endl;
		else
			cout << "NO" << endl;
	}
	return 0;
}

bool dfs(int coordinate_idx) {
	if(coordinate_idx == 8){
		return true;
	}
	int z = coordinate_idx / 4;
	int y = (coordinate_idx % 4) / 2;
	int x = (coordinate_idx % 4) % 2;


	for (int j = 0; j < 8; j++) {
		if (is_used[j])
			continue;
		for (int i = j * 24; i < j * 24 + 24; i++) {
			if (x > 0){
				if (check_ch(puzzle[x - 1][y][z][2], dice_states[i][3]) == false){
					continue;
				}
			}
			if (y > 0){
				if (check_ch(puzzle[x][y - 1][z][4], dice_states[i][1]) == false){
					continue;
				}
			}
			if (z > 0){
				if (check_ch(puzzle[x][y][z - 1][0], dice_states[i][5]) == false){
					continue;
				}
			}

			is_used[j] = true;
			puzzle[x][y][z] = dice_states[i];
			if(dfs(coordinate_idx + 1)){
				return true;
			}
			is_used[j] = false;
		}
	}
	return false;
}

bool input() {
	string in, str, str2;
	int n = 0;
	for (int i = 0; i < 8; i++) {
		cin >> in;
		if (in == "0")
			return false;
		for (int j = 0; j < 6; j++) {
			if (j < 4)
				str = roll_west(in, j);
			else if (j == 4)
				str = roll_north(in, 1);
			else
				str = roll_north(in, 3);
			for (int k = 0; k < 4; k++) {
				str2 = rotate_right(str, k);
				dice_states[n++] = str2;
			}
		}
	}
	return true;
}

string roll_west(string str, int x) {
	char tmp;
	for (int i = 0; i < x; i++) {
		tmp = str[3];
		str[3] = str[0];
		str[0] = str[2];
		str[2] = str[5];
		str[5] = tmp;
	}
	return str;
}
string roll_north(string str, int x) {
	char tmp;
	for (int i = 0; i < x; i++) {
		tmp = str[5];
		str[5] = str[4];
		str[4] = str[0];
		str[0] = str[1];
		str[1] = tmp;
	}
	return str;
}
string rotate_right(string str, int x) {
	char tmp;
	for (int i = 0; i < x; i++) {
		tmp = str[1];
		str[1] = str[2];
		str[2] = str[4];
		str[4] = str[3];
		str[3] = tmp;
	}
	return str;
}
accuracy: 1
time_ms: 210
memory_kb: 3176
final_rank: 9

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

#define READ_FROM_FILE 0

#if READ_FROM_FILE
#include <fstream>
#endif

using namespace std;
string puzzle[2][2][2];
string dice_states[192];
bool is_used[8];
int n, flg;

int checkch = 'A' - 'a';

bool input();
string roll_west(string, int);
string roll_north(string, int);
string rotate_right(string, int);
void dfs(int);

inline bool check_ch(char x, char y) {
	return (x + checkch == y || x - checkch == y);
}

int main() {
#if READ_FROM_FILE
	std::ifstream ifs("test0.txt");
	std::cin.rdbuf(ifs.rdbuf());
#endif
	while (input()) {
		for (int i = 0; i < 8; i++)
			is_used[i] = false;

		flg = 0;
		dfs(0);
		if (flg)
			cout << "YES" << endl;
		else
			cout << "NO" << endl;
	}
	return 0;
}

void dfs(int coordinate_idx) {
	if(coordinate_idx == 8){
		flg = 1;
		return;
	}
	int z = coordinate_idx / 4;
	int y = (coordinate_idx % 4) / 2;
	int x = (coordinate_idx % 4) % 2;


	for (int j = 0; j < 8; j++) {
		if (is_used[j])
			continue;
		for (int i = j * 24; i < j * 24 + 24; i++) {
			if (flg)
				return;
			if (x > 0)
				if (check_ch(puzzle[x - 1][y][z][2], dice_states[i][3]) == false)
					continue;
			if (y > 0)
				if (check_ch(puzzle[x][y - 1][z][4], dice_states[i][1]) == false)
					continue;
			if (z > 0)
				if (check_ch(puzzle[x][y][z - 1][0], dice_states[i][5]) == false)
					continue;

			is_used[j] = true;
			puzzle[x][y][z] = dice_states[i];
			dfs(coordinate_idx + 1);
			is_used[j] = false;
		}
	}
}

bool input() {
	string in, str, str2;
	n = 0;
	for (int i = 0; i < 8; i++) {
		cin >> in;
		if (in == "0")
			return false;
		for (int j = 0; j < 6; j++) {
			if (j < 4)
				str = roll_west(in, j);
			else if (j == 4)
				str = roll_north(in, 1);
			else
				str = roll_north(in, 3);
			for (int k = 0; k < 4; k++) {
				str2 = rotate_right(str, k);
				dice_states[n++] = str2;
			}
		}
	}
	return true;
}

string roll_west(string str, int x) {
	char tmp;
	for (int i = 0; i < x; i++) {
		tmp = str[3];
		str[3] = str[0];
		str[0] = str[2];
		str[2] = str[5];
		str[5] = tmp;
	}
	return str;
}
string roll_north(string str, int x) {
	char tmp;
	for (int i = 0; i < x; i++) {
		tmp = str[5];
		str[5] = str[4];
		str[4] = str[0];
		str[0] = str[1];
		str[1] = tmp;
	}
	return str;
}
string rotate_right(string str, int x) {
	char tmp;
	for (int i = 0; i < x; i++) {
		tmp = str[1];
		str[1] = str[2];
		str[2] = str[4];
		str[4] = str[3];
		str[3] = tmp;
	}
	return str;
}
accuracy: 1
time_ms: 210
memory_kb: 3184
final_rank: 10

code_snippet: #include<iostream>

#define READ_FROM_FILE 0

#if READ_FROM_FILE
#include <fstream>
#endif

using namespace std;
string t[2][2][2];
string u[192];
bool used[8];
int n,flg;

int checkch='A'-'a';

bool input();
string ra(string,int);
string rb(string,int);
string rc(string,int);
void dfs(int,int,int);

inline bool check_ch(char x,char y){
  return (x+checkch==y||x-checkch==y);
}

int main(){
#if READ_FROM_FILE
	std::ifstream ifs("test0.txt");
	std::cin.rdbuf(ifs.rdbuf());
#endif
  while(input()){
    for(int i=0;i<8;i++)used[i]=false;

    flg=0;
    dfs(0,0,0);
    if(flg)cout<<"YES"<<endl;
    else cout<<"NO"<<endl;
  }
  return 0;
}

void dfs(int x,int y,int z){
  //cout<<x<<' '<<y<<' '<<z<<endl;
  if(x==2){x=0;y++;}
  if(y==2){y=0;z++;}
  if(x==0&&y==0&&z==2)flg=1;

  for(int j=0;j<8;j++){
    if(used[j])continue;
    for(int i=j*24;i<j*24+24;i++){
      if(flg)return;
      if(x>0)if(check_ch(t[x-1][y][z][2],u[i][3])==false)continue;
      if(y>0)if(check_ch(t[x][y-1][z][4],u[i][1])==false)continue;
      if(z>0)if(check_ch(t[x][y][z-1][0],u[i][5])==false)continue;

      used[j]=true;
      t[x][y][z]=u[i];
      dfs(x+1,y,z);
      used[j]=false;
    }
  }
}

bool input(){
  string in,str,str2;
  n=0;
  for(int i=0;i<8;i++){
    cin>>in;if(in=="0")return false;
    for(int j=0;j<6;j++){
      if(j<4)str=ra(in,j);
      else if(j==4)str=rb(in,1);
      else str=rb(in,3);
      for(int k=0;k<4;k++){
	str2=rc(str,k);
	u[n++]=str2;
      }
    }
  }
  return true;
}

string ra(string str,int x){
  char tmp;
  for(int i=0;i<x;i++){
    tmp=str[3];
    str[3]=str[0];
    str[0]=str[2];
    str[2]=str[5];
    str[5]=tmp;
  }
  return str;
}
string rb(string str,int x){
  char tmp;
  for(int i=0;i<x;i++){
    tmp=str[5];
    str[5]=str[4];
    str[4]=str[0];
    str[0]=str[1];
    str[1]=tmp;
  }
  return str;
}
string rc(string str,int x){
  char tmp;
  for(int i=0;i<x;i++){
    tmp=str[1];
    str[1]=str[2];
    str[2]=str[4];
    str[4]=str[3];
    str[3]=tmp;
  }
  return str;
}
accuracy: 1
time_ms: 240
memory_kb: 3092
final_rank: 2

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

void rot(vector<int> &v, int a, int b, int c, int d){
    swap(v[d], v[c]);
    swap(v[c], v[b]);
    swap(v[b], v[a]);
}
void turn(vector<int> &v, string s){
    if(s == "North") rot(v, 0, 3, 5, 1);
    if(s == "East") rot(v, 0, 2, 5, 4);
    if(s == "South") rot(v, 0, 1, 5, 3);
    if(s == "West") rot(v, 0, 4, 5, 2);
    if(s == "Right") rot(v, 1, 4, 3, 2);
    if(s == "Left") rot(v, 1, 2, 3, 4);
}
vector<vector<int>> rot24(vector<int> v){
    vector<vector<int>> res;
    for(int i=0; i<6; i++){
        for(int j=0; j<4; j++){
            res.push_back(v);
            turn(v, "Left"s);
        }
        if(i%2 == 0){
            turn(v, "North"s);
        }else{
            turn(v, "East"s);
        }
    }
    return res;
}

bool same_face(int a, int b){
    if(abs(a-b) == abs('A'-'a')) return true;
    if(a==0 or b==0) return true;
    return false;
}
bool check(vector<vector<int>> &a){
    int from[3][4] = {{0,2,4,6}, {0,1,4,5}, {0,1,2,3}};
    int diff[3] = {1,2,4};
    int face[3][2] = {{2,4}, {1,3}, {5,0}};
    for(int i=0; i<3; i++){
        for(int j=0; j<4; j++){
            if(!same_face(a[from[i][j]][face[i][0]], a[from[i][j]+diff[i]][face[i][1]])){
                return false;
            }
        }
    }
    return true;
}

bool dfs(int step, vector<bool> &used, vector<vector<int>> &state, vector<vector<vector<int>>> &dice){
    if(step == 8) return true;
    for(int i=0; i<8; i++){
        if(step==0 and i>0) break;
        if(used[i]) continue;
        used[i] = true;
        for(int j=0; j<24; j++){
            state[step] = dice[i][j];
            if(!check(state)) continue;
            bool ret = dfs(step+1, used, state, dice);
            if(ret) return true;
        }
        state[step] = vector<int>(6, 0);
        used[i] = false;
    }
    return false;
}

int main(){
    while(1){
        vector<string> s(8);
        cin >> s[0];
        if(s[0] == "0") break;

        for(int i=1; i<8; i++){
            cin >> s[i];
        }
        vector<vector<vector<int>>> dice;
        for(int i=0; i<8; i++){
            vector<int> a(6);
            for(int j=0; j<6; j++){
                a[j] = (int)s[i][j];
            }
            swap(a[3], a[4]);
            dice.push_back(rot24(a));
        }
        vector<vector<int>> state(8, vector<int>(6, 0));
        vector<bool> used(8, false);
        if(dfs(0, used, state, dice)){
            cout << "YES" << endl;
        }else{
            cout << "NO" << endl;
        }

    }
    return 0;
}
accuracy: 1
time_ms: 150
memory_kb: 3364
final_rank: 18

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define reps(i, s, n) for(ll i = (s); i < n; ++i)
#define rep(i, n) reps(i, 0, n)
#define ALL(v) v.begin(), v.end()

using ll = long long;
using vec = vector<ll>;
using mat = vector<vec>;

struct Dice{
    int l, r, u, d, f, b;
    vector<int> val;

    void vtoi(){
        u = val[0]; f = val[1];
        r = val[2]; l = val[3];
        b = val[4]; d = val[5];
    }

    void itov(){
        val = {u, f, r, l, b, d};
    }

    void init(vector<int> v){
        rep(i, 6) val[i] = v[i];
        vtoi();
    }

    Dice(vector<int> v = {}) 
    : val(6), l(0), r(0), f(0), b(0), u(0), d(0){
        if(v.size() == 6) init(v);
    }

    void RollN(){
        int temp = u;
        u = f; f = d; d = b; b = temp;
        //itov();
    }

    void RollE(){
        int temp = u;
        u = l; l = d; d = r; r = temp;
        //itov();
    }

    void RollS(){RollN(); RollN(); RollN(); }
    void RollW(){RollE(); RollE(); RollE(); }
    void RollL(){RollN(); RollE(); RollS(); }
    void RollR(){RollS(); RollE(); RollN(); }

    void print(){
        cout<<"dice\n";
        cout<<l<<' '<<r<<' '<<u<<' '<<d<<' '<<f<<' '<<b<<endl;
        rep(i, 6) cout<<val[i]<<" \n"[i == 5];
    }

};

bool judge(Dice &low, Dice &high, int i){
    if(i == 0) return low.r + high.l == 52;
    if(i == 1) return low.b + high.f == 52;
    return low.u + high.d == 52;
}

bool dfs(int n, vector<Dice> &ds){
    if(n == 8) return true;
    Dice &mine = ds[n];
    rep(_, 4){
        rep(__, 4){
            bool dfs_do = true;
            rep(i, 3){
                if((n>>i)&1){
                    int last = n ^ (1<<i);
                    if(!judge(ds[last], ds[n], i)) {
                        dfs_do = false;
                        break;
                    }
                }
            }
            if(dfs_do) if(dfs(n + 1, ds)) return true;
            mine.RollL();
        }
        mine.RollN();
    }
    rep(_, 2){
        rep(__, 4){
            bool dfs_do = true;
            rep(i, 3){
                if((n>>i)&1){
                    int last = n ^ (1<<i);
                    if(!judge(ds[last], ds[n], i)) {
                        dfs_do = false;
                        break;
                    }
                }
            }
            if(dfs_do) if(dfs(n + 1, ds)) return true;
            mine.RollE();
        }
        mine.RollN(); mine.RollN();
    }
    return false;
}

bool solve(vector<Dice> ds){
    vector<int> ord(8); 
    iota(ALL(ord), 0);
    do{
        vector<Dice> sorted(0);
        rep(i, 8) sorted.push_back(ds[ord[i]]);
        if(dfs(0, sorted)) return true;
    }while(next_permutation(ALL(ord)));
    return false;
}

int main(){
    string S;
    while(cin>>S, S[0] != '0'){
        vector<Dice> ds(8);
        rep(i, 8){
            if(i) cin>>S;
            vector<int> state(6);
            rep(j, 6){
                state[j] = S[j] - 'A';
                if(state[j] < 0 || state[j] >= 26) state[j] = 52 - (S[j] - 'a');
            }
            ds[i].init(state);
        }
        cout<<(solve(ds) ? "YES" : "NO")<<endl;
    }
}
accuracy: 1
time_ms: 3520
memory_kb: 3332
final_rank: 20

code_snippet: #include <iostream>
#include <algorithm>
#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 = unsigned;
using u64 = unsigned long long;
using namespace std;

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

int main() {
    using P = array<int, 6>;
    vector<P> ps{{0,1,2,3,4,5},{0,2,4,1,3,5},{0,3,1,4,2,5},{0,4,3,2,1,5},{1,0,3,2,5,4},{1,2,0,5,3,4},{1,3,5,0,2,4},{1,5,2,3,0,4},{2,0,1,4,5,3},{2,1,5,0,4,3},{2,4,0,5,1,3},{2,5,4,1,0,3},{3,0,4,1,5,2},{3,1,0,5,4,2},{3,4,5,0,1,2},{3,5,1,4,0,2},{4,0,2,3,5,1},{4,2,5,0,3,1},{4,3,0,5,2,1},{4,5,3,2,0,1},{5,1,3,2,4,0},{5,2,1,4,3,0},{5,3,4,1,2,0},{5,4,2,3,1,0}};
    vector<string> v(8);
    while(cin >> v[0], v[0] != "0") {
        for (int i = 1; i < 8; ++i) cin >> v[i];
        vector<vector<string>> vv(8);
        array<char, 256> p;
        for (int i = 'a'; i <= 'z'; ++i) {
            p[i] = i-'a'+'A';
            p[i-'a'+'A'] = i;
        }
        for (int i = 0; i < 8; ++i) {
            for (int j = 0; j < ps.size(); ++j) {
                string a(6, ' ');
                for (int k = 0; k < 6; ++k) {
                    a[k] = v[i][ps[j][k]];
                }
                vv[i].emplace_back(a);
            }
        }
        vector<string> ans;
        vector<int> selected(8, 0);

        auto f = [&](int cur, auto &&f) -> bool {
            if (cur == 8) return true;
            for (int i = 0; i < 8; ++i) {
                if (!selected[i]){
                    selected[i] = 1;
                    for (int j = 0; j < ps.size(); ++j) {
                        int ok = 1;
                        if(cur&1){
                            if(p[ans[cur^1][1]] != vv[i][j][4]) ok = 0;
                        }
                        if(cur & 2){
                            if(p[ans[cur^2][3]] != vv[i][j][2]) ok = 0;
                        }
                        if(cur & 4){
                            if(p[ans[cur^4][0]] != vv[i][j][5]) ok = 0;
                        }
                        if(ok){
                            ans.emplace_back(vv[i][j]);
                            if(f(cur+1, f)) return true;
                            ans.pop_back();
                        }
                    }
                    selected[i] = 0;
                }
            }
            return false;
        };
        puts(f(0, f) ? "YES" : "NO");
    }
    return 0;

}
accuracy: 1
time_ms: 510
memory_kb: 3228
final_rank: 13

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

using namespace std;

string CubeInfo[8];
pair<int, int*> Selected[8];
bool Used[8];
//top front right left back bottom
int RotateBase[24][6] = {
	{ 0,1,2,3,4,5 },{},{},{},
	{ 1,5,2,3,0,4 },{},{},{},
	{ 2,1,5,0,4,3 },{},{},{},
	{ 3,1,0,5,4,2 },{},{},{},
	{ 4,0,2,3,5,1 },{},{},{},
	{ 5,4,2,3,1,0 },{},{},{},
};


void fullRotateBase() {
	int tmp[6], num;

	for (int i = 0; i < 21; i += 4) {
		for (int j = 0; j < 6; ++j) {
			tmp[j] = RotateBase[i][j];
		}
		for (int k = 1; k < 4; ++k) {
			num = tmp[1];
			tmp[1] = tmp[3];
			tmp[3] = tmp[4];
			tmp[4] = tmp[2];
			tmp[2] = num;
			for (int j = 0; j < 6; ++j) {
				RotateBase[i + k][j] = tmp[j];
			}
		}
	}
}

bool checkSame(int dice1, int pos1, int dice2, int id, int pos2) {
	char c1, c2;

	c1 = CubeInfo[Selected[dice1].first][Selected[dice1].second[pos1]];
	c2 = CubeInfo[dice2][RotateBase[id][pos2]];

	if (abs(c1 - c2) == 32) {
		return true;
	}
	return false;
}

int solve(int currentId) {
	bool flag;

	if (currentId == 8) {
		return true;
	}

	for (int i = 0; i < 8; ++i) {
		if (Used[i]) {
			continue;
		}
		Used[i] = true;
		for (int id = 0; id < 24; ++id) {
			flag = false;
			//cout << "pos:" << currentId << " =(now_" << i + 1 << ")_pattern:" << setw(2) << id+1 << " = " << StrTmp[currentId] << endl;
			switch (currentId)
			{
			case 0:flag = true; break;
			case 1:
				if (checkSame(0, 2, i, id, 3)) {
					flag = true;
				}
				break;
			case 2:
				if (checkSame(0, 1, i, id, 4)) {
					flag = true;
				}
				break;
			case 3:
				if (checkSame(1, 1, i, id, 4) && checkSame(2, 2, i, id, 3)) {
					flag = true;
				}
				break;
			case 4:
				if (checkSame(0, 5, i, id, 0)) {
					flag = true;
				}
				break;
			case 5:
				if (checkSame(1, 5, i, id, 0) && checkSame(4, 2, i, id, 3)) {
					flag = true;
				}
				break;
			case 6:
				if (checkSame(2, 5, i, id, 0) && checkSame(4, 1, i, id, 4)) {
					flag = true;
				}
				break;
			case 7:
				if (checkSame(3, 5, i, id, 0) && checkSame(5, 1, i, id, 4) && checkSame(6, 2, i, id, 3)) {
					flag = true;
				}
				break;
			default:
				break;
			}
			Selected[currentId] = make_pair(i, RotateBase[id]);
			if (flag && solve(currentId + 1)) {
				return true;
			}
		}
		Used[i] = false;
	}
	return false;
}

int main() {
	int cnt;
	string str;

	fullRotateBase();
	cnt = 0;
	while (cin >> str, str[0] != '0') {
		CubeInfo[cnt] = str;
		++cnt;
		while (cnt != 8) {
			cin >> str;
			CubeInfo[cnt] = str;
			++cnt;
		}
		//show:
		/*for (int i = 0; i < cnt; ++i) {
		cout << CubeInfo[i] << endl;
		}*/
		memset(Used, false, sizeof(Used));
		memset(Selected, 0, sizeof(Selected));
		if (solve(0)) {
			cout << "YES" << endl;
		}
		else {
			cout << "NO" << endl;
		}
		cnt = 0;
	}

	return 0;
}
accuracy: 1
time_ms: 340
memory_kb: 3140
final_rank: 6

code_snippet: #include<iostream>
using namespace std;
bool used[8];
string s[8];
int A[8];
bool ok(char a,char b){return (a^b)==32;}
bool dfs(int id)
{
	if(id==8)return true;
	for(int i=0;i<8;i++)
	{
		if(used[i])continue;
		used[i]=true;
		A[id]=i;
		for(int r=0;r<8;r++)
		{
			for(int rr=0;rr<4;rr++)
			{
				bool check=true;
				if(id==0);
				else if(id==1)check=ok(s[A[0]][4],s[i][1]);
				else if(id==2)check=ok(s[A[0]][2],s[i][3]);
				else if(id==3)check=ok(s[A[1]][2],s[i][3])&&ok(s[A[2]][4],s[i][1]);
				else if(id==4)check=ok(s[A[0]][5],s[i][0]);
				else if(id==5)check=ok(s[A[1]][5],s[i][0])&&ok(s[A[4]][4],s[i][1]);
				else if(id==6)check=ok(s[A[2]][5],s[i][0])&&ok(s[A[4]][2],s[i][3]);
				else if(id==7)check=ok(s[A[3]][5],s[i][0])&&ok(s[A[5]][2],s[i][3])&&ok(s[A[6]][4],s[i][1]);
				if(check)
				{
					if(dfs(id+1))return true;
				}
				char tmp=s[i][1];
				s[i][1]=s[i][2];
				s[i][2]=s[i][4];
				s[i][4]=s[i][3];
				s[i][3]=tmp;
			}
			if(r<4)
			{
				char tmp=s[i][0];
				s[i][0]=s[i][1];
				s[i][1]=s[i][5];
				s[i][5]=s[i][4];
				s[i][4]=tmp;
			}
			else
			{
				char tmp=s[i][0];
				s[i][0]=s[i][2];
				s[i][2]=s[i][5];
				s[i][5]=s[i][3];
				s[i][3]=tmp;
			}
		}
		used[i]=false;
		A[id]=0;
		if(id==0)return false;
	}
	return false;
}
main()
{
	while(cin>>s[0])
	{
		if(s[0]=="0")break;
		for(int i=1;i<8;i++)cin>>s[i];
		for(int i=0;i<8;i++)used[i]=false;
		cout<<(dfs(0)?"YES":"NO")<<endl;
	}
}
accuracy: 1
time_ms: 290
memory_kb: 3040
final_rank: 1

code_snippet: #include<iostream>
using namespace std;
bool used[8];
string s[8];
int A[8];
bool ok(char a,char b){return (a^b)==32;}
bool dfs(int id)
{
	if(id==8)return true;
	for(int i=0;i<8;i++)
	{
		if(used[i])continue;
		used[i]=true;
		A[id]=i;
		for(int r=0;r<8;r++)
		{
			for(int rr=0;rr<4;rr++)
			{
				bool check=true;
				if(id==0);
				else if(id==1)check=ok(s[A[0]][4],s[i][1]);
				else if(id==2)check=ok(s[A[0]][2],s[i][3]);
				else if(id==3)check=ok(s[A[1]][2],s[i][3])&&ok(s[A[2]][4],s[i][1]);
				else if(id==4)check=ok(s[A[0]][5],s[i][0]);
				else if(id==5)check=ok(s[A[1]][5],s[i][0])&&ok(s[A[4]][4],s[i][1]);
				else if(id==6)check=ok(s[A[2]][5],s[i][0])&&ok(s[A[4]][2],s[i][3]);
				else if(id==7)check=ok(s[A[3]][5],s[i][0])&&ok(s[A[5]][2],s[i][3])&&ok(s[A[6]][4],s[i][1]);
				if(check)
				{
					if(dfs(id+1))return true;
				}
				char tmp=s[i][1];
				s[i][1]=s[i][2];
				s[i][2]=s[i][4];
				s[i][4]=s[i][3];
				s[i][3]=tmp;
			}
			if(r<4)
			{
				char tmp=s[i][0];
				s[i][0]=s[i][1];
				s[i][1]=s[i][5];
				s[i][5]=s[i][4];
				s[i][4]=tmp;
			}
			else
			{
				char tmp=s[i][0];
				s[i][0]=s[i][2];
				s[i][2]=s[i][5];
				s[i][5]=s[i][3];
				s[i][3]=tmp;
			}
		}
		used[i]=false;
		A[id]=0;
	}
	return false;
}
main()
{
	while(cin>>s[0])
	{
		if(s[0]=="0")break;
		for(int i=1;i<8;i++)cin>>s[i];
		for(int i=0;i<8;i++)used[i]=false;
		cout<<(dfs(0)?"YES":"NO")<<endl;
	}
}
accuracy: 1
time_ms: 1730
memory_kb: 3112
final_rank: 14

code_snippet: #include <stdio.h>
#include <cmath>
#include <algorithm>
#include <cfloat>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <iostream>
#include <set>
#include <map>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

class Dice{
public:



	void setPos(int num){

		for(int i = 0; i < 6; i++)work[i] = words[i];

		switch(num){
		case 0:
			setWords(work[4],work[0],work[2],work[3],work[5],work[1]);
			break;
		case 1:
			setWords(work[5],work[4],work[2],work[3],work[1],work[0]);
			break;
		case 2:
			setWords(work[1],work[5],work[2],work[3],work[0],work[4]);
			break;
		case 3:
			setWords(work[0],work[1],work[2],work[3],work[4],work[5]);
			break;
		case 4:
			setWords(work[2],work[1],work[5],work[0],work[4],work[3]);
			break;
		case 5:
			setWords(work[4],work[2],work[5],work[0],work[3],work[1]);
			break;
		case 6:
			setWords(work[3],work[4],work[5],work[0],work[1],work[2]);
			break;
		case 7:
			setWords(work[1],work[3],work[5],work[0],work[2],work[4]);
			break;
		case 8:
			setWords(work[5],work[3],work[4],work[1],work[2],work[0]);
			break;
		case 9:
			setWords(work[2],work[5],work[4],work[1],work[0],work[3]);
			break;
		case 10:
			setWords(work[0],work[2],work[4],work[1],work[3],work[5]);
			break;
		case 11:
			setWords(work[3],work[0],work[4],work[1],work[5],work[2]);
			break;
		case 12:
			setWords(work[1],work[0],work[3],work[2],work[5],work[4]);
			break;
		case 13:
			setWords(work[5],work[1],work[3],work[2],work[4],work[0]);
			break;
		case 14:
			setWords(work[4],work[5],work[3],work[2],work[0],work[1]);
			break;
		case 15:
			setWords(work[0],work[4],work[3],work[2],work[1],work[5]);
			break;
		case 16:
			setWords(work[2],work[4],work[0],work[5],work[1],work[3]);
			break;
		case 17:
			setWords(work[1],work[2],work[0],work[5],work[3],work[4]);
			break;
		case 18:
			setWords(work[3],work[1],work[0],work[5],work[4],work[2]);
			break;
		case 19:
			setWords(work[4],work[3],work[0],work[5],work[2],work[1]);
			break;
		case 20:
			setWords(work[0],work[3],work[1],work[4],work[2],work[5]);
			break;
		case 21:
			setWords(work[2],work[0],work[1],work[4],work[5],work[3]);
			break;
		case 22:
			setWords(work[5],work[2],work[1],work[4],work[3],work[0]);
			break;
		case 23:
			setWords(work[3],work[5],work[1],work[4],work[0],work[2]);
			break;
		}
	}


	char getTop(){
		return words[0];
	}

	char getEast(){
		return words[2];
	}

	char getNorth(){
		return words[1];
	}

	char getWest(){
		return words[3];
	}

	char getBottom(){
		return words[5];
	}

	char getSouth(){
		return words[4];
	}

	void setWords(char w0,char w1,char w2,char w3,char w4,char w5){
		words[0] = w0;
		words[1] = w1;
		words[2] = w2;
		words[3] = w3;
		words[4] = w4;
		words[5] = w5;
	}

	void setFirst(char c1,char c2,char c3,char c4,char c5,char c6){
		words[0] = c1;
		words[1] = c5;
		words[2] = c3;
		words[3] = c4;
		words[4] = c2;
		words[5] = c6;
	}

	void set(Dice arg){
		for(int i = 0; i < 6; i++){
			words[i] = arg.words[i];
		}
	}

	char words[6];
	char work[6];
};

struct Puzzle{
	Dice dice[2][2][2];
	int num;
};

char buf[8][7];
int calc_array[5040][7];
int array_index = 0;

bool clearFLG;


void copyPuzzle(Puzzle& to, Puzzle from){

	for(int h = 0; h < 2; h++){
		for(int row = 0; row < 2; row++){
			for(int col = 0; col < 2; col++){
				to.dice[h][row][col].set(from.dice[h][row][col]);
			}
		}
	}
}

bool is_adj(char left,char right){

	if(left > right){
		swap(left,right);
	}

	return left+32 == right;
}

void puzzle_calc(Puzzle puzzle){

	if(puzzle.num == 8){
		clearFLG = true;
		return;
	}

	switch(puzzle.num){
	case 1:

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

			Dice tmp_dice;
			tmp_dice.set(puzzle.dice[1][0][0]);
			tmp_dice.setPos(i);

			if(is_adj(puzzle.dice[1][1][0].getNorth(),tmp_dice.getSouth()) == false)continue;

			Puzzle new_puzzle;
			copyPuzzle(new_puzzle,puzzle);

			new_puzzle.dice[1][0][0].setPos(i);

			new_puzzle.num = puzzle.num+1;
			puzzle_calc(new_puzzle);
		}
		break;
	case 2:
		for(int i = 0; i <= 23; i++){
			Dice tmp_dice;
			tmp_dice.set(puzzle.dice[1][0][1]);
			tmp_dice.setPos(i);

			if(is_adj(puzzle.dice[1][0][0].getEast(),tmp_dice.getWest()) == false)continue;

			Puzzle new_puzzle;
			copyPuzzle(new_puzzle,puzzle);

			new_puzzle.dice[1][0][1].setPos(i);

			new_puzzle.num = puzzle.num+1;
			puzzle_calc(new_puzzle);
		}
		break;
	case 3:
		for(int i = 0; i <= 23; i++){
			Dice tmp_dice;
			tmp_dice.set(puzzle.dice[1][1][1]);
			tmp_dice.setPos(i);

			if(is_adj(puzzle.dice[1][0][1].getSouth(),tmp_dice.getNorth()) == false ||
					is_adj(puzzle.dice[1][1][0].getEast(),tmp_dice.getWest()) == false)continue;

			Puzzle new_puzzle;
			copyPuzzle(new_puzzle,puzzle);

			new_puzzle.dice[1][1][1].setPos(i);

			new_puzzle.num = puzzle.num+1;
			puzzle_calc(new_puzzle);
		}
		break;
	case 4:
		for(int i = 0; i <= 23; i++){

			Dice tmp_dice;
			tmp_dice.set(puzzle.dice[0][1][0]);
			tmp_dice.setPos(i);

			if(is_adj(puzzle.dice[1][1][0].getBottom(),tmp_dice.getTop()) == false)continue;

			Puzzle new_puzzle;
			copyPuzzle(new_puzzle,puzzle);

			new_puzzle.dice[0][1][0].setPos(i);

			new_puzzle.num = puzzle.num+1;
			puzzle_calc(new_puzzle);
		}
		break;
	case 5:
		for(int i = 0; i <= 23; i++){

			Dice tmp_dice;
			tmp_dice.set(puzzle.dice[0][0][0]);
			tmp_dice.setPos(i);

			if(is_adj(puzzle.dice[1][0][0].getBottom(),tmp_dice.getTop()) == false ||
					is_adj(puzzle.dice[0][1][0].getNorth(),tmp_dice.getSouth()) == false)continue;

			Puzzle new_puzzle;
			copyPuzzle(new_puzzle,puzzle);

			new_puzzle.dice[0][0][0].setPos(i);

			new_puzzle.num = puzzle.num+1;
			puzzle_calc(new_puzzle);
		}
		break;
	case 6:
		for(int i = 0; i <= 23; i++){

			Dice tmp_dice;
			tmp_dice.set(puzzle.dice[0][0][1]);
			tmp_dice.setPos(i);

			if(is_adj(puzzle.dice[1][0][1].getBottom(),tmp_dice.getTop()) == false ||
					is_adj(puzzle.dice[0][0][0].getEast(),tmp_dice.getWest()) == false)continue;

			Puzzle new_puzzle;
			copyPuzzle(new_puzzle,puzzle);

			new_puzzle.dice[0][0][1].setPos(i);

			new_puzzle.num = puzzle.num+1;
			puzzle_calc(new_puzzle);
		}
		break;
	case 7:
		for(int i = 0; i <= 23; i++){

			Dice tmp_dice;
			tmp_dice.set(puzzle.dice[0][1][1]);
			tmp_dice.setPos(i);

			if(is_adj(puzzle.dice[1][1][1].getBottom(),tmp_dice.getTop()) == false ||
					is_adj(puzzle.dice[0][1][0].getEast(),tmp_dice.getWest()) == false ||
					is_adj(puzzle.dice[0][0][1].getSouth(),tmp_dice.getNorth()) == false)continue;

			Puzzle new_puzzle;
			copyPuzzle(new_puzzle,puzzle);

			new_puzzle.dice[0][1][1].setPos(i);

			new_puzzle.num = puzzle.num+1;
			puzzle_calc(new_puzzle);
		}
		break;
	}
}

void func(){

	for(int i = 1; i <= 7; i++){
		scanf("%s",buf[i]);
	}

	clearFLG = false;
	int calc_index = 0;

	while(clearFLG == false && calc_index < 5040){

		Puzzle new_puzzle;
		new_puzzle.dice[1][1][0].setFirst(buf[0][0],buf[0][1],buf[0][2],buf[0][3],buf[0][4],buf[0][5]); //?????????S1?????????(1,1,0)????¨????

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

			int num = calc_array[calc_index][i];

			switch(i){
			case 0:
				new_puzzle.dice[1][0][0].setFirst(buf[num][0],buf[num][1],buf[num][2],buf[num][3],buf[num][4],buf[num][5]);
				break;
			case 1:
				new_puzzle.dice[1][0][1].setFirst(buf[num][0],buf[num][1],buf[num][2],buf[num][3],buf[num][4],buf[num][5]);
				break;
			case 2:
				new_puzzle.dice[1][1][1].setFirst(buf[num][0],buf[num][1],buf[num][2],buf[num][3],buf[num][4],buf[num][5]);
				break;
			case 3:
				new_puzzle.dice[0][1][0].setFirst(buf[num][0],buf[num][1],buf[num][2],buf[num][3],buf[num][4],buf[num][5]);
				break;
			case 4:
				new_puzzle.dice[0][0][0].setFirst(buf[num][0],buf[num][1],buf[num][2],buf[num][3],buf[num][4],buf[num][5]);
				break;
			case 5:
				new_puzzle.dice[0][0][1].setFirst(buf[num][0],buf[num][1],buf[num][2],buf[num][3],buf[num][4],buf[num][5]);
				break;
			case 6:
				new_puzzle.dice[0][1][1].setFirst(buf[num][0],buf[num][1],buf[num][2],buf[num][3],buf[num][4],buf[num][5]);
				break;
			}
		}

		for(int i = 0; i <= 23; i++){
			Puzzle next_puzzle;
			copyPuzzle(next_puzzle,new_puzzle);
			next_puzzle.dice[1][1][0].setPos(i);
			next_puzzle.num = 1;
			puzzle_calc(next_puzzle);
		}

		calc_index++;
	}

	if(clearFLG){
		printf("YES\n");
	}else{
		printf("NO\n");
	}
}


void recursive(bool check[8],int jyunretsu[7],int num){

	if(num == 7){
		for(int k = 0; k < 7; k++){
			calc_array[array_index][k] = jyunretsu[k];
		}
		array_index++;
		return;
	}

	for(int i = 1; i <= 7; i++){
		if(check[i] == false){
			int next_jyunretsu[7];
			for(int k = 0; k < num; k++)next_jyunretsu[k] = jyunretsu[k];
			next_jyunretsu[num] = i;

			bool next_check[8];
			for(int k = 1; k <= 7; k++)next_check[k] = check[k];
			next_check[i] = true;

			recursive(next_check,next_jyunretsu,num+1);
		}
	}
}

int main(){

	bool check[8];
	for(int i = 1; i <= 7; i++){
		check[i] = false;
	}
	int jyunretsu[7];

	recursive(check,jyunretsu,0);

	while(true){
		scanf("%s",buf[0]);
		if(buf[0][0] == '0')break;

		func();
	}
	return 0;
}
accuracy: 1
time_ms: 2270
memory_kb: 3288
final_rank: 19