HDU1022

传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1022

给你两串数 输出让第一串变成第二串的进出栈过程,如果能,输出yes,并输出进出栈的过程,如果不能,输出no。在每种情况最后输出FINISH

栈的基本运用吧!用一个vector数组记录进出栈情况。

#include <iostream>
#include <stack>
#include<vector>
#include<string>
#include<cstring>
using namespace std;

int n;
char str1[100],str2[100];
int main()
{
    vector<string> p;   
    while(cin>>n)
    {
        p.clear();
        stack<int> s;
        int A=0,B=0;
        scanf("%s%s",str1,str2);
        int ok=1;
        while(B<n)
        {
            if(str1[A]==str2[B]) 
            {
                A++;B++;
                p.push_back("in");
                p.push_back("out");
            }
            else if(!s.empty() && s.top()==str2[B])
            {
                s.pop();
                p.push_back("out");
                B++;
            }
            else if(A<n)
            {
                s.push(str1[A++]);
                p.push_back("in");
            }
            else
            {
                ok=0;
                break;
            }
        }
        printf("%s\n",ok?"Yes.":"No.");
        if(ok)
        for(int i=0;i<p.size();i++)
            cout<<p[i]<<endl;
        cout<<"FINISH\n";
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值