PAT1051

题目链接:http://pat.zju.edu.cn/contests/pat-a-practise/1051

遍历序列,基于这样的思想,例如 3 2 1 7 5 6 4 开始是3 则1 2 3入栈 并标记,然后判断栈有没有超出给定大小,然后看s.top() == 3 否,若都为true 则走到2,若中间有一项不符则,这个序列式impossible ,然成功遍历一遍,则这个序列式可以的。

 

 1 #include<iostream>
 2 #include<vector>
 3 #include<stack>
 4 using namespace std;
 5 int main()
 6 {
 7     int M,N,K;
 8     while(cin>>M>>N>>K)
 9     {
10         vector<int> s(N);
11         vector<vector<int>> check(K, s);
12         for(int i=0; i<K; ++i)
13             for(int j=0; j<N; ++j)
14                 cin>>check[i][j];
15         for(int i=0; i<K; ++i)
16         {
17             bool flag(false);
18             /*记录栈中元素*/
19             stack<int> s;
20             /*用来记录是否在栈中*/
21             vector<int> inorout(N+1, 0);
22             for(int j=0; j<N; ++j)
23             {
24                 for(int k=1; k<=check[i][j]; ++k)
25                     if(inorout[k] == 0)
26                     {
27                         inorout[k]=1;
28                         s.push(k);
29                     }
30                 if(s.size() > M)
31                 {
32                     cout<<"NO"<<endl;
33                     flag=true;
34                     break;
35                 }
36                 if(s.top() == check[i][j])
37                     s.pop();
38                 else
39                 {
40                     cout<<"NO"<<endl;
41                     flag=true;
42                     break;
43                 }
44             }
45             if(flag == false)
46                 cout<<"YES"<<endl;
47         }
48     }
49     return 0;
50 }

 

转载于:https://www.cnblogs.com/bochen-sam/p/3386984.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值