UVa 514 (stack的使用) Rails
练习一下stack的使用,还有要注意一下输入的格式,看了好长时间没懂。
1 //#define LOCAL 2 #include <iostream> 3 #include <cstdio> 4 #include <cstring> 5 #include <stack> 6 using namespace std; 7 8 const int maxn = 1000 + 10; 9 int n, target[maxn]; 10 11 int main(void) 12 { 13 #ifdef LOCAL 14 freopen("514in.txt", "r", stdin); 15 #endif 16 17 while(scanf("%d", &n) == 1 && n) 18 { 19 while(scanf("%d", &target[1]) == 1 && target[1]) 20 { 21 for(int i = 2; i <= n; ++i) scanf("%d", &target[i]); 22 stack<int> s; 23 int A = 1, B = 1; 24 int ok = 1; 25 while(B <= n) 26 { 27 if(A == target[B]) { A++; B++; } 28 else if(!s.empty() && s.top() == target[B]) { s.pop(); B++; } 29 else if(A <= n) s.push(A++); 30 else { ok = 0; break; } 31 } 32 printf("%s\n", ok ? "Yes" : "No"); 33 } 34 printf("\n"); 35 } 36 37 return 0; 38 }