题意:
给出一棵树的先序遍历,但是在离开该点时还会再输出一次。每个节点用一个大写字母表示,要求输出每个节点的度数。
题解:
因为一个点先后输出了两次,所以可以用一个栈记录当前的最后一个点。
//Time:12ms
//Length:764B
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std;
#define MAXN 505
char str[MAXN];
int sta[MAXN],stop,cnt[MAXN];
int main()
{
//freopen("/home/moor/Code/input","r",stdin);
int ncase;
scanf("%d",&ncase);
for(int hh=1;hh<=ncase;++hh)
{
scanf("%s",str);
stop=0;
memset(cnt,0,sizeof(cnt));
for(int i=0;str[i];++i)
{
if(stop==0||sta[stop-1]!=str[i])
++cnt[stop?sta[stop-1]:0],sta[stop++]=str[i],++cnt[sta[stop-1]];
else --stop;
}
--cnt[sta[0]];
printf("Case %d\n",hh);
for(int i='A';i<='Z';++i)
if(cnt[i])
printf("%c = %d\n",i,cnt[i]);
}
return 0;
}