HDU

来源:互联网 发布:hp3055扫描仪驱动软件 编辑:程序博客网 时间:2024/06/06 00:35

题目链接 : http://acm.hdu.edu.cn/showproblem.php?pid=5818

就是模拟栈的操作,也不用想什么巧妙的办法,直接来就可以过了。

#include<iostream>#include<stdio.h>#include<string.h>using namespace std;const int maxn = 100100;int n,st[maxn],belone[maxn],vis[maxn];int main(){    int cas = 0;    while(~scanf("%d",&n)&&n)    {        printf("Case #%d:\n",++cas);        char s1[10],c1,c2;        int num , cnt = 0;        memset(st,0,sizeof st);        memset(vis,0,sizeof vis);        memset(belone,0,sizeof belone);        for(int i=0;i<n;i++)        {            scanf("%s",s1);            if(s1[1]=='u')            {                scanf("%s%d",&c1,&num);                st[cnt] = num;                belone[cnt++] = c1 - 'A' + 1;            }            else if(s1[1]=='o')            {                scanf(" %c",&c1);                int be;                be = c1 - 'A' + 1;                int pos = cnt;                while(pos--)                {                    if(vis[pos])                        continue;                    if(belone[pos]==3||belone[pos]==4)                        break;                    if(belone[pos]==be)                    {                        vis[pos] = 1;                        printf("%d\n",st[pos]);                        break;                    }                }                if(belone[pos]==3||belone[pos]==4)                {                    while(pos--)                    {                        if(vis[pos]||belone[pos]==3||belone[pos]==4)                            continue;                        vis[pos] = 1;                        printf("%d\n",st[pos]);                        break;                    }                }            }            else            {                scanf(" %c %c",&c1,&c2);                belone[cnt++] = c1 - 'A' + 3;            }        }    }    return 0;}