首页 > 其他 > 详细

HDOJ1022(模拟栈)

时间:2015-07-31 12:44:14      阅读:135      评论:0      收藏:0      [点我收藏+]

贪心法:

若栈为空或者栈顶元素不等于or2[j],则入栈,否则出栈。

#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
    int n;
    char or1[10]={\0};
    char or2[10]={\0};
    
    while(scanf("%d %s %s",&n,or1,or2)!=EOF)
    {
        int top=-1;
        char stack[10];
        const char *op[20];
        int cnt=0;
        int i=0;
        int j=0;
        while(j<n)
        {
            if(top==-1||stack[top]!=or2[j])
            {
                if(i>=n)
                    break;
                stack[++top]=or1[i++];
                op[cnt++]="in";
            }
            if(stack[top]==or2[j])
            {
                top--;
                op[cnt++]="out";
                j++;
            }
        }
        
        if(top==-1)
        {
            printf("Yes.\n");
            for(int i=0;i<cnt;i++)
                printf("%s\n",op[i]);
        }
        else
        {
            printf("No.\n");
        }
        
        printf("FINISH\n");
    }
    
    return 0;
}

 

HDOJ1022(模拟栈)

原文:http://www.cnblogs.com/program-ccc/p/4691725.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!