hoj2353-Card Hands

Card Hands

My Tags  (Edit)
 Source : unknown
 Time limit : 2 sec Memory limit : 64 M

Submitted : 204, Accepted : 49

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

地址:
题意:给出n副牌,有顺序,如果两副牌最后一张或最后几张完全一样,那么这张牌只记一次,问有多少张牌。
要是只看题真的很难理解他在干什么,但是如果看了trie树的话就容易理解了,就是把每组输入从后往前构成一棵trie树,前缀一样的只需要一个节点,输出总结点数。理解了题意就知道了,做法就是先输入,然后倒序建立trie树,最后输出sz-1即可。。。。。貌似很简单的样子,但是debug了一晚上,一直超时,最后在伟大的室友帮助下,终于发现是memset这个东西每次要把10^5的数组清零占了太多时间,不能随便用啊,还有就是我不细心,犯一些各种小毛病,我都想放弃了的时候,终于ac了(室友的debug能力好强,要是我自己,这道题一定又无限wa下去。。.)
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <vector>
#include <queue>
#include <algorithm>

using namespace std;
const int MaxNode=100000;
int chd[MaxNode][100];//存储节点编号
int sz;//节点数
int s[100000];
int n;
void insert()  //插入一个串
{
    int cur = 1;
    for (int i = 0; i<n; i++)
    {
        if (!chd[cur][s[i]])
            {chd[cur][s[i]] = ++sz;}
        cur = chd[cur][s[i]];
    }

}

void clear()  //初始化
{
    memset(chd, 0, sizeof (chd));
    sz = 1;
}

int main()
{
    int t,op[128],ip[128];
    for(int i=2;i<10;i++)
    {
        op[i+'0']=i-1;
    }
    op['A']=0;op['J']=10;op['Q']=11;op['K']=12;
    ip['C']=0;ip['D']=13;ip['H']=26;ip['S']=39;
    while(scanf("%d",&t)==1&&t)
    {
        clear();
        while(t--)
        {

            char str[5];
            scanf("%d",&n);
            for(int i=0; i<n; i++)
            {
                scanf("%s",str);
                if(str[0]=='1'&&str[1]=='0'){
                    s[n-i-1]=ip[str[2]]+9;
                }
                else s[n-i-1]=ip[str[1]]+op[str[0]];
            }
            insert();
            for(int i=0; i<n; i++) s[i] = 0;//神奇的取代了memset进行了初始化
        }
        printf("%d\n",sz-1);
    }
    return 0;
}
 
  

 



转载于:https://www.cnblogs.com/2860359561snail/p/3231722.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值