1. 程式人生 > >UVa 514 Rails(棧的應用)

UVa 514 Rails(棧的應用)

clu 題目 是否 一個 else uva 棧操作 for IV

題目鏈接:

https://cn.vjudge.net/problem/UVA-514

 1 /*
 2 問題
 3 輸入猜測出棧順序,如果可能輸出Yes,否則輸出No
 4 
 5 解題思路
 6 貌似沒有直接可以判定的方法,紫書上給出的方法是在進行一個入棧出棧操作,看是否能夠構成原始的入棧序列。 
 7 */ 
 8 #include<cstdio>
 9 #include<stack>
10 
11 using namespace std;
12 int ok(int a[],int n);
13 int main()
14 {
15     freopen("E:\\testin.txt
","r",stdin); 16 int a[1100]; 17 int n,i,j; 18 stack<int> s; 19 while(scanf("%d",&n) == 1 && n != 0){ 20 while(1){ 21 for(i=1;i<=n;i++){ 22 scanf("%d",&a[i]); 23 if(a[1] == 0) 24 break; 25 }
26 if(a[1] == 0) 27 break; 28 29 int A=1,B=1,ok=0; 30 while(B <= n){ 31 if(A == a[B]){ 32 A++;B++; 33 }else if(!s.empty() && s.top() == a[B]){ 34 s.pop();
35 B++; 36 }else if(A <= n){ 37 s.push(A++); 38 }else { 39 ok=1; 40 break; 41 } 42 } 43 44 if(ok) 45 printf("No\n"); 46 else 47 printf("Yes\n"); 48 } 49 printf("\n"); 50 } 51 return 0; 52 }

UVa 514 Rails(棧的應用)