#include #include using namespace std; class Card { public: string array[15] = { "JOKER","joker","2","A","K","Q","J","10","9","8","7","6","5","4","3"}; int card_number(int* a) { int number = 0; for (int i = 0; i < 15; i++) number += a[i]; return number; } int decide(string s) { for (int i = 0; i < 15; i++) { if (s == array[i]) return i; } } bool one(int* a, int* b) { if (card_number(a) == 1) { for (int i = 0; i < 15; i++) if (a[i]) { *b = i; return true; } } return false; } bool pairs(int* a, int* b) { if (card_number(a) == 2) { for (int i = 2; i < 15; i++) if (a[i] == 2) { *b = i; return true; } } return false; } bool three(int* a, int* three_card, int *bring_num) { if (card_number(a) >= 3 && card_number(a) <= 5) { for (int i = 2; i < 15; i++) { if (a[i] == 3) { *three_card = i; *bring_num = card_number(a) - 3; return true; } } } return false; } bool continuous_pairs(int* a, int *start, int *length){//连对 int count = 0,i; for (i = 2; i < 15; i++) { if (a[i] == 2) { count++; if (i==14||a[i + 1] != 2) break; } } *start = i; *length = count; if (count > 2) return true; else return false; } bool continuous_one(int* a, int* start, int* length) { int count = 0,i; for (i = 2; i < 15; i++) {//从2开始 if (a[i] == 1) { count++; if (i==14||a[i + 1] != 1) break; } } *start = i; *length = count; if (count > 4) return true; else return false; } bool four_and_two(int* a, int* four_card, int* bring_2or4) { if (card_number(a) == 6 || card_number(a) == 8) { for (int i = 0; i < 15; i++) { if (a[i] == 4) { *four_card = i; *bring_2or4 = card_number(a) - 4; return true; } } } return false; } bool plane(int* a, int* start, int* num, int* type) {//num=2 or 3,type=2 or 3 or 4 or 6 if (card_number(a) == 8) { for (int i = 2; i < 14; i++) { if (a[i] == 3 && a[i + 1] == 3) { *start = i + 1; *num = 2; *type = 2; return true; } } } if (card_number(a) == 10) { for (int i = 2; i < 14; i++) { if (a[i] == 3 && a[i + 1] == 3) { *start = i + 1; *num = 2; *type = 4; return true; } } } if (card_number(a) == 12) { for (int i = 2; i < 13; i++) { if (a[i] == 3 && a[i + 1] == 3 && a[i + 2] == 3) { *start = i + 2; *num = 3; *type = 3; return true; } } } if (card_number(a) == 15) { for (int i = 2; i < 13; i++) { if (a[i] == 3 && a[i + 1] == 3 && a[i + 2] == 3) { *start = i + 2; *num = 3; *type = 6; return true; } } } return false; } bool bomb(int* a, int* b) { for (int i = 0; i < 15; i++) { if (a[i] == 4 && card_number(a) == 4) { *b = i; return true; } } return false; } bool king_bomb(int* a) { if (a[0] == 1 && a[1] == 1) return true; else return false; } int* judge_one(int* a, int b) { for (int i = b - 1; i >= 0; i--) { if (a[i]) { a[i]--; cout << array[i]; return a; } } return judge_bomb(a,15); } int* judge_pairs(int* a, int b) { for (int i = b - 1; i >= 0; i--) { if (a[i] >= 2) { a[i] -= 2; cout << array[i] << " " << array[i]; return a; } } return judge_bomb(a,15); } int* judge_three(int* a, int three_card,int bring_num) { for (int i = three_card - 1; i >= 0; i--) { if (a[i] >= 3&&card_number(a)>=3+bring_num) { a[i] -= 3; for (int k = 0; k < 3; k++) cout << array[i] <<" "; if (bring_num == 2) { int temp[2],t=0; for (int j = 14; j >= 0; j--) { if (bring_num == 0) { cout << array[temp[1]] << " " << array[temp[0]]; return a; } if (a[j]) { temp[t++] = j; a[j++]--; bring_num--; } } } if (bring_num == 0) return a; for (int j = 14; j >= 0; j--) {//bring_num==1 if (a[j]) { cout << array[j]; a[j]--; bring_num--; return a; } } } } return judge_bomb(a,15); } int* judge_continuous_pairs(int* a, int start, int length) { if (card_number(a) < 2 * length) { return judge_bomb(a,15); } for (int i = start - 1; i >= 2; i--) { if (a[i] >= 2) { int k; for (k = i - 1; k >= 2; k--) { if (a[k] < 2) break; } if (i - k >= length) { for (int j = i - length + 1;j<=i; j++) { a[j] -= 2; cout << array[j] << " " << array[j] << " "; } return a; } } } return judge_bomb(a,15); } int* judge_continuous_one(int* a, int start, int length) { if (card_number(a) < length) { return judge_bomb(a, 15); } for (int i = start - 1; i >= 2; i--) { if (a[i] >= 1) { int k; for (k = i - 1; k >= 2; k--) { if (a[k]==0) break; } if (i - k >= length) { for (int j = i - length + 1; j <= i; j++) { a[j] -= 1; cout << array[j] << " "; } return a; } } } return judge_bomb(a, 15); } int* judge_four_and_two(int* a, int four_card, int bring_2or4) { if (card_number(a) < bring_2or4 + 4) return judge_bomb(a, 15); for (int i = four_card - 1; i >= 2; i--) { if (a[i] == 4) { if (bring_2or4 == 2) { a[i] = 0; for (int j = 0; j < 4; j++) cout << array[i] << " "; int temp[2], t = 0; for (int j = 14; j >= 0; j--) { if (a[j]) { if (bring_2or4 == 0) { cout << array[temp[1]] << " " << array[temp[0]]; return a; } temp[t++]=j; a[j++]--; bring_2or4--; } } } if (bring_2or4 == 4) { for (int j = 14; j >= 2; j--) { if (a[j] >= 2) { for (int k = j - 1; k >= 2; k--) { if (a[k] >= 2) { a[i] = 0; for (int j = 0; j < 4; j++) cout << array[i] << " "; cout << array[k] << " " << array[k] << " "; cout << array[j] << " " << array[j] << " "; a[j] -= 2; a[k] -= 2; return a; } } } } } } } return judge_bomb(a, 15); } int* judge_plane(int* a, int start, int num, int type) { if (card_number(a) < num * 3 + type) return judge_bomb(a,15); if (num == 2 && type == 2) { for (int i = start - 1; i >= 3; i--) { if (a[i] >= 3 && a[i - 1] >= 3) { a[i] -=3; a[i - 1] -=3; for (int k = 0; k < 3; k++) cout << array[i-1] << " "; for (int k = 0; k < 3; k++) cout << array[i] << " "; int temp[2], t=0; for (int j = 14; j >= 0; j--) { if (type == 0) { cout << array[temp[1]] << " " << array[temp[0]]; return a; } if (a[j]) { temp[t++]=j; a[j++]--; type--; } } } } } if (num == 2 && type == 4) { for (int i = start - 1; i >= 3; i--) { if (a[i] >= 3 && a[i - 1] >= 3) { int count = 0,temp[2]; for (int j = 14; j >= 0; j--) { if (count == 2) { a[i] -= 3; a[i - 1] -= 3; a[temp[1]] -= 2; a[temp[0]] -= 2; for(int k=0;k<3;k++) cout << array[i-1] << " "; for(int k=0;k<3;k++) cout << array[i] << " "; cout << array[temp[1]] << " " << array[temp[1]] << " " << array[temp[0]] << " " << array[temp[0]]; return a; } if (j != i&&j != i - 1 && a[j] >= 2) { temp[count] = j; count++; } } } } } if (num == 3 && type == 3) { for (int i = start - 1; i >= 4; i--) { if (a[i] >= 3 && a[i - 1] >= 3 && a[i - 2] >= 3) { a[i] -=3; a[i - 1] -=3; a[i - 2] -=3; for (int k = 2; k >= 0; k--) { for (int l = 0; l < 3; l++) { cout << array[i-k] << " "; } } int temp[3], t=0; for (int j = 14; j >= 0; j--) { if (type == 0) { for (int k = 2; k >= 0; k--) { cout << array[temp[k]] << " "; } return a; } if (a[j]) { temp[t++]=j; a[j++]--; type--; } } } } } if (num == 3 && type == 6) { for (int i = start - 1; i >= 3; i--) { if (a[i] >= 3 && a[i - 1] >= 3 && a[i - 2] >= 3) { int count = 0, temp[3]; for (int j = 14; j >= 0; j--) { if (count == 3) { for (int k = 0; k < 3; k++) { a[i - k] -= 3; a[temp[k]] -= 2; } for (int k = 0; k<3; k++) cout << array[i-2] << " "; for (int k = 0; k<3; k++) cout << array[i-1] << " "; for (int k = 0; k<3; k++) cout << array[i] << " "; for (int k = 2; k >= 0; k--) { cout << array[temp[k]] << " " << array[temp[k]] << " "; } return a; } if (j != i&&j != i - 1&&j!=i-1 && a[j] >= 2) { temp[count] = j; count++; } } } } } return judge_bomb(a, 15); } int* judge_bomb(int* a ,int b) { for (int i = b - 1; i >= 2; i--) { if (a[i] == 4) { a[i] = 0; for (int j = 0; j < 4; j++) cout << array[i] << " "; return a; } } return judge_king_bomb(a); } int* judge_king_bomb(int* a) { if (a[0] == 1 && a[1] == 1) { cout << array[0] << " " << array[1]; a[0] = 0; a[1] = 0; return a; } cout << "YAO BU QI"; return a; } int* judge(int* my, int* other) { int b = 0, c = 0, d = 0; if (one(other, &b)) my = judge_one(my, b); else if (pairs(other, &b)) my = judge_pairs(my, b); else if (three(other, &b, &c)) my = judge_three(my, b, c); else if (continuous_pairs(other, &b, &c)) my = judge_continuous_pairs(my, b, c); else if (continuous_one(other, &b, &c)) my = judge_continuous_one(my, b, c); else if (four_and_two(other, &b, &c)) my = judge_four_and_two(my, b, c); else if (plane(other, &b, &c, &d)) my = judge_plane(my, b, c, d); else if (bomb(other, &b)) my = judge_bomb(my, b); else if (king_bomb(other)) my = judge_king_bomb(my); else cout << "Wrong!" << endl; return my; } }; int main() { int n, m; cin >> n >> m; int *my = new int[15]; for (int i = 0; i < 15; i++) my[i] = 0; int other[15] = { 0 }; string s; Card card; for (int i = 0; i < n; i++) { cin >> s; my[card.decide(s)]++; } for (int i = 0; i < m; i++) { cin >> s; other[card.decide(s)]++; } card.judge(my,other); }