若是凉夜已成梦

青春里 总有些事情要努力去做 总有些梦想要拼命去追。


  • 运维

  • 前端

  • 编程

  • 随笔

  • hust-oj

1195: Card Hands

发表于 2017-10-06   |   分类于 HUSTOJ   |   阅读次数 1,197

题目描述

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.

输入

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.

输出

For each test case, output a line containing the number of linked list nodes needed to store all the lists.

样例输入

3
3 7D AH 5S
4 9C 3D 4D 5S
2 AH 5S
0

样例输出

6

参考代码

#include <stdio.h>
#include <string.h>
#include <assert.h>
char op[101][101];
int i,j,k,n,N,no;
int eval(int x) 
{
    int i,j;
    int stack[101], n=0;
    for (i=0;i<no;i++) 
    {
        if (!strcmp(op[i],"x")) 
        {
            stack[n++] = x;
        } else if (!strcmp(op[i],"+")) 
        {
            stack[n-2] = (stack[n-2]+stack[n-1])%N;
            n--;
        } else if (!strcmp(op[i],"*")) 
        {
            stack[n-2] = ((long long)stack[n-2]*stack[n-1])%N;
            n--;
        } else 
        {
            stack[n++] = atoi(op[i]);
        }
    }
    assert (n == 1);
    return stack[0];
}
main() 
{
    int x,xx;
    while (2 == scanf("%d%d",&N,&n)&&N) 
    {
        for (i=0;1 == scanf("%s",op[i]) && strcmp(op[i],"%");i++);
        no = i-1;
        x = xx = n;
        for (j=1;;j++) 
        {
            x = eval(x);
            xx = eval(xx);
            xx = eval(xx);
            if (x == xx) 
            {
                for (k=1;x != (xx=eval(xx));k++);
                printf("%dn",k);
            break;
         }
      }
   }
}

解析

暂无

hustoj

发表评论 取消回复

电子邮件地址不会被公开。 必填项已用*标注

*
*


hoxis wechat
著作权归作者所有
站点更新说明
  • 文章目录
  • 站点概览
若是凉夜已成梦

若是凉夜已成梦

青春里 总有些事情要努力去做 总有些梦想要拼命去追。

1904 日志
6 分类
12 标签
RSS
weibo github twitter facebook

友情链接

Skip 原站点 Dreams孤独患者
© 2017 若是凉夜已成梦
Powered by WordPress | 已运行
Theme By NexT.Mist