码迷,mamicode.com
首页 > 其他好文 > 详细

HDOJ1022(模拟栈)

时间:2015-07-31 12:46:45      阅读:84      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!