POJ-3283 Card Hands
来源:互联网 发布:数据算法 编辑:程序博客网 时间:2024/06/05 16:06
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
33 7D AH 5S4 9C 3D 4D 5S2 AH 5S0
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;}
- POJ 3283 Card Hands
- POJ-3283 Card Hands
- POJ 3283 Card Hands 可能会
- POJ 3283 Card Hands Trie树
- [HOJ 2353]Card Hands[Trie]
- POJ3283 Card Hands(字典树)
- Poj 1786 Bridge Hands
- poj 1786 Bridge Hands
- POJ 1786Bridge Hands
- poj 1786 Bridge Hands
- POJ 1786 Bridge Hands 笔记
- poj 3032.Card Trick
- POJ-3629-Card Stacking
- POJ,3032,Card Trick
- poj 3032 Card Trick
- POJ 3032Card Trick
- POJ-3032 Card Trick
- POJ-3032 Card Trick
- cocos2dx内存管理的一些看法
- openOCD+jlink调试uboot
- ubuntu下使用OpenCV打开摄像头
- POJ 3687
- WebView加载加速
- POJ-3283 Card Hands
- 【Android】SlidingMenu属性详解
- 程序1.4 等分路径压缩
- ubuntu下配置eclipse+tomcat
- jQuery学习总结
- opengl编程指南第一例
- 享元模式
- java中String的“==”和equals判断相等性
- 字符设备注册相关函数