标签:des style blog http os 2014
Description
Input
Output
Sample Input
5 1 2 3 4 5 5 4 1 2 3 0 6 6 5 4 3 2 1 0 0
Sample Output
Yes No Yes
题意:要按1-n序列入栈,求给出的序列是否是合理的出栈序列
思路:模拟栈的过程,两个指针,一个指向当前的出栈序列位置,一个指向入栈序列的位置,当当前出栈数是大于入栈的数的话就要将入栈的数压入栈,相等就移除,小的话就判断当前入栈数和栈顶的大小
#include <iostream> #include <cstring> #include <cstdio> #include <algorithm> #include <stack> using namespace std; const int MAXN = 1005; stack<int> st; int n, num[MAXN]; int main() { while (scanf("%d", &n) != EOF && n) { while (scanf("%d", &num[1]) && num[1]) { for (int i = 2; i <= n; i++) scanf("%d", &num[i]); int ans = 1; int cur = 1, cnt = 1; while (!st.empty()) st.pop(); while (cur <= n) { if (num[cur] > cnt) st.push(cnt++); else if (num[cur] == cnt) { cur++; cnt++; } else if (!st.empty()) { if (st.top() != num[cur]) { ans = 0; break; } st.pop(); cur++; } } if (ans) printf("Yes\n"); else printf("No\n"); } printf("\n"); } return 0; }
POJ - 1363 Rails,布布扣,bubuko.com
标签:des style blog http os 2014
原文地址:http://blog.csdn.net/u011345136/article/details/37901331