Inspirational as few code to do efficient expression.
Submitted (Subject) link → the UVa-514
This is the essence of the problem: given "Drawing order", it is determined whether a given sequence the stack.
A little problem like this:
So the idea is similar:
Ideas:
Array stores train scheduling order, stack storage stack normal order, if head = stack array, the array moves to the next one, pop the top element
after the traversal is complete, if the stack is empty, then the full schedule is completed, output Yes
important point:
1, the output is not Yes YES! (Beginning wrong, crying in the toilet halo)
2, empty lines between each set of results, a blank line after the final result can not be omitted.
3, input to this question is out of mind, enter a n, as long as the line does not appear the next n 0, you can always enter it. So you can try to bottom-up programming method: write your main idea, and finally supplemental input format.
Code:
#include<bits/stdc++.h>
using namespace std;
int main() {
int a[1010];
int n; while((cin >> n) && n) {
while(cin >> a[1] && a[1]) {
for(int i = 2; i <= n; i++) { cin >> a[i]; }
stack<int> s2;
int j = 1;
for(int i = 1; i <= n; i++) {
s2.push(i);
while((!s2.empty()) && (s2.top() == a[j])) { j++; s2.pop(); }
}
cout << ((s2.empty()&&(j==n+1)) ? "Yes\n" : "No\n");
}
cout << "\n";
}
return 0;
}
reward:
1, bottom-up programming methods.
2, the use of the stack to improve proficiency.