思路:
预处理所有牌,对已有的牌标记。枚举可以组成一组的两张牌(此时第三张牌已被固定),判断手上是否有第三张牌
Code:
#pragma GCC optimize(3) #pragma GCC optimize(2) #include <map> #include <set> // #include <array> #include <queue> #include <stack> #include <vector> #include <cstdio> #include <cstring> #include <sstream> #include <iostream> #include <stdlib.h> #include <algorithm> // #include <unordered_map> using namespace std; typedef long long ll; typedef pair<int, int> PII; #define Time (double)clock() / CLOCKS_PER_SEC #define sd(a) scanf("%d", &a) #define sdd(a, b) scanf("%d%d", &a, &b) #define slld(a) scanf("%lld", &a) #define slldd(a, b) scanf("%lld%lld", &a, &b) const int N = 300 + 20; const int M = 1e6 + 20; const int mod = 1e9 + 7; struct node { int a, b, c, d; } s[N]; int n; char t[N], p[10]; map<int, int> mp; void save(int j) { int i = 0, cnt = 0; int m = strlen(t); while (t[i] != ']') { if (t[i] == '[') { i++; continue; } p[cnt++] = t[i]; i++; } // p[cnt] = ' '; // cout << p << endl; if (p[0] == 'o') s[j].a = 1; else if (p[0] == 't' && p[1] == 'w') s[j].a = 2; else if (p[0] == '*') s[j].a = 4; else s[j].a = 3; i++; cnt = 0; while (t[i] != ']') { if (t[i] == '[') { i++; continue; } p[cnt++] = t[i]; i++; } // p[cnt] = ' '; // cout << p << endl; if (p[0] == 'd') s[j].b = 1; else if (p[0] == 's') s[j].b = 2; else if (p[0] == '*') s[j].b = 4; else s[j].b = 3; i++; cnt = 0; while (t[i] != ']') { if (t[i] == '[') { i++; continue; } p[cnt++] = t[i]; i++; } // p[cnt] = ' '; // cout << p << endl; if (p[0] == 's' && p[1] == 'o') s[j].c = 1; else if (p[0] == 's' && p[1] == 't') s[j].c = 2; else if (p[0] == '*') s[j].c = 4; else s[j].c = 3; i++; cnt = 0; while (t[i] != ']') { if (t[i] == '[') { i++; continue; } p[cnt++] = t[i]; i++; } // p[cnt] = ' '; // cout << p << endl; if (p[0] == 'r') s[j].d = 1; else if (p[0] == 'g') s[j].d = 2; else if (p[0] == '*') s[j].d = 4; else s[j].d = 3; } void solve() { sd(n); mp.clear(); for (int i = 0; i < n; i++) { scanf("%s", t); save(i); vector<int> aa, bb, cc, dd; if (s[i].a == 4) { for (int j = 1; j <= 3; j++) { aa.push_back(j); } } else { aa.push_back(s[i].a); } if (s[i].b == 4) { for (int j = 1; j <= 3; j++) { bb.push_back(j); } } else { bb.push_back(s[i].b); } if (s[i].c == 4) { for (int j = 1; j <= 3; j++) { cc.push_back(j); } } else { cc.push_back(s[i].c); } if (s[i].d == 4) { for (int j = 1; j <= 3; j++) { dd.push_back(j); } } else { dd.push_back(s[i].d); } for (auto A : aa) { for (auto B : bb) { for (auto C : cc) { for (auto D : dd) { mp[A * 1000 + B * 100 + C * 10 + D] = i + 1; } } } } } int k; vector<int> success; for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { k = 0; vector<int> aa, bb, cc, dd; if (s[i].a == 4 || s[j].a == 4) { aa.push_back(1); aa.push_back(2); aa.push_back(3); } else if (s[i].a == s[j].a) { aa.push_back(s[i].a); } else { aa.push_back(6 - s[i].a - s[j].a); } if (s[i].b == 4 || s[j].b == 4) { bb.push_back(1); bb.push_back(2); bb.push_back(3); } else if (s[i].b == s[j].b) { bb.push_back(s[i].b); } else { bb.push_back(6 - s[i].b - s[j].b); } if (s[i].c == 4 || s[j].c == 4) { cc.push_back(1); cc.push_back(2); cc.push_back(3); } else if (s[i].c == s[j].c) { cc.push_back(s[i].c); } else { cc.push_back(6 - s[i].c - s[j].c); } if (s[i].d == 4 || s[j].d == 4) { dd.push_back(1); dd.push_back(2); dd.push_back(3); } else if (s[i].d == s[j].d) { dd.push_back(s[i].d); } else { dd.push_back(6 - s[i].d - s[j].d); } for (auto A : aa) { for (auto B : bb) { for (auto C : cc) { for (auto D : dd) { k = A * 1000 + B * 100 + C * 10 + D; if (mp[k] && mp[k] != i + 1 && mp[k] != j + 1) { success.push_back(i + 1); success.push_back(j + 1); success.push_back(mp[k]); break; } } if (success.size()) break; } if (success.size()) break; } if (success.size()) break; } if (success.size()) break; } if (success.size()) break; } if (success.empty()) { printf(" -1 "); } else { for (auto x : success) { printf(" %d", x); } printf(" "); // success.clear(); } } int main() { #ifdef ONLINE_JUDGE #else freopen("/home/jungu/code/in.txt", "r", stdin); // freopen("/home/jungu/code/out.txt", "w", stdout); // freopen("/home/jungu/code/out.txt","w",stdout); #endif // ios::sync_with_stdio(false); cin.tie(0), cout.tie(0); int T = 1; sd(T); int cas = 1; while (T--) { printf("Case #%d:", cas++); solve(); } return 0; }