Card Hands(字典树)

Card Hands

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.
Samples
Input
3
3 7D AH 5S
4 9C 3D 4D 5S
2 AH 5S
0
Output
6
Source
waterloo 2020.7.25

题意:有一副扑克牌,"A, 2, 3, 4, 5, 6, 7, 8, 9, 10, J, Q, K"代表点数,"C, D, H, or S"代表梅花,方片,红桃,黑桃;输入第一行T个人,接下来T行每一行第一个数是该人手牌的数量n,然后跟着n个字符串。有相同后缀的手牌只算一次,问你总共有几张手牌;
样例:第一个人和第二个人有相同后缀5S,和第三个人有相同后缀5S及AH,故答案为6;

思路:字典树建树,然后看有几个节点就好了;
tree数组的第一维表示该节点的编号;第二维表示该节点下的son是否存在;
例如:tree[0][3]表示0号节点(即根节点)下3号儿子存在
Code:

#include<bits/stdc++.h>
#pragma GCC optimise(2)
using namespace std;
typedef long long ll;
const int N = 1e5 + 10;
const int inf = 1e9;
int tree[N][99];
int T,p,n;
map<char,int> mp,st;
void work() {
	mp['A'] = 0;
	for(int i=2;i<=10;i++) 	mp['0'+i] = i-1;
	mp['J'] = 10;
	mp['Q'] = 11;
	mp['K'] = 12;
	st['C'] = 0;
	st['D'] = 13;
	st['H'] = 26;
	st['S'] = 39;
	return ;
}
void build(int res[],int len) {
	int rt = 0;
	for(int i=0;i<len;i++) {
		if(!tree[rt][res[i]]) tree[rt][res[i]] =  ++p;
		rt = tree[rt][res[i]];
	}
}
int main() {
	work();
	while(cin>>T && T) {
		memset(tree,0,sizeof(tree));
		p=0;
		while(T--) {
			scanf("%d",&n);
			int res[N<<2];
			for(int i=1;i<=n;i++) {
				string s;
				cin>>s;
				if(s[0]=='1' && s[1]=='0') {
					res[n-i] = 9 + st[s[2]];
				} else res[n-i] = mp[s[0]] + st[s[1]];
			}
			build(res,n);
		}	
		printf("%d\n",p);
	}
	return 0;
}
/*


*/

不要伤心,不要忧郁,快乐的日子终会到来;

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值