hdu1022-火车-栈的使用

今天做的水题有点多,犹豫要不要发出来,不管了,明天再说……

想起大二下的时候数据结构为了一个栈的写费了多长的时间,现在用STL的格式简洁明快。所以说:不要重复造轮子。

简直要哭了。

随便写写吧:完整代码如下,一些小的感受也保存了起来,明天再好好练习:

#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<stack>
using namespace std;
//1、2、3表示的只是入栈的顺序,没有说什么时候会出来
int main(void)
{
    int n;
    int i,j,k=0;
    int count=0;
    char temp[1000];
    int order[1000],target[1000];
    while(cin>>n){
        bool flag=true;
        cin>>temp; //数组,string也可以
        for(i=0;i<n;i++)
            order[i]=temp[i]-48; //完成char数字向int型数字的转化
        cin>>temp;
        for(i=0;i<n;i++)
            target[i]=temp[i]-48;
        stack<int> s;
        int A=0,B=0;
        while(B<n){
            if(!s.empty() && s.top()==target[B])
            {
                s.pop();
                B++;
                ans[k++]=1; //out
            }
            else if(A<n)
            {
                s.push(order[A]);
                A++;
                ans[k++]=0;
            }
            else {
                flag=false;
                break;
            }
        }
     if(!flag) cout<<"no."<<endl;
     else for(i=0;i<=k-1;k++) 
         if(ans[k]) cout<<"in"<<endl;
     else cout<<"out"<<endl;
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值