显示原始代码
#include <bits/stdc++.h>
#define db double
#define ll long long
#define int ll
#define vi vector<int>
#define vii vector<vi>
#define pii pair<int, int>
#define vp vector<pii>
#define vip vector<vp>
#define mkp make_pair
#define pb push_back
#define Case(x) cout << "Case #" << x << ": "
using namespace std;
const int INF = 0x3f3f3f3f;
const int P = 998244353;
vi card, cnt;
map<string, int> hsh;
bool dfs(int p = 0, int tot = 0) {
if (tot == 12)
return 1;
while (cnt[p] == 0 && p < 38) p++;
if (p + 2 < 30 && cnt[p] && cnt[p + 1] && cnt[p + 2]) {
for (int j = 0; j < 3; j++) cnt[p + j]--;
if (dfs(p, tot + 3))
return 1;
for (int j = 0; j < 3; j++) cnt[p + j]++;
}
if (cnt[p] >= 3) {
cnt[p] -= 3;
if (dfs(p, tot + 3))
return 1;
cnt[p] += 3;
}
return 0;
}
bool chk(vi &a) {
sort(a.begin(), a.end());
vi b = a;
for (int i = 0; i < 13; i++) {
if (b[i] == b[i + 1]) {
b.erase(b.erase(b.begin() + i));
cnt = vi(38);
for (int j : b) cnt[j]++;
if (dfs())
return 1;
b = a;
i++;
}
}
return 0;
}
void findcard(vi &win, vi &c) {
vi d = c;
for (int i : card) {
d.pb(i);
if (chk(d))
win.pb(i);
d = c;
}
}
string word[4] = { "m", "s", "p", "z" };
string bck(int x) { return to_string(x % 10) + word[x / 10]; }
signed main() {
#ifdef _DEBUG
#endif
ios::sync_with_stdio(0);
cin.tie(0);
for (int i = 0; i < 4; i++) {
for (int j = 1; j < 10; j++) {
if (i < 3 || j < 8) {
card.pb(i * 10 + j);
hsh[to_string(j) + word[i]] = i * 10 + j;
}
}
}
int T;
cin >> T;
while (T--) {
string s;
cin >> s;
vi a;
for (int i = 0; i < 28; i += 2) a.pb(hsh[s.substr(i, 2)]);
sort(a.begin(), a.end());
if (chk(a)) {
cout << "Tsumo!" << '\n';
continue;
}
vii win(14);
vi c = a;
int ans = 0;
for (int i = 0; i < 14; i++) {
if (i > 0 && a[i] == a[i - 1])
continue;
c.erase(c.begin() + i);
findcard(win[i], c);
if (!win[i].empty())
ans++;
c = a;
}
cout << ans << '\n';
for (int i = 0; i < 14; i++) {
if (!win[i].empty()) {
cout << bck(a[i]) << ' ';
for (auto j : win[i]) cout << bck(j) << ' ';
cout << '\n';
}
}
}
return 0;
}