栈的算法练习题

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

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 Nis 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

输入M,N,K,M是测试的组数,N是数组的长度,K是栈的最大深度

题目的意思是,1到N的数按顺序随机出栈,

比如要实现第一个例子,1入栈出栈,2入栈出栈

第二个3要出栈,2和1必须入栈,2,1必须按顺序入栈,7要入栈,4,5,6必须按顺序入栈出栈,所以不行

第四组例子:5入栈,1,2,3,4必须入栈,6入栈,出栈,按顺序入栈出栈,不难得到以下代码


#include<stack>
#include<iostream>
#include<vector>
#include<iterator>
#include<algorithm>

using namespace std;
int M,N,K;
int check_stack(vector<int> &v){
    stack<int> sta;
    sta.push(0);    //dummy element, as sential
    int cap = M + 1;    //stack capacity
    int idx = 0;        //index of vector v
    int num = 1;        //to push to stack
    while(idx != N){
    	// 入栈的条件 
        while(v[idx]>sta.top()&&sta.size()<cap&&idx<=N)
        // 从1开始入栈 
            sta.push(num++); 
		// 如果该序列与栈顶位置相同 ,说明栈顶出栈       
        if(v[idx]==sta.top()){
            sta.pop();
            idx++; // 切换到下一个指标 
            
        }
        // 如果不相同,则说明错误 
        else //sta.size>=cap; idx>N
            return 0;   //false
    }
    return 1;   //true
}
int main(){
	
    cin>>M>>N>>K;
    vector<int> vec(N,0);   //N pop number
    for(int i = 0; i<K; i++){
        //input to vec
        //copy_n(istream_iterator<int>(cin),N,vec.begin());
        for(int j=0;j<N;j++)
        cin>>vec[j];
        cout << (check_stack(vec)?"YES":"NO")<<endl;
    }   
}

的四则运算表达式习是指通过的数据结构来进行四则运算的练习题。其中包括中缀表达式转后缀表达式和后缀表达式的计算。首先,中缀表达式转后缀表达式是通过使用的特点,将中缀表达式转化为等价的后缀表达式。然后,利用后缀表达式进行计算时,需要遍历表达式,按照计算方法进行计算。以下是一个完整的示例代码,用于演示的四则运算表达式习: ```python # 定义函数,用于判断运算符的优先级 def precedence(operator): if operator == '+' or operator == '-': return 1 elif operator == '*' or operator == '/': return 2 else: return 0 # 定义函数,用于将中缀表达式转化为后缀表达式 def infix_to_postfix(expression): stack = [] postfix = '' for char in expression: if char.isdigit(): postfix += char elif char == '(': stack.append(char) elif char == ')': while stack and stack[-1 != '(': postfix += stack.pop() stack.pop() else: while stack and precedence(char) <= precedence(stack[-1]): postfix += stack.pop() stack.append(char) while stack: postfix += stack.pop() return postfix # 定义函数,用于计算后缀表达式的值 def evaluate_postfix(expression): stack = [] for char in expression: if char.isdigit(): stack.append(int(char)) else: num2 = stack.pop() num1 = stack.pop() if char == '+': stack.append(num1 + num2) elif char == '-': stack.append(num1 - num2) elif char == '*': stack.append(num1 * num2) elif char == '/': stack.append(num1 / num2) return stack.pop() # 示例表达式 infix_expression = '(3+4)*(5-2)' postfix_expression = infix_to_postfix(infix_expression) result = evaluate_postfix(postfix_expression) # 输出结果 print("中缀表达式:", infix_expression) print("后缀表达式:", postfix_expression) print("计算结果:", result) ``` 以上代码会将中缀表达式"(3+4)*(5-2)"转化为后缀表达式"34+52-*",并且计算出结果为21。这是的四则运算表达式习的一个例子。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值