日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

POJ1363Rails队列和栈应用

發布時間:2025/4/5 编程问答 17 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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組合使用。

  • 隊列queue保存待判斷的序列,比如樣例中的5 4 1 2 3
  • 1,2,,,n按照順序進棧stack,判斷棧頂元素top()是否等于隊首元素front(),如果相等,則說明有合法的可能性,同時彈出隊首元素和棧頂元素
  • 最后如果棧空,則說明待判斷的序列是合法的序列。
  • 代碼說明:
    這里的函數參數為隊列,需要傳進來一個隊列。看似多此一舉,實則鍛煉自己形參形式的轉化能力。
    是不是還沒用過隊列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; }

    總結:
    該題考查基本的棧的應用。
    該題需要注意輸入輸出的格式。

    5 1 2 3 4 5 5 4 1 2 3 0 6 6 5 4 3 2 1 0 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队列和栈应用的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。