pat02-线性结构4. Pop Sequence (25)

02-线性结构4. Pop Sequence (25)

时间限制
100 ms
内存限制
65536 kB
代码长度限制
8000 B
判题程序
Standard
作者
CHEN, Yue

Given a stack which can keep M numbers at most. Push N numbers in the order of 1, 2, 3, ..., N and pop randomly. You are supposed to tell if a given sequence of numbers is a possible pop sequence of the stack. For example, if M is 5 and N is 7, we can obtain 1, 2, 3, 4, 5, 6, 7 from the stack, but not 3, 2, 1, 7, 5, 6, 4.

Input Specification:

Each input file contains one test case. For each case, the first line contains 3 numbers (all no more than 1000): M (the maximum capacity of the stack), N (the length of push sequence), and K (the number of pop sequences to be checked). Then K lines follow, each contains a pop sequence of N numbers. All the numbers in a line are separated by a space.

Output Specification:

For each pop sequence, print in one line "YES" if it is indeed a possible pop sequence of the stack, or "NO" if not.

Sample Input:
5 7 5
1 2 3 4 5 6 7
3 2 1 7 5 6 4
7 6 5 4 3 2 1
5 6 4 3 7 2 1
1 7 6 5 4 3 2
Sample Output:
YES
NO
NO
YES
NO

提交代码

 

 1 #include<cstdio>
 2 #include<algorithm>
 3 #include<stack>
 4 #include<queue>
 5 #include<vector>
 6 #include<iostream>
 7 using namespace std;
 8 int line[1005];
 9 int main(){
10     //freopen("D:\\input.txt","r",stdin);
11     int m,n,k;
12     int i,j,num,hav,count;
13     scanf("%d %d %d",&m,&n,&k);
14     while(k--){
15         stack<int> s;
16         count=0;
17         hav=0;
18         for(i=0;i<n;i++)  
19             scanf("%d",&line[i]);
20         for(i=0;i<n;i++){
21             num=line[i];            
22             //hav表示的是入栈过的最大数 
23             //当前栈顶元素<=hav,凡是<=hav的元素都已经进入到栈中 
24             //1.num>hav时,num肯定>当前的栈顶元素,故hav+1到num的元素要进栈&&栈的最大元素个数<=m 
25             //2.不满足1时,num<hav&&num<=当前栈顶元素,不断退栈,直到退到满足条件的元素或者栈空 
26             //3.
27             if(hav<num&&num-hav+count<=m){//num>hav时,num肯定>当前的栈顶元素,故hav+1到num的元素要进栈&&栈的最大元素个数<=m 
28                 for(j=hav+1;j<num;j++){
29                     
30                     //cout<<"j:  "<<j<<endl;
31                     
32                     s.push(j);
33                     count++; 
34                 }
35                 hav=num; 
36             }
37             else  if(!s.empty()&&s.top()>=num){
38                 //不满足1时,num<hav&&num<=当前栈顶元素,不断退栈,直到退到满足条件的元素或者栈空
39                        while(!s.empty()&&s.top()!=num){
40                                 s.pop();
41                                 count--;
42                         }
43                        if(s.empty()){//栈空说明这个元素之前已经pop过,不符合题意 
44                                 break;
45                        }
46                        else{//找到满足的元素 
47                                 s.pop();
48                                 count--;
49                        }
50                 }else{//其他情况不符合题意 
51                     break;
52                 }
53         }
54         if(i==n){
55             printf("YES\n");
56         } 
57         else{
58             printf("NO\n");
59         }
60     }
61     return 0;
62 }

 

posted @ 2015-08-13 22:30  Deribs4  阅读(704)  评论(0编辑  收藏  举报