赞
踩
提示:栈和队列相关知识点见
数据结构-栈和队列
题目描述
实现一个栈,栈初始为空,支持四种操作:
现在要对栈进行 M 个操作,其中的每个操作 3 和操作 4 都要输出相应的结果。
输入格式
第一行包含整数 M,表示操作次数。
接下来 M 行,每行包含一个操作命令,操作命令为 push x ,pop ,empty ,query。
输出格式
对于每个 empty 和 query 操作都要输出一个查询结果,每个结果占一行。
其中,empty 操作的查询结果为 YES 或 NO,query 操作的查询结果为一个整数,表示栈顶元素的值。
数据范围
1 ≤ M ≤ 100000
1 ≤ x ≤ 1e9
所有操作保证合法。
输入样例
10
push 5
query
push 6
pop
query
pop
empty
push 4
query
empty
输出样例
5
5
YES
4
NO
#include<bits/stdc++.h> using namespace std; const int N=1e5+5; int m; int tt,stk[N]; int main() { cin>>m; while(m--) { string s; cin>>s; if(s=="push") { int x; cin>>x; stk[++tt]=x; } else if(s=="pop") { tt--; } else if(s=="query") { cout<<stk[tt]<<endl; } else if(s=="empty") { if(tt>0) puts("NO"); else puts("YES"); } } system("pause"); return 0; }
#include<bits/stdc++.h> using namespace std; int m; stack<int>stk; int main() { cin>>m; while(m--) { string s; cin>>s; if(s=="push") { int x; cin>>x; stk.push(x); } else if(s=="pop") { stk.pop(); } else if(s=="query") { cout<<stk.top()<<endl; } else if(s=="empty") { if(!stk.empty()) puts("NO"); else puts("YES"); } } system("pause"); return 0; }
题目描述
给定一个表达式,其中运算符仅包含 +,-,*,/(加 减 乘 整除),可能包含括号,请你求出表达式的最终值。
注意:
输入格式
共一行,为给定表达式。
输出格式
共一行,为表达式的结果。
数据范围
表达式的长度不超过 1e5。
输入样例
(2+2)*(1+1)
输出样例
8
#include <bits/stdc++.h> using namespace std; stack<int> num; stack<char> op; void eval() { auto b = num.top(); num.pop(); auto a = num.top(); num.pop(); auto c = op.top(); op.pop(); int x; if (c == '+') { x = a + b; } else if (c == '-') { x = a - b; } else if (c == '*') { x = a * b; } else { x = a / b; } num.push(x); } int main() { unordered_map<char, int> pr{{'+', 1}, {'-', 1}, {'*', 2}, {'/', 2}}; string str; cin >> str; for (int i = 0; i < str.size(); i ++ ) { auto c = str[i]; if (isdigit(c)) { int x = 0, j = i; while (j < str.size() && isdigit(str[j])) { x = x * 10 + str[j ++ ] - '0'; } i = j - 1; num.push(x); } else if (c == '(') { op.push(c); } else if (c == ')') { while (op.top() != '(') eval(); { op.pop(); } } else { while (op.size() && op.top() != '(' && pr[op.top()] >= pr[c]) eval(); { op.push(c); } } } while (op.size()) { eval(); } cout << num.top() << endl; system("pause"); return 0; }
题目描述
给定一个长度为 N 的整数数列,输出每个数左边第一个比它小的数,如果不存在则输出 −1。
输入格式
第一行包含整数 N,表示数列长度。
第二行包含 N 个整数,表示整数数列。
输出格式
共一行,包含 N 个整数,其中第 i 个数表示第 i 个数的左边第一个比它小的数,如果不存在则输出 −1。
数据范围
1 ≤ N ≤ 1e5
1 ≤ 数列中元素 ≤ 1e9
输入样例
5
3 4 2 7 5
输出样例
-1 3 -1 2 2
#include <bits/stdc++.h> using namespace std; const int N = 100010; int stk[N], tt; int main() { int n; cin >> n; while (n -- ) { int x; cin >> x; while (tt && stk[tt] >= x) { tt -- ; } if (!tt) { cout << "-1" << " "; } else { cout << stk[tt] << " "; } stk[ ++ tt] = x; } system("pause"); return 0; }
题目描述
实现一个队列,队列初始为空,支持四种操作:
现在要对队列进行 M 个操作,其中的每个操作 3 和操作 4 都要输出相应的结果。
输入格式
第一行包含整数 M,表示操作次数。
接下来 M 行,每行包含一个操作命令,操作命令为 push x,pop,empty,query 中的一种。
输出格式
对于每个 empty 和 query 操作都要输出一个查询结果,每个结果占一行。
其中,empty 操作的查询结果为 YES 或 NO,query 操作的查询结果为一个整数,表示队头元素的值。
数据范围
1 ≤ M ≤ 100000
1 ≤ x ≤ 1e9
所有操作保证合法。
输入样例
10
push 6
empty
query
pop
empty
push 3
push 4
pop
query
push 6
输出样例
NO
6
YES
4
#include <bits/stdc++.h> using namespace std; const int N = 100010; int m; int q[N], hh, tt = -1; int main() { cin >> m; while (m -- ) { string op; int x; cin >> op; if (op == "push") { cin >> x; tt++; q[tt] = x; } else if (op == "pop") { hh ++ ; } else if (op == "empty") { cout << (hh <= tt ? "NO" : "YES") << endl; } else { cout << q[hh] << endl; } } system("pause"); return 0; }
题目描述
给定一个大小为 n ≤ 1e6 的数组。
有一个大小为 k 的滑动窗口,它从数组的最左边移动到最右边。
你只能在窗口中看到 k 个数字。
每次滑动窗口向右移动一个位置。
以下是一个例子:
该数组为 [1 3 -1 -3 5 3 6 7],k 为 3。
窗口位置 | 最小值 | 最大值 |
---|---|---|
[1 3 -1] -3 5 3 6 7 | -1 | 3 |
1 [3 -1 -3] 5 3 6 7 | -3 | 3 |
1 3 [-1 -3 5] 3 6 7 | -3 | 5 |
1 3 -1 [-3 5 3] 6 7 | -3 | 5 |
1 3 -1 -3 [5 3 6] 7 | 3 | 6 |
1 3 -1 -3 5 [3 6 7] | 3 | 7 |
你的任务是确定滑动窗口位于每个位置时,窗口中的最大值和最小值。
输入格式
输入包含两行。
第一行包含两个整数 n 和 k,分别代表数组长度和滑动窗口的长度。
第二行有 n 个整数,代表数组的具体数值。
同行数据之间用空格隔开。
输出格式
输出包含两个。
第一行输出,从左至右,每个位置滑动窗口中的最小值。
第二行输出,从左至右,每个位置滑动窗口中的最大值。
输入样例
8 3
1 3 -1 -3 5 3 6 7
输出样例
-1 -3 -3 -3 3 3
3 3 5 5 6 7
#include <bits/stdc++.h> using namespace std; const int N = 1000010; int a[N], q[N]; int main() { int n, k; cin >> n >> k; for (int i = 0; i < n; i ++ ) { cin >> a[i]; } int hh = 0, tt = -1; for (int i = 0; i < n; i ++ ) { if (hh <= tt && i - k + 1 > q[hh]) { hh ++ ; } while (hh <= tt && a[q[tt]] >= a[i]) { tt -- ; } q[ ++ tt] = i; if (i >= k - 1) { cout << a[q[hh]] << " "; } } puts(""); hh = 0, tt = -1; for (int i = 0; i < n; i ++ ) { if (hh <= tt && i - k + 1 > q[hh]) { hh ++ ; } while (hh <= tt && a[q[tt]] <= a[i]) { tt -- ; } q[ ++ tt] = i; if (i >= k - 1) { cout << a[q[hh]] << " "; } } puts(""); system("pause"); return 0; }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。