(hdu step 6.3.5)Card Game Cheater(匹配的最大数:a与b打牌,问b赢a多少次)

(hdu step 6.3.5)Card Game Cheater(匹配的最大数:a与b打牌,问b赢a多少次)

大家好,又见面了,我是全栈君,今天给大家准备了Idea注册码。

称号:

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 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

 
 
Source
Northwestern Europe 2004
 
Recommend
8600


题目大意:

a和b手上都有n张牌,b的一张牌赢了a的一张牌,b就得一分,问b能得多少分。

例子分析:

输入:

3
1//两个人手中的牌的张数
JD//adams手上的牌
JH//eves手上的牌
2
5D TC
4C 5H

输出:

第二个例子中的输出结果为1.为什么呢。

由于eves手上仅仅有5H比adams手上的5D大。得1分。他手上的4C要比

adams手上的TC要小,不能得分。


题目分析:

              二分图,求最大匹配。需要注意的是。

1、这道题中的匹配规则已经不是“a愿不愿意与b匹配”了。而是,仅仅有在b>a的时候才干匹配

2、这道题中b和a都是字符串。

怎样比較大小呢?先比較第1个字符然后,假设第1个字符相等再比較第2个字符。

3、这道题是用的是邻接表求的最大匹配数。



代码例如以下:

/*
 * e.cpp
 *
 *  Created on: 2015年3月14日
 *      Author: Administrator
 */

#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>

using namespace std;

const int maxn = 27;

vector<int> map[maxn];
bool useif[maxn];
int link[maxn];

int n;

/**
 * 获取一张牌前缀的索引
 */
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;
}

/**
 * 获取一张牌后缀的索引
 */
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;
}

/**
 * 比較a和b这两张牌哪一张牌比較大
 */
bool isBig(string a,string b){
	int aa = card_pre(a[0]);//获取一张牌的前缀
	int bb = card_pre(b[0]);//获取一张牌的后缀

	if(aa == bb){//假设这两张牌的前缀同样
		return card_suf(a[1]) < card_suf(b[1]);//则比較后缀
	}

	//假设这两张牌的前缀已经不同样则直接比較前缀
	return aa < bb;
}


/**
 * 推断一个节点t是否能找到和她匹配的节点
 */
bool can(int t){
	int i;
	int size = map[t].size();//获取愿意与节点t匹配的结点的数量
	for(i = 0 ; i < size ; ++i){//遍历每个愿意和结点t匹配的结点
		int index = map[t][i];//获取当前愿意和结点t匹配的节点的索引
		if(useif[index] == false){//假设这个节点还没有匹配
			useif[index] = true;//那么将这个节点标记为已经匹配
			//假设这个节点还没有匹配的节点||月这个节点匹配的节点可以找到其它结点与它匹配
			if(link[index] == -1 || can(link[index])){
				link[index] = t;//那么将当前节点匹配的结点设置为t

				return true;//返回true,表示根节点t可以找到与它匹配的节点
			}
		}
	}

	return false;//返回false,表示节点t无法找到与它匹配的结点
}


/**
 * 求醉大匹配数
 *
 */
