#include #include #include using namespace std; int copy_it(int deal[], int goal[], int length) { for (int i = 0; i < length; ++i) goal[i] = deal[i]; return 0; } //search for the first location the element appears int search(int a[], int length, int element) { int i = 0; for (; i < length; ++i) { if (a[i] == element) return i; } return -1; } //带sum组不同的,每组有num个且组内相同,且只能取在up_bound和down_bound之外(不含边界) int pick_out(int own[], int cho[], int length, int sum, int num, int up_bound, int down_bound) { int time = 0; bool signs = false; int temporary[15] = { 0 }; for (int m = 0; m < length && time < sum; ++m) { if (own[m] >= num && m > up_bound && m < down_bound) { temporary[m] = num; signs = true; time++; } else { signs = false; time = 0; } } if (time < sum) return -1; for (int i = 0; i < length; ++i) { if (temporary[i] != 0) { cho[i] = temporary[i]; own[i] -= temporary[i]; } } return 0; } int get_card(int a[], int num, int len) { string s; for (int i = 0; i < len; ++i) { cin >> s; if (s == "3") a[0]++; else if (s == "4") a[1]++; else if (s == "5") a[2]++; else if (s == "6") a[3]++; else if (s == "7") a[4]++; else if (s == "8") a[5]++; else if (s == "9") a[6]++; else if (s == "10") a[7]++; else if (s == "J") a[8]++; else if (s == "Q") a[9]++; else if (s == "K") a[10]++; else if (s == "A") a[11]++; else if (s == "2") a[12]++; else if (s == "joker") a[13]++; else if (s == "JOKER") a[14]++; } return 0; } int select_one(int own[], int cho[], int length, int num,int first_loc) { int j = first_loc; bool sign = false; for (int i = j + 1; i < length; ++i) { if (own[i] >= num) { cho[i] = num; own[i] -= num; sign = true; break; } } if(sign) return 0; if (own[length - 2] == 1 && own[length - 1] == 1) { cho[length - 2] = 1; own[length - 2] -= 1; cho[length - 1] = 1; cho[length - 2] -= 1; return 0; } return -1; } int attach_to_three_or_four(int own[], int cho[], int length, int sum_2, int sum_1, int major_card, int first_loc) { bool sign = true; int loc_maj = first_loc + 1; for (; loc_maj < length; ++loc_maj) { if (own[loc_maj] >= major_card) { cho[loc_maj] = major_card; own[loc_maj] -= major_card; sign = false; break; } } if (sign)//there is no bigger cards return -1; else if (sum_1 == 0 && sum_2 == 0) return 0; else if (sum_1 == 1 || sum_2 == 1) { int sum; if (sum_1 == 1) sum = 1; else if (sum_2 == 1) sum = 2; for (int k = 0; k < length; ++k) { if (own[k] >= sum && k != loc_maj) { cho[k] = sum; own[k] -= sum; return 0; } } return -1;//no proper cards } else if (sum_1 == 2) return pick_out(own, cho, length, 2, 1, loc_maj, loc_maj); else if (sum_2 == 2) return pick_out(own, cho, length, 2, 2, loc_maj, loc_maj); return -1; } //pick out the straignt and sequential pairs int straight(int own[], int cho[], int length, int sum, int sequential_num, int first_loc) { //int loc = first_loc; bool sign = false; int select = 0, time = 0; for (int j = first_loc + 1; j < length; ++j) { if (own[j] >= sequential_num) { if (!sign) select = j; sign = true; ++time; } else { sign = false; time = 0; } if (time == sum) break; } if (time == sum) { for (int k = 0; k < sum; ++k) { cho[select + k] = sequential_num; own[select + k] -= sequential_num; } return select; } else return -1; } //pick out the aeroplane int aeroplane(int own[], int cho[], int length, int sum_3, int sum_2, int sum_1, int first_loc) { int provi[15] = { 0 }; copy_it(own, provi, length); int trans[15] = { 0 }; int flag = straight(provi, trans, length, sum_3, 3, first_loc); if (flag == -1) return -1; int tempor[15] = { 0 }; int sign = 0; if (sum_2 == sum_3 || sum_1 == sum_3) { int sum = 0; if (sum_2 == sum_3) sum = 2; else if (sum_1 == sum_3) sum = 1; sign = pick_out(provi, trans, length, sum_3, sum, flag + sum_3, flag); } else if (sum_1 == sum_3 * 2) sign = pick_out(provi, trans, length, sum_1, 1, flag + sum_3, flag); if (sign == 0) { copy_it(provi, own, length); copy_it(trans, cho, length); } return sign; } //search for the crump int crump(int own[], int cho[], int length, int first_loc = -1) { bool sign = false; for (int i = first_loc; i < length; ++i) { if (own[i] == 4) { cho[i] = 4; own[i] -= 4; sign = true; break; } } if (sign) return 0; if (own[length - 2] == 1 && own[length - 1] == 1) { cho[length - 2] = 1; own[length - 2] -= 1; cho[length - 1] = 1; own[length - 1] -= 1; return 0; } else return -1; } int determine(int own[], int adv[], int cho[], int length) { int sum = 0, single_card = 0, double_card = 0, three_card = 0, four_card = 0; for (int i = 0; i < length; ++i) { sum += adv[i]; switch (adv[i]) { case 1:single_card++; break; case 2:double_card++; break; case 3:three_card++; break; case 4:four_card++; break; } } int first_place = 0; int sign = 0; if (single_card == 1 && sum == single_card) { first_place = search(adv, length, 1); sign = select_one(own, cho, length, 1, first_place);//there is only one card } else if (double_card == 1 && sum == double_card * 2) { first_place = search(adv, length, 2); sign = select_one(own, cho, length, 2, first_place);//only one pair } else if (three_card == 1) { first_place = search(adv, length, 3); sign = attach_to_three_or_four(own, cho, length, double_card, single_card, 3, first_place);//必为三带形式 } else if (four_card == 1 && four_card + 2 == sum)//so it will not be a crump { first_place = search(adv, length, 4); sign = attach_to_three_or_four(own, cho, length, double_card, single_card, 4, first_place);//应该为四带二或 炸弹? } else if (single_card >= 5 && single_card == sum) { first_place = search(adv, length, 1); sign = straight(own, cho, length, single_card, 1, first_place);//must be a straight } else if (double_card >= 3 && double_card * 2 == sum) { first_place = (adv, length, 2); sign = straight(own, cho, length, double_card, 2, first_place);//must be sequential pairs } else if (four_card == 1 && four_card * 4 == sum) { first_place = (adv, length, 4); sign = crump(own, cho, length, first_place); return sign; } else { first_place = search(adv, length, 3); sign = aeroplane(own, cho, length, three_card, double_card, single_card, first_place);//only the aeroplane left } if (sign != -1) return 0; else return crump(own, cho, length); } int print(int a[], int length) { int ability = 0; for (int i = length - 1; i >= 0; --i) { if (a[i] > 0) { int time = a[i]; for (int j = 0; j < time; ++j) { switch (i) { case 0:cout << 3 << ' '; break; case 1:cout << 4 << ' '; break; case 2:cout << 5 << ' '; break; case 3:cout << 6 << ' '; break; case 4:cout << 7 << ' '; break; case 5:cout << 8 << ' '; break; case 6:cout << 9 << ' '; break; case 7:cout << 10 << ' '; break; case 8:cout << 'J' << ' '; break; case 9:cout << 'Q' << ' '; break; case 10:cout << 'K' << ' '; break; case 11:cout << 'A' << ' '; break; case 12:cout << 2 << ' '; break; case 13:cout << "joker" << ' '; break; case 14:cout << "JOKER" << ' '; break; } } } else ability++; } if (ability == length) cout << "Yao Bu Qi"; cout << endl; return 0; } int main() { int len1, len2; cin >> len1>>len2; int own[15] = { 0 }; int adv[15] = { 0 }; int choice[15] = { 0 }; get_card(own, 15, len1); get_card(adv, 15, len2);//collect the cards int sign = 0; sign = determine(own, adv, choice, 15);//determine the type of the cards //search whether you have proper cards to give if (sign != -1) print(choice, 15); else cout << "Yao Bu Qi" << endl; //cout << "Now we have: "; //print(own, 15); return 0; }