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

**

思路:
本题是求字典树的节点数,不过与通常不同的是,他是后缀,所以我们可以从后往前建字典树,这样建完和前缀字典树相同。
对牌进行赋值(字符串建成字典树也是先把字符转化成数字

map<char,int>mp,ip;
void intal(){
	mp['A'] = 0;
	for(int i=2;i<10;i++) 	mp['0'+i] = i-1;
	mp['J'] = 10;
	mp['Q'] = 11;
	mp['K'] = 12;
	ip['C']=0;ip['D']=13;ip['H']=26;ip['S']=39;
}
int tr[maxn][99];
string s;
int tot;
void creat(int res[],int n){
	int p=0;
	for(int i=0;i<n;i++){
		if(!tr[p][res[i]]) tr[p][res[i]]=++tot;
		p=tr[p][res[i]];
	}
}
int main(){
	intal();
	int T,n;
	while(cin>>T&&T){
		memset(tr,0,sizeof(tr));
		tot=0;
		for(int i=1;i<=T;i++){
			scanf("%d",&n);
		int res[200000];
			for(int j=1;j<=n;j++){
				cin>>s;
				if(s[0]=='1'&&s[1]=='0')	res[n-j]=9+ip[s[2]];//当第一位是10的时候
				else{
					res[n-j]=mp[s[0]]+ip[s[1]];
				}	
			}
		creat(res,n);		
		}
		cout<<tot<<"\n";//tot 就是结点数量
	} 
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值