1. 程式人生 > 實用技巧 >POJ 1363 Rails

POJ 1363 Rails

題目傳送門:POJ 1363

Descirbe:
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

題目大意:

這道題第一次看中文題目的時候,都沒看懂輸入輸出說的啥,可能是翻譯的問題吧(應該不會是我的問題)。大概就是,給你一列數,問你能否通過堆疊,將這列數變成指定的排列。輸入有若干樣例,每個樣例先給個n,然後有若干行,每行是一個1-n的排列,每個樣例最後一行是0,代表該樣例結束,要求對每個樣例的每個排列輸出Yes或者No,每個樣例間加個空行。

比如:n是6,給你一個排列6 5 4 3 2 1,那麼這個應該輸出Yes,因為,將1-n就是1-6按順序存入棧,然後再一個一個彈出,就是6 5 4 3 2 1了,所以這個排列是可以形成的。

解題思路:

大概思路也比較簡單,就是每次入棧前都檢查一下,如果跟當前排列的數相等,直接pop,否則就入棧,最後如果棧為空,就是Yes(都出棧了,說明排列可以形成) 否則為No

AC程式碼:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <stack> // 別忘了標頭檔案
 4 using namespace std;
 5 int main()
 6 {
 7     int n,i,cnt;
 8     while(~scanf("%d",&n) && n)
 9     {
10         int a[n+1]; // 構建陣列,+1是為了從1開始,純屬習慣
11         i = 1; // 輸入時用的下標
12         cnt = 1; // 檢查時用的下標
13         while(~scanf("%d",&a[i]) && a[i])
14         {
15             i++;
16             stack<int> s;
17             for( ; i <= n; i++) scanf("%d",&a[i]); // 輸入,不多說了
18             for(int j = 1; j <= n+1; j++) // 這裡注意了,為什麼是n+1,就好比上面舉的那個n=6例子
19             // 前面入棧的都不符合,到最後才相等,所以就得多一個用來判斷
20             {
21                 if(s.empty() && j <= n) {s.push(j);continue;} // 棧空就直接入棧
22                 if(j == a[cnt]) {cnt++; continue;} // 相等的話,也不用入棧了,直接跳過
23                 while(!s.empty() && s.top() == a[cnt]) {s.pop();cnt++;} // 應該很好理解
24                 if(j <= n) s.push(j); // 一直別忘了if的判斷
25             }
26             if(s.empty()) printf("Yes\n"); // 按要求輸出
27             else printf("No\n");
28             // 注意一些變數要歸位,不然下次判斷會出錯
29             i = 1;
30             cnt = 1;
31         }
32         printf("\n");
33     }
34 }

小結: 這個題就是熟練一下棧的操作

更新一下:如果堆疊為空,s.top() 會有問題,所以檢視棧頂元素前要注意判空