HDU 1528 Card Game Cheater(打扑克)

Description:

Adam and Eve play a card game using a regular deck of 52 cards. The rules are simple. The players sit on opposite sides of a table, facing each other. Each player gets k cards from the deck and, after looking at them, places the cards face down in a row on the table. Adam’s cards are numbered from 1 to k from his left, and Eve’s cards are numbered 1 to k from her right (so Eve’s i:th card is opposite Adam’s i:th card). The cards are turned face up, and points are awarded as follows (for each i ∈ {1, . . . , k}): 

If Adam’s i:th card beats Eve’s i:th card, then Adam gets one point. 

If Eve’s i:th card beats Adam’s i:th card, then Eve gets one point. 

A card with higher value always beats a card with a lower value: a three beats a two, a four beats a three and a two, etc. An ace beats every card except (possibly) another ace. 

If the two i:th cards have the same value, then the suit determines who wins: hearts beats all other suits, spades beats all suits except hearts, diamond beats only clubs, and clubs does not beat any suit. 

For example, the ten of spades beats the ten of diamonds but not the Jack of clubs. 

This ought to be a game of chance, but lately Eve is winning most of the time, and the reason is that she has started to use marked cards. In other words, she knows which cards Adam has on the table before he turns them face up. Using this information she orders her own cards so that she gets as many points as possible. 

Your task is to, given Adam’s and Eve’s cards, determine how many points Eve will get if she plays optimally. 

Input:

There will be several test cases. The first line of input will contain a single positive integer N giving the number of test cases. After that line follow the test cases. 

Each test case starts with a line with a single positive integer k <= 26 which is the number of cards each player gets. The next line describes the k cards Adam has placed on the table, left to right. The next line describes the k cards Eve has (but she has not yet placed them on the table). A card is described by two characters, the first one being its value (2, 3, 4, 5, 6, 7, 8 ,9, T, J, Q, K, or A), and the second one being its suit (C, D, S, or H). Cards are separated by white spaces. So if Adam’s cards are the ten of clubs, the two of hearts, and the Jack of diamonds, that could be described by the line 
TC 2H JD 

Output:

For each test case output a single line with the number of points Eve gets if she picks the optimal way to arrange her cards on the table. 

Sample Input:
3
1
JD
JH
2
5D TC
4C 5H
3
2H 3H 4H
2D 3D 4D

Sample Output:

1
1
2
题意:两个人Adam,Eve在一起打扑克牌,本来两个人是要按照顺序出牌的,但是由于Adam的牌放在桌面上,而Eve的牌放在下面,所以时间长了Eve就知道了Adam出牌的规律,由此Eve也知道自己该如何出牌来得到更高的分数,谁的牌大,谁得1分,现在问Eve用最好的方案来得到的最高的分数。
 
比牌大小的规则:两个人的出的牌假设都是只有两个字符的字符串a,b,那么当a[0]
 
#include
#include<string.h>
#include
#include
#include
#include
using namespace std;

const int N=1e6+10;
const int INF=0x3f3f3f3f;
const int MOD=1e9+7;

typedef long long LL;

char str1[20]={"23456789TJQKA"}; ///在这两个str字符串中下标较小的元素也是较小的,这样便于比较
char str2[20]={"CDSH"};
char s1[50][50], s2[50][50];

void Sort(char a[], char b[]) ///判断a是否>b,如果是则交换
{
    int ida1, ida2, idb1, idb2, i; 
    char ch[50];

    for (i = 0; i < 13; i++)
    {
        if (str1[i] == a[0]) ida1 = i; ///ida1代表a[0]在str1中的位置
        if (str1[i] == b[0]) idb1 = i; ///idb1代表b[0]在str1中的位置
    }
    for (i = 0; i < 4; i++)
    {
        if (str2[i] == a[1]) ida2 = i; ///ida2代表a[1]在str2中的位置
        if (str2[i] == b[1]) idb2 = i; ///idb2代表b[1]在str2中的位置
    }

    if (ida1 > idb1) ///下标大,对应的元素就大
    {
        strcpy(ch, a);
        strcpy(a, b);
        strcpy(b, ch);
    }
    else if (ida1 == idb1)
    {
        if (ida2 > idb2)
        {
            strcpy(ch, a);
            strcpy(a, b);
            strcpy(b, ch);
        }
    }
}

int Judge(char a[], char b[]) ///判断b是否>a
{
    int ida1, ida2, idb1, idb2, i;

    for (i = 0; i < 13; i++)
    {
        if (str1[i] == a[0]) ida1 = i;
        if (str1[i] == b[0]) idb1 = i;
    }
    for (i = 0; i < 4; i++)
    {
        if (str2[i] == a[1]) ida2 = i;
        if (str2[i] == b[1]) idb2 = i;
    }

    if (ida1 < idb1) return 1;
    if (ida1 == idb1 && ida2 < idb2) return 1;

    return 0;
}

int main ()
{
    int T, n, i, j, ans;

    scanf("%d", &T);

    while (T--)
    {
        ans = 0;

        scanf("%d", &n);
        for (i = 0; i < n; i++)
            scanf("%s", s1[i]);
        for (i = 0; i < n; i++)
            scanf("%s", s2[i]);

        for (i = 0; i < n-1; i++)
        {
            for (j = i+1; j < n; j++)
            {
                Sort(s1[i], s1[j]); ///由于牌的排序比较麻烦,所以我们可以采用冒泡排序,每次比较两个牌的大小
                Sort(s2[i], s2[j]);
            }
        }

        i = j = 0;
        while (i < n && j < n) ///任意一方无法找到合适的牌出,就结束
        {
            if (Judge(s1[i], s2[j])) ///如果在s2中找到比s1[i]大的牌,分数+1,双方都能出一张牌i++,j++
            {
                i++;
                j++;
                ans++;
            }
            else j++; ///由于两个数组都是排过序的,所以s2[j]当这张牌不能大于s1[i]时,我们需要j++,继续在s2中查找,那么试想当Adam出了一张牌,而我们不能在s2中找到比它大的牌,那么这种查找就可以结束了,因为Adam剩下的牌会更大
        }

        printf("%d\n", ans);
    }

    return 0;
}

转载于:https://www.cnblogs.com/syhandll/p/4950809.html

你可能感兴趣的:(HDU 1528 Card Game Cheater(打扑克))