POJ-3283 Card Hands

9 篇文章 0 订阅
7 篇文章 0 订阅
Card Hands
Time Limit: 1000MS Memory Limit: 65536K
   

Description

Jim is writing a program for statistically analyzing card games. He needs to store many different card hands in memory efficiently. Each card has one of four suits and one of thirteen values. In his implementation, each hand is stored as a linked list of cards in a canonical order: the cards are first ordered by suit: all the clubs come first, followed by all the diamonds, then all the hearts, and finally the spades. Within each suit, the cards are ordered by value: A, 2, 3, 4, 5, 6, 7, 8, 9, 10, J, Q, K. Each hand contains at most one of any given card.

The card hands are using lots of memory. Jim therefore decides to try a more efficient representation. Whenever any two lists share a common tail, they can be updated to share one copy of the tail, and the other copy can be discarded. This process can be repeated until no two lists share a common tail.

Your job is to tell Jim how many linked list nodes he needs to store all the card hands.

Input

The input contains several test cases followed by a line containing 0. The first line of each case contains the number of card hands. Each subsequent line describes a card hand. It starts with a number indicating the number of cards in the hand. The cards follow, separated by spaces, in the canonical order defined above. For each card, the value is given first, followed by the suit (C, D, H, or S). There are at most 100,000 cards in all hands.

Output

For each test case, output a line containing the number of linked list nodes needed to store all the lists.

Sample Input

3
3 7D AH 5S
4 9C 3D 4D 5S
2 AH 5S
0

Sample Output

6

Source

————————————————————解气的分割线————————————————————

思路:终于把这题弄出来了!理解能力太渣!战斗力不足5啊!其实的确是水题,奈何读不懂到底想干嘛……有必要解释一下题意!输入是这样的,先给出人数。然后每个人手中有若干牌,把所有人手中的牌倒序插入字典树,问树上多少结点。恰当的映射会使得这些牌已经按字典序排好。这里牵扯到映射,52张扑克牌就像是一个有52个字母的语言,因此要想办法把字符变成整数。哈希。散列值。将字符转换一下。数学渣的反省中……注意了!扑克10是两个字符。

怎么映射呢?有标准的完全散列:

(十三进制思想)花色CDHS变成0123,然后是A~K:0~12,即 花色*13+值。

之后就是理解题目,题目就是单纯的从最后一张牌开始看,假如root上有这张牌,不必新开结点,直接插上去,然后看该结点下有没有倒数第二张;如果root上没有这张牌,则新开结点。一路向下都是新结点。我的写法是递归。

代码如下:

#include <stdio.h>
#include <string.h>
const int MAXN = 100010;
struct Trienode {
	Trienode* next[52];
}Node[MAXN];
int nxt, ans;
char dic[MAXN][4];

Trienode* Newnode() {
	memset(&Node[nxt], 0, sizeof(Trienode));
	return &Node[nxt++];
}

void Insert(Trienode* rt, int i) {//参数i是此人手中的第i张牌
	if(i < 0)	return;//递归边界
	int c, ten = 0;
	switch(dic[i][0]) {//以下是散列过程
		case '1':c = 9; ten = 1; break;
		case 'A':c = 0; break;
		case 'J':c = 10; break;
		case 'Q':c = 11; break;
		case 'K':c = 12; break;
		default :c = dic[i][0] - '0' - 1;
	}
	switch((ten ? dic[i][2] : dic[i][1])) {//10占两个字符
		case 'C':c += 0; break;
		case 'D':c += 13; break;
		case 'H':c += 13<<1; break;
		case 'S':c += 13*3; break;
	}
	if(!rt->next[c]) {
		rt->next[c] = Newnode();
		ans++;
	}
	rt = rt->next[c];
	Insert(rt, i-1);
}

int main() {
	int man;
	//freopen("test.in", "r", stdin);
	while(~scanf("%d", &man), man) {
		ans = nxt = 0;
		Trienode* root = Newnode();
		int card;
		while(man--) {
			scanf("%d", &card);
			for(int i = 0; i < card; i++)
				scanf("%s", dic[i]);
			Insert(root, card-1);
		}
		printf("%d\n", ans);
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值