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