#include <stdio.h>#include <stdlib.h>#define LOCAL#define ERROR 0#define OK 1typedefstructdata{
charrank;
charsuit;
structdata*next;
}Data;
typedefstructlnode{
intcount;
Data*top;
structlnode*next;
}LNode, *LinkList;
LinkListinitList();
intGetElem(LNode*h, inti, LNode**e);
LNode*GetPrecursor(LinkListh, LNode*e);
intmatch(Data*a, Data*b);
voidfreespace(LNode*h);
intmain()
{
intans[52];
inti;
intfirst;
charstr[3];
LinkListh=NULL;
intcount=0;
intstatus;
LNode*node , *e=NULL, *p=NULL, *pre=NULL;
Data*q=NULL;
intflag;
Data*datanode=NULL;
intpiles;
#ifdef LOCALfreopen("c://uva_in.txt", "r", stdin);
#endifwhile (1)
{
scanf("%s", str);
if (str[0] =='#')
break;
if (count==0)
h=initList();
datanode= (Data*)malloc(sizeof(Data));
datanode->rank=str[0];
datanode->suit=str[1];
datanode->next=NULL;
node= (LNode*)malloc(sizeof(LNode));
node->top= (Data*)malloc(sizeof(Data));
node->top->next=datanode;
node->count=1;
node->next=h->next;
h->next=node;
p=h->next;
while (1)
{
flag=1;
while (flag)
{
flag=0;
status=GetElem(p, 3, &e);
if (status==ERROR|| (status==OK&&!match(p->top->next, e->top->next)))
status=GetElem(p, 1, &e);
if (status==OK&&match(p->top->next, e->top->next))
{
flag=1;
q=p->top->next;
p->top->next=q->next;
q->next=e->top->next;
e->top->next=q;
e->count++;
p->count--;
if (p->count==0)
{
pre=GetPrecursor(h, p);
pre->next=p->next;
free(p);
}
p=e;
}
}
pre=GetPrecursor(h, p);
if (pre!=h)
p=pre;
elsebreak;
}
count++;
if (count==52)
{
p=h->next;
i=0;
while (p)
{
ans[i++] =p->count;
p=p->next;
}
piles=i;
if (piles==1)
printf("%d pile remaining: ", piles);
elseprintf("%d piles remaining: ", piles);
for (i=piles-1, first=1; i>=0; i--)
{
if (first)
first=0;
elseprintf(" ");
printf("%d", ans[i]);
}
printf("/n");
freespace(h);
count=0;
}
}
return0;
}
LinkListinitList()
{
LNode*p= (LNode*)malloc(sizeof(LNode));
p->top=NULL;
p->next=NULL;
returnp;
}
intGetElem(LNode*h, inti, LNode**e)
{
LNode*p=h;
intj=0;
while (p&&j<i)
{
p=p->next;
j++;
}
if (!p)
returnERROR;
*e=p;
returnOK;
}
LNode*GetPrecursor(LNode*h, LNode*e)
{
LNode*p=h;
while (p->next!=e)
p=p->next;
returnp;
}
intmatch(Data*a, Data*b)
{
if (a->rank==b->rank||a->suit==b->suit)
return1;
elsereturn0;
}
voidfreespace(LNode*h)
{
LNode*p;
Data*q;
if (h->next)
{
p=h->next;
while (p->top->next)
{
q=p->top->next;
p->top->next=q->next;
free(q);
}
free(p->top);
freespace(h->next);
}
free(h);
}