现在的位置: 首页 > 综合 > 正文

HOJ1022

2013年02月28日 ⁄ 综合 ⁄ 共 1529字 ⁄ 字号 评论关闭
*------------------这是我在年初在HOJ上做的题,现在摆上来,原文链接:http://user.qzone.qq.com/316516903#!app=2&via=QZ.HashRefresh&pos=1363524166 当时还不会用STL,所以用栈解决,留心数组越界和空栈的问题;做好进栈,出栈的操作。正是因为数组越界空栈RE5次,忘记出栈WA3次---------------------*/
#include<iostream>
using namespace std;
#include<string>
void main()
{
    int n;             // 火车的数量
    string o1,o2;              //进站顺序,出站顺序
    re:
    while(cin>>n>>o1>>o2){
        char stack[9];
string order[18];         //stack保存停留在站中的火车,order保留出站入站记录
        int top=0,index=-1,sq=0;              //top为站中火车数量,index为o1中已进站的火车数量,sq为火车出入站记录的数量
        for(int i=0;o1[i]!=o2[0];i++){
            stack[top++]=o1[i];                      //o1中的火车一直进站,直至要进站的火车恰好是第一个出站的火车
            index=i;
            order[sq++]="in";                       //保存进站记录
        }
        index++;
        order[sq++]="in";order[sq++]="out";                //保存第一个进站火车的进站,出站记录
        for(int i=1;i<n;i++){                                //o2的火车
            if(top>0){                                      //如果栈非空,也就是站内有火车停留
              if(o2[i]==stack[top-1]){                          //如果栈顶刚好是要出站的火车序号
                  top--;  
                  order[sq++]="out";
                  continue;
                }
              for(int j=0;j<top-1;j++)                                     //栈顶不是刚好要出站的火车序号,则检查正要出站的火车栈中是否在栈内
                  if(stack[j]==o2[i]){cout<<"No."<<endl<<"FINISH"<<endl;goto re;}      //如果在栈内,因为此火车不是在栈顶,所以无法出栈。此次test失败,重新进行下次test
            }
            for(int j=index+1;o1[j]!=o2[i];j++){               //如果要出站的火车一直没进站,则排在此火车前的火车全部进站。
                stack[top++]=o1[j];
                index=j;
                order[sq++]="in";
            }
            order[sq++]="in";order[sq++]="out";index++;
        }
        cout<<"Yes."<<endl;                  //end of outter for
        for(int i=0;i!=sq;i++)              //输出解决方案
            cout<<order[i]<<endl;
        cout<<"FINISH"<<endl;
    }
}

抱歉!评论已关闭.