本文主要是介绍POJ 1363 解题报告,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
这道题不难但是题目很难理解。看了测试样例才明白,如果入栈顺序是递增的:1,2,3,4,5. 那么给出一个出栈顺序,比如5,4,1,2,3,判断这个出栈顺序是否可能。
我这里就是按照题意模拟的。比如碰到5,就将小于等于5的都入栈(1,2,3,4,5),然是将5出栈(判断这时栈顶一定是5),同样地,之后遇到4,已经没有什么可入栈了,栈顶是4,出栈,再遇到1,同样没什么可入栈的,栈顶元素是3,不是1,说明不可能。
thestoryofsnow | 1363 | Accepted | 156K | 110MS | C++ | 1354B |
/*
ID: thestor1
LANG: C++
TASK: poj1363
*/
#include <iostream>
#include <fstream>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <limits>
#include <string>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <algorithm>
#include <cassert>using namespace std;int main()
{int N;scanf("%d", &N);while (N){int num;scanf("%d", &num);// the number of numbers inputint cnt = 1;// fprintf(stderr, "num: %d, cnt: %d\n", num, cnt);if (!num){printf("\n");scanf("%d", &N);continue;}int instacktop = 1;stack<int> outstack;bool flag = true;while (true){while (instacktop <= N && instacktop <= num){outstack.push(instacktop);instacktop++;}if (outstack.empty() || outstack.top() != num){flag = false;break;}outstack.pop();if (cnt == N){break;}scanf("%d", &num);cnt++;// fprintf(stderr, "num: %d, cnt: %d\n", num, cnt);}// get the rest of numberswhile (cnt < N){scanf("%d", &num);cnt++;// fprintf(stderr, "num: %d, cnt: %d\n", num, cnt);}if (flag){printf("Yes\n");}else{printf("No\n");}}return 0;
}
这篇关于POJ 1363 解题报告的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!