int max_match(){
	int num = 0;

	int i;
	for(i = 0 ; i < n ; ++i){//遍历每个结点
		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;//这里千万不要在定义一个局部变量,否则会覆盖全局变量
		scanf("%d",&n);
		int i;

		//初始化变量
		memset(link,-1,sizeof(link));
		for(i = 0 ; i < n ; ++i){
			map[i].clear();
		}

		for(i = 0 ; i < n ; ++i){//初始化adams的牌
			cin >> adams[i];
		}

		string eves;
		/**
		 * 每次读入eves当前的牌,都接着计算它能与adams的那些牌匹配.
		 * 这列的匹配规则已经不是:a愿不愿意 与b匹配了.
		 * 而是仅仅有当b>a的时候,b才干与a匹配
		 * (为什么呢?由于eves要得分,那么他手上的牌就必需要比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());//计算最大匹配数
	}

	return 0;
}




版权声明:本文博客原创文章,博客,未经同意,不得转载。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/117559.html原文链接:https://javaforall.net

赞 (0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • mysql将yyyy转换_Mysql时间格式转换

    mysql将yyyy转换_Mysql时间格式转换对于每个类型拥有的值范围以及并且指定日期何时间值的有效格式的描述见7.3.6日期和时间类型。这里是一个使用日期函数的例子。下面的查询选择了所有记录,其date_col的值是在最后30天以内:mysql>SELECTsomethingFROMtableWHERETO_DAYS(NOW())-TO_DAYS(date_col)<=30;DAYOFWEEK(date)返回…

    2022年6月19日
  • JSP页面运行却显示源码

    JSP页面运行却显示源码使用SpringMVC跳转页面,但是页面显示的缺失JSP源码,如下:通过网上查找各种原因,最后找到解决方法,如下:问题是因为在web.xml文件中的”/*”,用该形式访问jsp文件时,星号“*”会把*.jsp,*.sql,*.txt都当做txt处理。结果就是直接在浏览器加载了jsp源码。所以,只要把星号“*”去掉,改为别种访问形式。如“。do”、“/…

    2022年6月22日
  • 免费申请国外免费域名超详细教程[通俗易懂]

    免费申请国外免费域名超详细教程[通俗易懂]1.首先申请免费域名网站:https://my.freenom.com/domains.php2.填入域名,这里我们以xcflag为列(尽量选择复杂一点的或者五个字母以上的域名,因为简单的有些域名是需要收费的),点击检查可用性。3.可以看到很多免费的域名(用的谷歌翻译插件,翻译有时候不是很准确,free翻译过来应该是免费而不是自由,之后会写一些关于谷歌插件的笔记,详细讲解)4.我们选择xcflag.tk点击立即获取,稍等一会点击购物车查看绿色按钮5.默认三个月试用,这里下拉框我们选择十二个月

    2022年6月30日
  • 看看行不行「建议收藏」

    看看行不行「建议收藏」支持FreakshareRapidgatorHotfileKeep2shareRyushareFilepostLumfileBitshareFilefactoryExtabitNetloadUploaded.toUl.toCtdisk等主流网盘会员直接高速下载!Ctdisk城通网盘/Dbank华为网盘完全免费!http://wishuhappy.com/[@Ie4Gy

    2022年9月6日
  • 数组的前缀和及查分数组

    数组的前缀和及查分数组1,前缀和主要适用场景是原始数组不会被修改的情况下,频繁查询某个区间的累加和。这里就不写前缀和的代码了,就是用一个数组记录下原有数组的前缀和。比如,prefix[i]就代表着nums[0…i-1]所有元素的累加和,如果我们想求区间nums[i…j]的累加和,只要计算prefix[j+1]-prefix[i]即可,而不需要遍历整个区间求和。(需要注意的是使用场景是频繁查询某个区间的累加和,而不需要对原始数组进行频繁修改)2,查分数组的主要适用场景是**频繁对原始数组的某个区间的元素进行增减。**比

    2022年6月11日
  • 【撸网站第一天】开篇

    【撸网站第一天】开篇今天准备撸一个网站,主要撸一下他的技术点,包括:前端、结构、后端、压力测试等等。现介绍一下这个网站:http://xingship.com测试下来,这个网站貌似只能在手机上使用,我在pc上显示的效果并不是很好,不知道站长是否可以兼容一下pc端。如下效果:很丑,但是当我跑到手机上来看的时候,还像那么回事。网站呢,主要是电影短视频的介绍。开始我以为这个是网站的布局是这一个标准的网格布局,但是其实他是个瀑布模式。第二篇,我们来详细介绍这个,好啦,今天由于时间关系,就现记录到这..

    2022年8月31日

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号