2014年12月29日星期一

[UVa] 514 - Rails

卡了很久
因為輸出的 "Yes" 寫成 "YES" ...

Stack基本題
已有通盤的瞭解

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <iostream>
#include <stack>
using namespace std;
int main(){
  freopen("input.txt","r",stdin);
  int n, B[1001];
  int tmp=0;
  while(scanf("%d",&n)==1){
    if(n==0)break;
    while(1){
      memset(B,0,sizeof(B));
      stack<int> s;
      for(int i=0;i<n;i++){
        scanf("%d",&B[i]);
        if(B[i]==0)break;
      }
      if(B[0]==0){
        puts("");
        break;
      }
      int flag=0,A=1,ok=1; 
      while(flag<n){ 
        if(A==B[flag]){
          A++;
          flag++;
        }
        else if(!s.empty() && s.top()==B[flag]){
          flag++;
          s.pop();
        }
        else if(A<n){
         s.push(A);
         A++;
        }else {
          break;
        }
      }
      if(s.empty())printf("Yes");else printf("No");
      puts("");
    }

  }
  return 0;
}

沒有留言: