出栈顺序判断

来源:互联网 发布:淘宝联盟活动推广 编辑:程序博客网 时间:2024/04/29 09:32
#include<iostream>#include<fstream>#include<stack>#include<sstream>using namespace std;int main(){ifstream in("stack.txt");for (int n, line = 0; in >> n&&n&&in.ignore();){cout << (line++ ? "\n" : "");for (string s; getline(in, s) && s != "0";){istringstream sin(s);stack<int> st;for (int last = 0, coach; sin >> coach; st.pop()){for (int p = last + 1; p <= coach; p++)st.push(p);if (last < coach)last = coach;if (st.top ()!= coach)break;}cout << (!sin ? "YES\n" : "NO\n");}}}

测试样例:

5
3 2 1 5 4
5 4 1 2 3
0
6
6 5 4 3 2 1
0
0

每一组第一个数据代表栈内数据数目,以0结尾

输出:

YES

NO


YES


0 0
原创粉丝点击