hdu 1022 Train problem I

Problem Description
As the new term comes, the Ignatius Train Station is very busy nowadays. A lot of student want to get back to school by train(because the trains in the Ignatius Train Station is the fastest all over the world ^v^). But here comes a problem, there is only one railway where all the trains stop. So all the trains come in from one side and get out from the other side. For this problem, if train A gets into the railway first, and then train B gets into the railway before train A leaves, train A can't leave until train B leaves. The pictures below figure out the problem. Now the problem for you is, there are at most 9 trains in the station, all the trains has an ID(numbered from 1 to n), the trains get into the railway in an order O1, your task is to determine whether the trains can get out in an order O2.
 

Input
The input contains several test cases. Each test case consists of an integer, the number of trains, and two strings, the order of the trains come in:O1, and the order of the trains leave:O2. The input is terminated by the end of file. More details in the Sample Input.
 
Output
The output contains a string "No." if you can't exchange O2 to O1, or you should output a line contains "Yes.", and then output your way in exchanging the order(you should output "in" for a train getting into the railway, and "out" for a train getting out of the railway). Print a line contains "FINISH" after each test case. More details in the Sample Output.
 
Sample Input
3 123 321 3 123 312
 
Sample Output
Yes. in in in out out out FINISH No. FINISH ------------------------------------------------------------------------------------
用數組模擬棧,
在輸出 in out 時,用了c++中的string 對象,在這裏用很方便;
#include<string.h> #include<stdio.h> #include<string> #include<string.h> #include<iostream> using namespace std; int main() {  char in[1024],out[1024],stack[1024];  int i,j,top,n;  memset(in,0,sizeof(in));  memset(out,0,sizeof(out));  memset(stack,0,sizeof(stack));  while(scanf("%d%s%s",&n,in,out)!=EOF)  {   top=0;   i=j=0;   string s = "";   while(i<=n&&j!=n)   {    if(top!=0&&stack[top-1]==out[j])// 當前棧中有元素時,就和out數組中的當前元素比較,判斷是否相同,相同則出棧;    {     top--;     s += "out\n";     j++;    }    else // 棧爲空,或者當前棧頂元素與out數組當前元素不相同時,則,將in數組當前元素入棧;    {     s += "in\n";     stack[top++] = in[i];     i++;    }   }   if(top==0)   {    printf("Yes.\n");    cout << s << "FINISH\n";   }   else   {    printf("No.\nFINISH\n");   }   memset(in,0,sizeof(in));   memset(out,0,sizeof(out));   memset(stack,0,sizeof(stack));  }  return 0; }
 
 
#include <stdio.h>   #include <iostream>   #include <string>   #include <stack>     using namespace std;    int main(int ac, char** av)  {      int n;            while ( cin >> n ) {          char come[1024];          char leave[1024];          int ci = 0;          int li = 0;                    stack<char> sta;          sta.push('0');            scanf("%s", come);          scanf("%s", leave);            string s("");            while ( (ci <= n) && (li !=n) ) {                  // ci 可能全部壓棧,所以要比數組長度大1               if ( sta.top() == leave[li] ) {                  // 先在棧中尋找                   sta.pop();                  s += "out\n";                  ++li;              } else {                  if ( come[ci] != leave[li] ) {                  // 在進站中尋找 == 出站                   sta.push(come[ci]);                  s += "in\n";                  ++ci;                  } else {                      ++li;   // 找到                       ++ci;   //                        s += "in\nout\n";                      // 進棧出棧;忽略                   }              }                                            }          if ( sta.top() == '0' ) {              printf("Yes.\n");              cout << s << "FINISH\n";          } else {             printf("No.\nFINISH\n");          }        }        return 0;  } 
 
發佈了62 篇原創文章 · 獲贊 6 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章