POJ1363Rails队列和栈应用
題目
POJ-1363 Rails
Description
There is a famous railway station in PopPush City. Country there is incredibly hilly. The station was built in last century. Unfortunately, funds were extremely limited that time. It was possible to establish only a surface track. Moreover, it turned out that the station could be only a dead-end one (see picture) and due to lack of available space it could have only one track.
The local tradition is that every train arriving from the direction A continues in the direction B with coaches reorganized in some way. Assume that the train arriving from the direction A has N <= 1000 coaches numbered in increasing order 1, 2, …, N. The chief for train reorganizations must know whether it is possible to marshal coaches continuing in the direction B so that their order will be a1, a2, …, aN. Help him and write a program that decides whether it is possible to get the required order of coaches. You can assume that single coaches can be disconnected from the train before they enter the station and that they can move themselves until they are on the track in the direction B. You can also suppose that at any time there can be located as many coaches as necessary in the station. But once a coach has entered the station it cannot return to the track in the direction A and also once it has left the station in the direction B it cannot return back to the station.
Input
The input consists of blocks of lines. Each block except the last describes one train and possibly more requirements for its reorganization. In the first line of the block there is the integer N described above. In each of the next lines of the block there is a permutation of 1, 2, …, N. The last line of the block contains just 0.
The last block consists of just one line containing 0.
Output
The output contains the lines corresponding to the lines with permutations in the input. A line of the output contains Yes if it is possible to marshal the coaches in the order required on the corresponding line of the input. Otherwise it contains No. In addition, there is one empty line after the lines corresponding to one block of the input. There is no line in the output corresponding to the last ``null’’ block of the input.
Sample Input
5
1 2 3 4 5
5 4 1 2 3
0
6
6 5 4 3 2 1
0
0
Sample Output
Yes
No
Yes
題目翻譯
有數字序列1,2,…n這樣嚴格順序入棧。由于進棧出棧時間不同,所以有很多合法的組合。只有符合棧的規則的,即后進先出的才是合法的。
下面舉例說明不合法:1,2,3按照順序入棧,如果出現1,2這樣的出棧即是不合法。因為1,2 不滿足后進先出的規則,應該是2在前1再后出棧。
解題思路
采用隊列queue和棧stack組合使用。
代碼說明:
這里的函數參數為隊列,需要傳進來一個隊列??此贫啻艘慌e,實則鍛煉自己形參形式的轉化能力。
是不是還沒用過隊列queue作為函數的形參呢?
代碼
#include<iostream> #include<queue> #include<stack>using namespace std;bool check(queue<int> &order) {stack<int> S;int n=order.size();//長度for(int i=0;i<n;++i){S.push(i+1);//入棧while(!S.empty()&&order.front()==S.top())//隊首==棧頂{order.pop();//同時彈出S.pop();}} if(!S.empty())return false;return true; //棧為空,說明合法 }int main() {queue<int> Q;int T,temp1,temp,a[10000];while(1){cin>>T;//讀入位數if(T==0) break;while(1){cin>>a[0];//讀入一行數據if(a[0]==0) break; //第一個數據為零,則退出到上層whileQ.push(a[0]);for(int i=1;i<T;++i){cin>>a[i];//借助數組,進入隊列Q.push(a[i]);}if(check(Q))//調用判斷合法函數cout<<"Yes"<<endl;elsecout<<"No"<<endl;while(!Q.empty())//清空隊列Q.pop();}cout<<endl;//輸出空行,跟位數T是一組。}return 0; }總結:
該題考查基本的棧的應用。
該題需要注意輸入輸出的格式。
這樣的輸入該怎么處理呢?
可以看出這里結束錄入的條件是輸入0 回車 0 回車。即:
0 0同時還要求是同一位數n的數據輸出結束后,輸出一個空行。
while(1) {cin>>n;//位數if(n==0) break;//退出該層循環//下面讀入每一行數據,這時候要考慮開頭為0,則為結束的標志。while(1){cin>>a[0];if(a[0]==0) break;for(int i=1;i<n;++i)cin<<a[i];} cout<<endl;//這里和位數n并列 }希望本次總結對你有幫助。
總結
以上是生活随笔為你收集整理的POJ1363Rails队列和栈应用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Leetcode155最小栈
- 下一篇: 军校毕业后如何分配会去部队吗