#include #include #include #include #include using namespace std; bool doit(const vector& poss, const vector>& restrict, vector& cur, vector>& s) { int i; for (i = 0; i < 26; i++) if (cur[i] == -1 && (poss[i] == 1 || poss[i] == 2 || poss[i] == 4)) goto found; for (i = 0; i < 26; i++) if (cur[i] == -1 && poss[i] > 0 && poss[i] < 7) goto found; for (i = 0; i < 26; i++) if (cur[i] == -1 && poss[i] > 0) goto found; return true; found: for (cur[i] = 0; cur[i] <= 2; cur[i]++) if (poss[i] & (1< poss2 = poss; for (auto j : restrict[i]) { poss2[j] &= ~(1<> N) { vector W(N); vector cur(26, -1), poss(26); vector> s(3); vector> restrict(26); for (auto& w : W) cin >> w; for (auto const& w : W) for (auto ch : w) poss[ch-'a'] = 7; for (auto const& w : W) for (int i = 0; i < 3; i++) for (int j = 0; j < 3; j++) if (i != j) { if (w[i] == w[j]) goto fail; restrict[w[i]-'a'].insert(w[j]-'a'); } if (!doit(poss, restrict, cur, s)) goto fail; for (int si = 0; si < 3; si++) { while (s[si].size() < 6) { for (int i = 0; ; i++) if (cur[i] == -1) { cur[i] = si; s[si].push_back(i); break; } } for (auto i : s[si]) cout << char(i+'a'); if (si < 2) cout << ' '; else cout << endl; } continue; fail: cout << 0 << endl; } }