列车厢调度 (25分)

s1,s3都是栈,s2是出栈序列

#include <iostream>
#include <stack>
#include <vector>
#include <string.h>

#define ERROR 0

using namespace std;
/** 先判断,后输出 */
int main()
{
    stack<char> s3,s1;
    std::vector<int> VecOperate;
    //3是栈,1是出容器顺序,2是进容器的顺序
    //用两位数记操作方法
    //1->3 13   1->2 12     3->2  32

    /**读取*/
    char stemp1[30],s2[30];
    cin.get(stemp1,30);
    int n=strlen(stemp1);
    for(int i=n-1;i>=0;i--)
        s1.push(stemp1[i]);

    cin.get();//读取回车
    cin.get(s2,30);
    int is=1 ,find ;

    char *tempLocation;

        for(int i=0; i<n ;i++)//遍历s2
        {
            if( !s3.empty() && s3.top()!=s2[i] && s1.empty() )
            {
                is=ERROR;
                break;
            }
            else if(!s3.empty()&&s3.top()==s2[i]) s2 的数据存在于 s3顶
            {
                VecOperate.push_back(32);
                s3.pop();
            }
            else if( (!s3.empty()&&s3.top()!=s2[i])||s3.empty() )// s2 的数据可能存在于 s1 中
            {
                //该字符前面的挪到s3,最后将该数字挪入
                find=0;
                while( !s1.empty()&&find==0 )//不为空
                {
                    if(s1.top()==s2[i])//发现元素
                    {
                        VecOperate.push_back(12);
                        s1.pop();
                        find=1;
                        break;
                    }
                    else//未发现元素
                    {
                        VecOperate.push_back(13);
                        s3.push(s1.top());
                        s1.pop();
                    }
                }
                //挪动
            }
        }
    if(is==ERROR)
        printf( "Are you kidding me?");
    else
    {
        for(int i=0;i<VecOperate.size();i++)
        {//1->3 13   1->2 12     3->2  32
            if(VecOperate[i]==13)
                printf("1->3");
            else if(VecOperate[i]==12)
                printf("1->2");
            else if(VecOperate[i]==32)
                printf("3->2");
            printf("\n");
        }
    }
    return 0;
}
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值