(Hdu step 6.3.5) Card Game cheater (maximum number of matches: A and B play cards, Q B win a how many times)

Source: Internet
Author: User

Title:

Card Game Cheater
Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 103 Accepted Submission (s): 74
Problem Descriptionadam and Eve play a card game using a regular deck of cards. The rules is 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-a row on the table. Adam's cards is numbered from 1 to K from the he left, and Eve's cards is numbered 1 to K from she right (so Eve's i:th car D is opposite Adam's i:th card). The cards is turned face up, and points is 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, a four beats a three and a, etc. An ace beats every card except (possibly) another ace.


If The i:th cards has the same value, then the suit determines who wins:hearts beats all other suits, spades beats a ll suits except hearts, diamond beats only clubs, and clubs does no beat any suit.

For example, the ten of Spades beats the ten of diamonds and not the Jack of clubs.

This ought to being a game of chance, but lately Eve was winning most of the time, and the reason was that she had started to u Se marked cards. In other words, she knows which cards Adam have on the table before he turns them face up. Using This information she orders hers own cards so, she gets as many points as possible.

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

inputthere would be several test cases. The first line of input would contain a single positive integer N giving the number of test cases. After this line follow the test cases.

Each test case starts with a line with a single positive integer k <= which are the number of cards each play Er gets. The next line describes the K cards Adam have placed on the table and left to right. The next line describes the K cards Eve had (but she had not yet placed them on the table). A card is described by-characters, the first one being its value (2, 3, 4, 5, 6, 7, 8, 9, T, J, Q, K, or A), and the S Econd one being its suit (C, D, S, or H). Cards is separated by white spaces. So if Adam's cards is the ten of clubs, the both of the hearts, and the Jack of diamonds, that could is described by the line< br>
TC 2H JD
 
Outputfor each test case output a single line with the number of points Eve gets if she picks the optimal-to arrange H ER cards on the table.

Sample Input
31jdjh25d tc4c 5h32h 3H 4h2d 3D 4D
Sample Output
112
Sourcenorthwestern Europe 2004
Recommend8600


Main topic:

A and B hand have n cards, b a card won a card, B will have a point, ask B how many points.

Example Analysis:

Input:


31//the number of cards in the hands of two people jd//adams hand cards jh//eves hands on the hand of the card 25D tc4c 5H

Output:

In the second example, the output is 1. For what?

Because the eves hand on only 5H than Adams hand 5D big. Get 1 points. He's got 4C in his hand.

Adams on the hands of the TC to be small, can not score.


Topic Analysis:

Binary graph, the maximum match is obtained. It's important to note that.

1. The matching rule in this question is not "A is willing to match with B". It's just the ability to match when B>a.

2. Both B and a are strings in this question.

What's the size? The 1th character then, assuming that the 1th character typeface and so on, is more than 2nd characters.

3, this problem is used is the adjacency table to find the maximum number of matches.



The code is as follows:

/* * e.cpp * * Created on:2015 March 14 * author:administrator * * #include <iostream> #include <cstdio> #inc Lude <vector> #include <cstring>using namespace std;const int maxn = 27;vector<int> Map[maxn];bool Useif[maxn];int link[maxn];int n;/** * Gets the index of a card prefix */int card_pre (char a) {string pre = "23456789TJQKA"; int len = Pre.length (); int i;for (i = 0; i < len; ++i) {if (A = = pre.at (i)) {return i;}} return 0;} /** * Gets the index of a card suffix */int card_suf (char a) {string suf = "CDSH"; int len = Suf.length (); int i;for (i = 0; i < len; ++i) {if ( A = = suf.at (i)) {return i;}} return 0;} /** * Compare A and B which cards are larger */bool IsBig (string a,string b) {int aa = Card_pre (a[0]);//get a card prefix int bb = card_pre (b[0]);//Get a card after The prefix if (aa = = BB) {//assumes that the prefixes of the two cards also return Card_suf (A[1]) < Card_suf (b[1]), or the suffix}//assumes that the prefixes of the two cards are not the same directly compared to the prefix return AA < BB;} /** * Infers if a node T can find a node that matches her */bool can (int t) {int I;int size = map[t].size ();//Gets the number of nodes that are willing to match node t for (i = 0; i < size; ++i {//iterate over each willing and node T match node int index = Map[t][i];//gets the index of the node that is currently willing and nodes T to match if (useif[index] = = False) {//Assuming that the node has not yet matched useif[index] = true;//then mark the node as already matched//assuming that the node has not yet matched the node | | Month This node matching node can find other nodes to match it if (link[index] = = 1 | | can (LINK[INDEX])) {Link[index] = t;//Then set the node matching the current nodes to Treturn true;// Returns True, which indicates that the root node T can find the node that matches it}}}return false;//returns false, indicating that node T cannot find a match with it}/** * Get drunk large Match number * */int Max_match () {int num = 0;int i; for (i = 0; i < n; ++i) {//Traverse each node memset (useif,false,sizeof (Useif)), if (can (i) = = true) {num++;}} return num;} int main () {int t;scanf ("%d", &t), String Adams[maxn];while (t--) {//int n;//do not define a local variable here, otherwise it overrides the global variable scanf ("%d", &n); int i;//initialize variable memset (link,-1,sizeof (link)); for (i = 0; i < n; ++i) {map[i].clear ();} for (i = 0; i < n; ++i) {//Initialize Adams card cin >> adams[i];} String eves;/** * Each time the eves current card is read, it is then calculated that it can match the Adams cards. * The matching rule for this column is no longer: A would like to match B. * But only when the B>a, B talent and a match * (for what? Because Eves to score, then his hand will need to be larger than Adams) */for (i = 0; i < n; ++i) {cin >> eves;int j;for (j = 0; J < N; ++j) {if (IsBig (adams[j],eves) = = True) {Map[i].push_back (j);}}} printf ("%d\n", Max_match ());//calculates the maximum number of matches}return 0;}



Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.

(Hdu step 6.3.5) Card Game cheater (maximum number of matches: A and B play cards, Q B win a how many times)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.