题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805427332562944
这题思路参考了一下算法笔记,其他都自己写的,居然和算法笔记上的代码差不多
唯一一个坑点是26行注释的代码会报错,我也是醉了,一定要判空条件在前才可以正确通过(否则有可能为空,top()就为无效迭代器了),以后尽量都先这样写吧
代码
#include<stack>#include<vector>#include<algorithm>#include<iostream>using namespace std;stack<int> s;vector<int> input;int main(){int m, n, k;scanf("%d%d%d", &m, &n, &k);//m是栈的容量,n是序列的长度,k是有多少序列要检查input.resize(n + 1);for(int i = 0; i < k; i++){while(!s.empty()) s.pop();bool flag = true;int now = 1;for(int j = 1; j <= n; j++) cin>>input[j];//初始化判断序列for(int j = 1; j <= n; j++){//开始入栈s.push(j);if(s.size() > m){flag = false;break;}while(!s.empty() && s.top() == input[now]){//while(s.top() == input[now] && !s.empty()){s.pop();now++;}}if(s.empty() && flag){printf("YES\n");} else {printf("NO\n");}}return 0;}
