[POJ] [1363] Rails

Problem Description

Loading...

Problem Analysis

The idea is the same as that problem above
[HDU][1022]Train Problem I](http://blog.csdn.net/u011493189/article/details/50805953)
more or less

This one only needs to determine whether the pop sequence is possible
and does not require printing the process
so irrelevant variables such as the flag array are omitted

cpp
 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
#include<stdio.h>
#include<stack>
using namespace std;
int main() {
	int n;
	bool flag=false;
	while(scanf("%d",&n),n) {
		if(flag)
			printf("\n");
		int a[1200];
		while(scanf("%d",&a[1]),a[1]) {
			stack<int>s;
			for(int i=2; i<=n; i++)
				scanf("%d",&a[i]);
			int j=1;
			for(int i=1; i<=n; i++) {
				s.push(i);
				while(!s.empty()&&s.top()==a[j]) {
					s.pop();
					j++;
				}
			}
			if(j-1==n)
				printf("Yes\n");
			else
				printf("No\n");
		}
		flag=true;
	}
	return 0;
}

Problem address: [POJ][1363]Rails](http://poj.org/problem?id=1363)

This post is licensed under CC BY-NC-SA 4.0. If it helped, or if you have anything to say, the comment box below is open.
Page views —
Unique visitors —

Loading...