Uva--127 (线性表)

2014-06-15 02:05:36

题意&思路:就是简单模拟一下这个处理,由于太困了。。。敲了个普通程序,应该还有很大优化空间。。。。QAQ(被the number of piles == 1时用pile不加s坑了一次)
  1 #include <cstdio>
  2 #include <cmath>
  3 #include <cstdlib>
  4 #include <cstring>
  5 #include <iostream>
  6 using namespace std;
  7 
  8 struct node{
  9     char val,suit;
 10     node *right,*left,*hang;
 11     node(){
 12         val = suit = '0';
 13         left = right = hang = NULL;
 14     }
 15 };
 16 
 17 void Insert(node *a,node *b){
 18     if(b->hang == NULL){
 19         b->left->right = b->right;
 20         if(b->right != NULL)
 21             b->right->left = b->left;
 22     }
 23     else{
 24         b->left->right = b->hang;
 25         if(b->right != NULL)
 26             b->right->left = b->hang;
 27         node *w = b->hang;
 28         while(w != NULL){
 29             w->left = b->left;
 30             w->right = b->right;
 31             w = w->hang;
 32         }
 33     }
 34 
 35     node *q = a->hang;
 36     while(q != NULL){
 37         q->left = a->left;
 38         q->right=a->right;
 39         q = q->hang;
 40     }
 41 
 42     b->right = a->right;
 43     b->left = a->left;
 44     b->hang = a;
 45 
 46     a->left->right = b;
 47     if(a->right != NULL)
 48         a->right->left = b;
 49 }
 50 
 51 int main(){
 52     node *root = (node *)malloc(sizeof(node));
 53     node *p,*q;
 54     char s[5];
 55     while(1){
 56         p = root;
 57         for(int i = 0; i < 52; ++i){
 58             scanf("%s",s);
 59             if(s[0] == '#') break;
 60             q = (node*)malloc(sizeof(node));
 61             q->val = s[0];
 62             q->suit = s[1];
 63 
 64             p->right = q;
 65             q->left = p;
 66 
 67             p = p->right;
 68         }
 69         if(s[0] == '#') break;
 70         p = root;
 71         int flag = 1;
 72         while(flag){
 73             flag = 0;
 74             while(p != NULL){
 75                 if(p->left != NULL && p->left->left != NULL
 76                         && p->left->left->left != NULL && (p->left->left->left->val == p->val || p->left->left->left->suit == p->suit)){
 77                         q = p->left->left->left;
 78                         Insert(q,p);
 79                         flag = 1;
 80                         break;
 81                 }
 82                 else if(p->left != NULL && (p->left->val == p->val || p->left->suit == p->suit)){
 83                     q = p->left;
 84                     Insert(q,p);
 85                     //printf("left:%c %c  right:%c %c\n",p->left->val,p->left->suit,p->right->val,p->right->suit);
 86                     flag = 1;
 87                     break;
 88                 }
 89                 p = p->right;
 90             }
 91             if(flag){
 92                 p = root;
 93             }
 94         }
 95         int cnt = 0;
 96         p = root->right;
 97         while(p != NULL){
 98             ++cnt;
 99             p = p->right;
100         }
101         if(cnt == 1)
102             printf("%d pile remaining:",cnt);
103         else
104             printf("%d piles remaining:",cnt);
105         p = root->right;
106         while(p != NULL){
107             cnt = 0;
108             q = p;
109             while(q != NULL){
110                 ++cnt;
111                 //printf("hang : %c %c\n",q->val,q->suit);
112                 q = q->hang;
113             }
114             printf(" %d",cnt);
115             p = p->right;
116         }
117         puts("");
118     }
119     return 0;
120 }

转载于:https://www.cnblogs.com/naturepengchen/articles/3789050.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值