现在的位置: 首页 > 例题 > 正文

铁轨

2011年03月31日 例题 ⁄ 共 163字 ⁄ 字号 暂无评论

栈的典型例子

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <stack>
 
const int MAXN = 1000 + 10;
int n, target[MAXN];
 
using namespace std;
 
int main(int argc, char *argv[])
{
	while (scanf("%d", &n) == 1) {
		stack<int> s;
		int A = 1, B = 1;
 
		for (int i=1; i<=n; i++)
			scanf("%d", &target[i]);
		int ok = 1;
		while (B <= n) {
			if (A==target[B]) {
				A++; B++;
			}
			else if (!s.empty() && s.top()==target[B]) {
				s.pop(); B++;
			}
			else if (A<=n) s.push(A++);
			else {
				ok = 0; break;
			}
		}
		printf("%sn", ok ? "Yes":"No");
	}	
	return 0;
}
【上篇】
【下篇】

抱歉!评论已关闭.