UVA-514 - Rails
03 Jun 2018
- UVa Online Judge 解題結果請於 Submit 後,參閱 uHunt。
- 如果你有任何建議與指教,歡迎於下方留言一起討論喔!
題意概要
題目給定一個數列,請判斷是否可能由 到 的順序放進堆疊 (Stack),按照自訂順序 pop 之後的數列剛好為題目給定的數列。
- 分析:本題為簡單的資料結構模擬問題。由題目的意思,可以直接模擬「堆疊 (Stack)」存取資料的過程,在從堆疊中 pop 資料時,每次只需判斷堆疊是否為空即可。
Input
The input file 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 described above. In each of the next lines of the block there is a permutation of , , …, . The last line of the block contains just 0
. The last block consists of just one line containing 0
.
Output
The output file contains the lines corresponding to the lines with permutations in the input file. A line of the output file contains Yes
if it is possible to marshal the coaches in the order required on the corresponding line of the input file. Otherwise it contains No
. In addition, there is one empty line after the lines corresponding to one block of the input file. There is no line in the output file corresponding to the last “null” block of the input file.
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