1 #include <stdio.h>
2 #include <string.h>
3
4 #define BOOLEAN int
5 #define TRUE 1
6 #define FALSE 0
7
8 #define MAX 52
9 #define INPUT_COUNT_EACH_LINE 26
10
11 typedef struct tagPile
12 {
13 char rank[MAX];
14 char suit[MAX];
15 int cardCount;
16 }Pile;
17
18 Pile pile[MAX] = {{0}};
19 int pileCount = 0;
20
21 char card[MAX][2] = {{0}};
22
23 BOOLEAN getNewPack()
24 {
25 int i = 0;
26 int j = 0;
27 int k = 0;
28 char line[100] = {0};
29
30 for(i = 0; i < 2; i++)
31 {
32 gets(line);
33 if(line[0] == ‘#‘)
34 {
35 return 0;
36 }
37
38 for(j=0; j<INPUT_COUNT_EACH_LINE; j++)
39 {
40 for(k=0; k<2; k++)
41 {
42 card[INPUT_COUNT_EACH_LINE*i+j][k] = line[j*3+k];
43 }
44 }
45 }
46 return TRUE;
47 }
48
49 BOOLEAN compareCard(pileId1, pileId2)
50 {
51 if((pile[pileId1].rank[pile[pileId1].cardCount-1] == pile[pileId2].rank[pile[pileId2].cardCount-1])
52 || (pile[pileId1].suit[pile[pileId1].cardCount-1] == pile[pileId2].suit[pile[pileId2].cardCount-1]))
53 {
54 return TRUE;
55 }
56 else
57 {
58 return FALSE;
59 }
60 }
61
62 int comparePile(int pileId)
63 {
64 int i = 0;
65
66 if(pileId>2)
67 {
68 if(compareCard(pileId, pileId-3))
69 {
70 return pileId-3;
71 }
72 }
73 else if(pileId > 0)
74 {
75 if(compareCard(pileId, pileId-1))
76 {
77 return pileId-1;
78 }
79 }
80
81 return pileId;
82 }
83
84 void moveCard(int pileIdNew, int pileIdOld)
85 {
86 int cardCountNew = pile[pileIdNew].cardCount;
87 int cardCountOld = pile[pileIdOld].cardCount;
88 cardCountNew++;
89
90 pile[pileIdNew].cardCount++;
91 pile[pileIdNew].rank[cardCountNew-1] = pile[pileIdOld].rank[cardCountOld-1];
92 pile[pileIdNew].suit[cardCountNew-1] = pile[pileIdOld].suit[cardCountOld-1];
93 pile[pileIdOld].cardCount--;
94
95 return;
96 }
97
98 void moveOnePile(int pileIdOld)
99 {
100 memcpy(&pile[pileIdOld], &pile[pileIdOld+1], sizeof(Pile));
101
102 return;
103 }
104
105 void moveAllPile(int pileIdOld)
106 {
107 for(; pileIdOld < pileCount-1; pileIdOld++)
108 {
109 moveOnePile(pileIdOld);
110 }
111
112 pileCount--;
113
114 return;
115 }
116
117
118 void mergePile(int pileId)
119 {
120 int pileIdNew = 0;
121 int pileIdOld = pileId;
122
123 for(pileIdNew = pileIdOld; pileIdNew<pileCount; )
124 {
125 pileIdNew = comparePile(pileIdOld);
126 if(pileIdNew == pileIdOld)
127 {
128 pileIdNew++;
129 pileIdOld++;
130 }
131 else
132 {
133 moveCard(pileIdNew, pileIdOld);
134 if(!pile[pileIdOld].cardCount)
135 {
136 moveAllPile(pileIdOld);
137 pileIdOld--;
138 }
139 }
140 }
141
142 return;
143 }
144
145 /*undo*/
146 void putCard(int cardId)
147 {
148 pileCount++;
149
150 pile[pileCount-1].rank[0] = card[cardId][0];
151 pile[pileCount-1].suit[0] = card[cardId][1];
152 pile[pileCount-1].cardCount++;
153
154 return;
155 }
156
157 void printCard()
158 {
159 int i = 0;
160
161 if(pileCount>1)
162 {
163 printf("%d piles remaining:", pileCount);
164 }
165 else{
166 printf("%d pile remaining:", pileCount);
167 }
168
169 printf("%d piles remaining:", pileCount);
170 for(i = 0; i < pileCount; i++)
171 {
172 printf(" %d", pile[i].cardCount);
173 }
174 printf("\n");
175 }
176
177 int main(int argc, char *argv[])
178 {
179 int i = 0;
180
181 while(getNewPack())
182 {
183 for(i = 0; i < MAX; i++)
184 {
185 putCard(i);
186 mergePile(pileCount-1);
187 }
188
189 printCard();
190 }
191
192 return 0;
193 }