码迷,mamicode.com
首页 > 其他好文 > 详细

UVa514 Rails (栈)

时间:2016-08-31 22:04:30      阅读:106      评论:0      收藏:0      [点我收藏+]

标签:

链接:http://acm.hust.edu.cn/vjudge/problem/19641
分析:车厢符合后进先出的原则,因此是一个栈。

 1 #include <cstdio>
 2 #include <stack>
 3 using namespace std;
 4 
 5 const int maxn = 1000 + 10;
 6 
 7 int n, target[maxn];
 8 
 9 int main() {
10     while (scanf("%d", &n) == 1 && n) {
11         while (scanf("%d", &target[1]) && target[1]) {
12             stack<int> s;
13             for (int i = 2; i <= n; i++) scanf("%d", &target[i]);
14             int A = 1, B = 1;
15             bool ok = true;
16             while (B <= n) {
17                 if (A == target[B]) { A++; B++; }
18                 else if (!s.empty() && s.top() == target[B]) { s.pop(); B++; }
19                 else if (A <= n) s.push(A++);
20                 else { ok = false; break; }
21             }
22             printf("%s\n", ok ? "Yes" : "No");
23         }
24         printf("\n");
25     }
26     return 0;
27 }

 

UVa514 Rails (栈)

标签:

原文地址:http://www.cnblogs.com/XieWeida/p/5827615.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!