刘汝佳的题目感觉都是比较难以处理的,就像这道题目,一看数据简直觉得头大。。。加上这个英文我也看的想死

最后看别人博客的题意讲解才知道原来是要移牌。

然后如果熟练的使用stack和手写链表的话,这个题目是不成问题的

#include <iostream>
#include <cstdio>
#include <cstring>
#include <stack>
#define N 100
using namespace std;
struct node{
char ch[];
};
stack<node> arr[N];
int next[N],pre[N];
int t,cnt;
void deletegap()
{
for (int i=;i!=t;i=next[i])
{
if (arr[i].empty())
{
next[pre[i]]=next[i];
pre[next[i]]=pre[i];
return;
}
}
}
bool movement()
{
int i;
for (i=next[];i<t;i=next[i])
{
int f1=pre[pre[pre[i]]];
if (f1>= && f1<t)
{ if (arr[i].top().ch[]==arr[f1].top().ch[] || arr[i].top().ch[]==arr[f1].top().ch[])
{
arr[f1].push(arr[i].top());
arr[i].pop();
return true;
}
}
int f0=pre[i];
if (arr[i].top().ch[]==arr[f0].top().ch[]||arr[i].top().ch[]==arr[f0].top().ch[])
{
arr[f0].push(arr[i].top());
arr[i].pop();
return true;
}
}
return false; }
void solve()
{
while (movement())
{
deletegap();
}
}
int main()
{
t=;
node temp;
while (scanf("%s",temp.ch))
{
if (temp.ch[]=='#')
break;
while (!arr[t].empty())
arr[t].pop();
arr[t].push(temp);
pre[t]=t-;
next[t]=t+;
t++;
if (t==)
{
solve();
int ans=;
for (int i=;i!=t;i=next[i])
ans++;
if (ans==) printf("1 pile remaining: ");
else printf("%d piles remaining: ",ans);
for (int j=;j!=t;j=next[j])
{
if (j) putchar(' ');
printf("%d",arr[j].size());
}
putchar('\n');
t=;
}
}
return ;
}
05-19 05:41