码迷,mamicode.com
首页 > 其他好文 > 详细

数据结构 02-线性结构4 Pop Sequence (25 分)

时间:2021-05-24 13:43:08      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:check   cat   std   for   线性   end   max   ret   maxsize   

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

 

解题思路

模拟入栈出栈过程,一组数字进行入栈出栈,  循环结束后行为都合法就输出YES ,有不合法行为就输出NO

1.因为入栈序列是固定的 ,  但是入栈时栈容量是否合法需要判断,  所以需要记录当前栈的容量, 

 

使用类来模拟stack的写法 面向对象

#include <iostream>
#include <vector>
using namespace std;
class stack{
public:
    vector<int> data;
    int size;
    int maxsize;
    stack()=default;
    stack(int maxsize_,int numbers_):maxsize(maxsize_),data{vector<int>(numbers_+1,0)},size{0}{};
    bool stackIn(int num){
        bool flag=true;
        for(int i=1;i<=num;i++){
            if(data[i]==0){
                if(size<maxsize){
                    data[i]=1;
                    size++;
                }
                if(size==maxsize&&i!=num){
                    flag=false;
                    break;
                }
            }else{
                continue;
            }
        }
        return flag;
    }
    bool stackOut(int num){
        bool flag=false;
        for(int i=data.size()-1;i>=num;i--){
            if(data[i]==1&&i!=num){
                break;
            }else if(data[i]==1&&i==num){
                data[i]=2;
                flag=true;
                size --;
                break;
            }
        }
        return flag;
    }
    
};
int main(){
    int m,n,k;
    int numToCheck,count{0};
    cin >> m >> n >>k;
    for(int i=0;i<k;i++){
        bool flag=true;
        count=0;
        stack s{m,n};
        for(int j=0;j<n;j++){
            scanf("%d",&numToCheck);
            if(s.stackIn(numToCheck)&&s.stackOut(numToCheck)){
                continue;
            }else{
                flag=false;
            }
        }
        if(flag){
            cout <<"YES"<<endl;
        }else{
            cout <<"NO"<<endl;
        }
    }
    return 0;
}

 

c风格写法 面向过程

#include <iostream>
#include <vector>
using namespace std;
bool stackIn(int num,vector<int> &stack,int &count,int m){
    bool flag=true;
    for(int i=1;i<=num;i++){
        if(stack[i]==0){
            if(count<m){
                stack[i]=1;
                count++;
            }
            if(count==m&&i!=num){
                flag=false;
                break;
            }
        }else{
            continue;
        }
    }
    return flag;
}
bool stackOut(int num,vector<int> &stack,int &count){
    bool flag=false;
    for(int i=stack.size()-1;i>=num;i--){
        if(stack[i]==1&&i!=num){
            break;
        }else if(stack[i]==1&&i==num){
            stack[i]=2;
            flag=true;
            count --;
            break;
        }
    }
    return flag;
}
int main(){
    int m,n,k;
    int numToCheck,count{0};
    cin >> m >> n >>k;
    for(int i=0;i<k;i++){
        bool flag=true;
        count=0;
        vector<int> stack(n+1,0);//0为不存在 1 为在栈中 2为已经出栈
        for(int j=0;j<n;j++){
            scanf("%d",&numToCheck);
            if(stackIn(numToCheck,stack,count,m)&&stackOut(numToCheck,stack,count)){
                continue;
            }else{
                flag=false;
            }
        }
        if(flag){
            cout <<"YES"<<endl;
        }else{
            cout <<"NO"<<endl;
        }
    }
    return 0;
}

 

数据结构 02-线性结构4 Pop Sequence (25 分)

标签:check   cat   std   for   线性   end   max   ret   maxsize   

原文地址:https://www.cnblogs.com/ichiha/p/14776285.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!