'commit'
This commit is contained in:
74
TangDou/AcWing/Stack/3302.cpp
Normal file
74
TangDou/AcWing/Stack/3302.cpp
Normal file
@@ -0,0 +1,74 @@
|
||||
#include <bits/stdc++.h>
|
||||
using namespace std;
|
||||
|
||||
stack<int> num; // 数字栈
|
||||
stack<char> op; // 操作符栈
|
||||
|
||||
// 优先级表
|
||||
unordered_map<char, int> h{{'+', 1}, {'-', 1}, {'*', 2}, {'/', 2}, {'(', 0}};
|
||||
|
||||
/**
|
||||
* 功能:计算两个数的和差积商
|
||||
*/
|
||||
void eval() {
|
||||
int a = num.top(); // 第二个操作数
|
||||
num.pop();
|
||||
|
||||
int b = num.top(); // 第一个操作数
|
||||
num.pop();
|
||||
|
||||
char p = op.top(); // 运算符
|
||||
op.pop();
|
||||
|
||||
int r; // 结果
|
||||
// 计算结果
|
||||
if (p == '+')
|
||||
r = b + a;
|
||||
else if (p == '-')
|
||||
r = b - a;
|
||||
else if (p == '*')
|
||||
r = b * a;
|
||||
else if (p == '/')
|
||||
r = b / a;
|
||||
// 结果入栈
|
||||
num.push(r);
|
||||
}
|
||||
|
||||
int main() {
|
||||
// 读入表达式
|
||||
string s;
|
||||
cin >> s;
|
||||
// 遍历字符串的每一位
|
||||
for (int i = 0; i < s.size(); i++) {
|
||||
// ① 如果是数字,则入栈
|
||||
if (isdigit(s[i])) {
|
||||
// 读出完整的数字
|
||||
int x = 0;
|
||||
while (i < s.size() && isdigit(s[i])) {
|
||||
x = x * 10 + s[i] - '0';
|
||||
i++;
|
||||
}
|
||||
i--; // 加多了一位,需要减去
|
||||
|
||||
num.push(x); // 数字入栈
|
||||
}
|
||||
// ② 左括号无优先级,入栈
|
||||
else if (s[i] == '(')
|
||||
op.push(s[i]);
|
||||
// ③ 右括号时,需计算最近一对括号里面的值
|
||||
else if (s[i] == ')') {
|
||||
// 从栈中向前找,一直找到左括号
|
||||
while (op.top() != '(') eval(); // 将左右括号之间的计算完,维护回栈里
|
||||
// 左括号出栈
|
||||
op.pop();
|
||||
} else { // ④ 运算符
|
||||
// 如果待入栈运算符优先级低,则先计算
|
||||
while (op.size() && h[op.top()] >= h[s[i]]) eval();
|
||||
op.push(s[i]); // 操作符入栈
|
||||
}
|
||||
}
|
||||
while (op.size()) eval(); // ⑤ 剩余的进行计算
|
||||
|
||||
printf("%d\n", num.top()); // 输出结果
|
||||
return 0;
|
||||
}
|
||||
222
TangDou/AcWing/Stack/3302.md
Normal file
222
TangDou/AcWing/Stack/3302.md
Normal file
@@ -0,0 +1,222 @@
|
||||
##[$AcWing$ $3302$. 表达式求值](https://www.acwing.com/problem/content/description/3305/)
|
||||
|
||||
### 一、题目描述
|
||||
给定一个表达式,其中运算符仅包含 `+,-,*,/`(加 减 乘 整除),可能包含括号,请你求出表达式的最终值。
|
||||
|
||||
**注意:**
|
||||
|
||||
- 数据保证给定的表达式合法。
|
||||
- 题目保证符号 $-$只作为减号出现,不会作为负号出现,例如,$-1+2,(2+2)*(-(1+1)+2)$ 之类表达式均不会出现。
|
||||
- 题目保证表达式中所有数字均为正整数。
|
||||
- 题目保证表达式在中间计算过程以及结果中,均不超过 $2^{31}−1$。
|
||||
- 题目中的整除是指向 $0$ 取整,也就是说对于大于 $0$ 的结果向下取整,例如 $5/3=1$
|
||||
,对于小于 $0$ 的结果向上取整,例如 $5/(1−4)=−1$。
|
||||
- $C++$和$Java$中的整除默认是向零取整;$Python$中的整除//默认向下取整,因此$Python$的$eval()$函数中的整除也是向下取整,在本题中不能直接使用。
|
||||
|
||||
**输入格式**
|
||||
共一行,为给定表达式。
|
||||
|
||||
**输出格式**
|
||||
共一行,为表达式的结果。
|
||||
|
||||
|
||||
**数据范围**
|
||||
表达式的长度不超过 $10^5$。
|
||||
|
||||
**输入样例:**
|
||||
```cpp {.line-numbers}
|
||||
(2+2)*(1+1)
|
||||
```
|
||||
|
||||
**输出样例:**
|
||||
```cpp {.line-numbers}
|
||||
8
|
||||
```
|
||||
|
||||
### 二、算法思路
|
||||
|
||||
同类题参考:
|
||||
|
||||
**[【$2013$ $NOIP$ 普及组】表达式求值](http://ybt.ssoier.cn:8088/problem_show.php?pid=1962)**
|
||||
|
||||
**[$CSP$ $2022$ $J2$ 试卷解析 第$3$题](https://www.cnblogs.com/littlehb/p/16865277.html)**
|
||||
|
||||
先看下只有 `+` 和 `*` 的。
|
||||
|
||||
输入长度为$n$的字符串,例如:$ 1 + 2 + 3 * 4 * 5$
|
||||
|
||||
输出表达式的值,即:$63$
|
||||
|
||||
|
||||
**“表达式求值”问题,两个核心关键点:**
|
||||
<font color='black' size=4><b>
|
||||
- 双栈,一个操作数栈,一个运算符栈;
|
||||
- 运算符优先级:"栈顶运算符"与"即将入栈的运算符"的优先级比较:
|
||||
* 如果栈顶的运算符优先级低,新运算符直接入栈。
|
||||
* 如果栈顶的运算符优先级高,先出栈计算,新运算符再入栈。
|
||||
</b></font>
|
||||
|
||||
**举个栗子**
|
||||
|
||||
$1+2+3 * 4 *5$,看是如何利用上述两个关键点实施计算的。
|
||||
|
||||
首先,这个例子只有`+`和`*`两个运算符,所以它的运算符表是:
|
||||
<center><img src='https://cdn.acwing.com/media/article/image/2021/03/20/55289_095276ee89-00.webp.jpg'></center>
|
||||
|
||||
(1)如果栈顶是`+`,即将入栈的是`+`,栈顶优先级高,需要先计算,再入栈;
|
||||
|
||||
(2)如果栈顶是`+`,即将入栈的是`*`,栈顶优先级低,直接入栈;
|
||||
|
||||
(3)如果栈顶是`*`,即将入栈的是`+`,栈顶优先级高,需要先计算,再入栈;
|
||||
|
||||
(4)如果栈顶是`*`,即将入栈的是`*`,栈顶优先级高,需要先计算,再入栈;
|
||||
|
||||
有了运算符表,一切就好办了。
|
||||
|
||||

|
||||
|
||||
一开始,初始化好输入的字符串,以及操作数栈,运算符栈。
|
||||
|
||||

|
||||
|
||||
一步步,扫描字符串,操作数一个个入栈,运算符也入栈。
|
||||
|
||||

|
||||
|
||||
下一个操作符要入栈时,需要先比较优先级。
|
||||
栈内的优先级高,必须先计算,才能入栈。
|
||||
|
||||

|
||||
|
||||
计算的过程为:
|
||||
|
||||
(1)操作数出栈,作为$num2$;
|
||||
|
||||
(2)操作数出栈,作为$num1$;
|
||||
|
||||
(3)运算符出栈,作为$op$;
|
||||
|
||||
(4)计算出结果;
|
||||
|
||||

|
||||
|
||||
(5)结果入操作数栈;
|
||||
|
||||

|
||||
|
||||
接下来,运算符和操作数才能继续入栈。下一个操作符要入栈时,继续比较与栈顶的优先级。
|
||||
|
||||
栈内的优先级低,可以直接入栈。
|
||||
|
||||

|
||||
|
||||
字符串继续移动。
|
||||

|
||||
|
||||
又要比较优先级了。
|
||||
|
||||

|
||||
|
||||
栈内的优先级高,还是先计算(`3*4=12`),再入栈。
|
||||
|
||||

|
||||
|
||||
|
||||
不断入栈,直到字符串扫描完毕。
|
||||

|
||||
|
||||
不断出栈,直到得到最终结果$3+60=63$,算法完成。
|
||||
|
||||
这个方法的时间复杂度为$O(n)$,整个字符串只需要扫描一遍。
|
||||
|
||||
运算符有$+$、 $-$、$*$、 $/$、 $($、 $)$、 $\{$ 、 $\}$ 都没问题,如果共有$n$个运算符,会有一个$n*n$的优先级表。
|
||||
|
||||
#### 小结
|
||||
|
||||
|
||||
1. 运算符优先级表
|
||||
|
||||
2. 左括号直接入操作符栈,右括号不入操作符栈,看到右括号,就不断的处理操作符栈,直到看到左括号,再把左括号弹出。
|
||||
|
||||
3. 抽象出从一个字符串中读取数字的办法。
|
||||
|
||||
4. 抽象出两个数字通过数字栈、操作符栈进行计算的通用办法。
|
||||
|
||||
|
||||
### 三、实现代码
|
||||
```cpp {.line-numbers}
|
||||
#include <bits/stdc++.h>
|
||||
using namespace std;
|
||||
|
||||
stack<int> num; // 数字栈
|
||||
stack<char> op; // 操作符栈
|
||||
|
||||
// 优先级表
|
||||
unordered_map<char, int> h{{'+', 1}, {'-', 1}, {'*', 2}, {'/', 2}, {'(', 0}};
|
||||
|
||||
/**
|
||||
* 功能:计算两个数的和差积商
|
||||
*/
|
||||
void eval() {
|
||||
int a = num.top(); // 第二个操作数
|
||||
num.pop();
|
||||
|
||||
int b = num.top(); // 第一个操作数
|
||||
num.pop();
|
||||
|
||||
char p = op.top(); // 运算符
|
||||
op.pop();
|
||||
|
||||
int r; // 结果
|
||||
// 计算结果
|
||||
if (p == '+')
|
||||
r = b + a;
|
||||
else if (p == '-')
|
||||
r = b - a;
|
||||
else if (p == '*')
|
||||
r = b * a;
|
||||
else if (p == '/')
|
||||
r = b / a;
|
||||
// 结果入栈
|
||||
num.push(r);
|
||||
}
|
||||
|
||||
int main() {
|
||||
// 读入表达式
|
||||
string s;
|
||||
cin >> s;
|
||||
// 遍历字符串的每一位
|
||||
for (int i = 0; i < s.size(); i++) {
|
||||
// ① 如果是数字,则入栈
|
||||
if (isdigit(s[i])) {
|
||||
// 读出完整的数字
|
||||
int x = 0;
|
||||
while (i < s.size() && isdigit(s[i])) {
|
||||
x = x * 10 + s[i] - '0';
|
||||
i++;
|
||||
}
|
||||
i--; // 加多了一位,需要减去
|
||||
|
||||
num.push(x); // 数字入栈
|
||||
}
|
||||
// ② 左括号无优先级,入栈
|
||||
else if (s[i] == '(')
|
||||
op.push(s[i]);
|
||||
// ③ 右括号时,需计算最近一对括号里面的值
|
||||
else if (s[i] == ')') {
|
||||
// 从栈中向前找,一直找到左括号
|
||||
while (op.top() != '(') eval(); // 将左右括号之间的计算完,维护回栈里
|
||||
// 左括号出栈
|
||||
op.pop();
|
||||
} else { // ④ 运算符
|
||||
// 如果待入栈运算符优先级低,则先计算
|
||||
while (op.size() && h[op.top()] >= h[s[i]]) eval();
|
||||
op.push(s[i]); // 操作符入栈
|
||||
}
|
||||
}
|
||||
while (op.size()) eval(); // ⑤ 剩余的进行计算
|
||||
|
||||
printf("%d\n", num.top()); // 输出结果
|
||||
return 0;
|
||||
}
|
||||
```
|
||||
27
TangDou/AcWing/Stack/828.cpp
Normal file
27
TangDou/AcWing/Stack/828.cpp
Normal file
@@ -0,0 +1,27 @@
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
const int N = 100010;
|
||||
int stk[N], tt;
|
||||
|
||||
int main() {
|
||||
string op;
|
||||
int n;
|
||||
cin >> n;
|
||||
while (n--) {
|
||||
cin >> op;
|
||||
if (op == "push") {
|
||||
int x;
|
||||
cin >> x;
|
||||
stk[++tt] = x;
|
||||
} else if (op == "pop")
|
||||
tt--;
|
||||
else if (op == "query")
|
||||
printf("%d\n", stk[tt]);
|
||||
else if (op == "empty") {
|
||||
if (tt == 0) printf("YES\n");
|
||||
else printf("NO\n");
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
91
TangDou/AcWing/Stack/828.md
Normal file
91
TangDou/AcWing/Stack/828.md
Normal file
@@ -0,0 +1,91 @@
|
||||
##[$AcWing$ $828$. 模拟栈](https://www.acwing.com/problem/content/description/830/)
|
||||
|
||||
### 一、题目描述
|
||||
|
||||
实现一个栈,栈初始为空,支持四种操作:
|
||||
|
||||
`push x` – 向栈顶插入一个数 `x`;
|
||||
`pop` – 从栈顶弹出一个数;
|
||||
`empty` – 判断栈是否为空;
|
||||
`query` – 查询栈顶元素。
|
||||
现在要对栈进行 $M$ 个操作,其中的每个操作 $3$ 和操作 $4$ 都要输出相应的结果。
|
||||
|
||||
**输入格式**
|
||||
第一行包含整数 $M$,表示操作次数。
|
||||
|
||||
接下来 $M$ 行,每行包含一个操作命令,操作命令为 `push` `x`,`pop`,`empty`,`query` 中的一种。
|
||||
|
||||
**输出格式**
|
||||
对于每个 `empty` 和 `query` 操作都要输出一个查询结果,每个结果占一行。
|
||||
|
||||
其中,`empty` 操作的查询结果为 `YES` 或 `NO`,`query` 操作的查询结果为一个整数,表示栈顶元素的值。
|
||||
|
||||
**数据范围**
|
||||
$1≤M≤100000,1≤x≤10^9$
|
||||
所有操作保证合法。
|
||||
|
||||
**输入样例:**
|
||||
```cpp {.line-numbers}
|
||||
10
|
||||
push 5
|
||||
query
|
||||
push 6
|
||||
pop
|
||||
query
|
||||
pop
|
||||
empty
|
||||
push 4
|
||||
query
|
||||
empty
|
||||
```
|
||||
**输出样例:**
|
||||
```cpp {.line-numbers}
|
||||
5
|
||||
5
|
||||
YES
|
||||
4
|
||||
NO
|
||||
```
|
||||
|
||||
|
||||
### 二、理解与感悟
|
||||
<img src='https://cdn.acwing.com/media/article/image/2020/12/17/2675_bb6f334a3f-1.png'>
|
||||
|
||||
1、$tt$确定初始值是$0$,但增加时++$tt$, 就是实际上数组是从$1$开始的。
|
||||
|
||||
2、弹出就$tt$--,指针变了,但多余的数据不用清除,不碍事。
|
||||
|
||||
3、$tt$回到$0$,就是一个都没有了。
|
||||
|
||||
4、用数组模拟栈,比$STL$的$stack$方便、速度快,可以遍历到栈中每一个元素。
|
||||
|
||||
### 三、实现代码
|
||||
```cpp {.line-numbers}
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
const int N = 100010;
|
||||
int stk[N], tt;
|
||||
string cmd;
|
||||
|
||||
int main() {
|
||||
int n;
|
||||
cin >> n;
|
||||
while (n--) {
|
||||
cin >> cmd;
|
||||
if (cmd == "push") {
|
||||
int x;
|
||||
cin >> x;
|
||||
stk[++tt] = x;
|
||||
} else if (cmd == "pop")
|
||||
tt--;
|
||||
else if (cmd == "query")
|
||||
printf("%d\n", stk[tt]);
|
||||
else if (cmd == "empty") {
|
||||
if (tt == 0) printf("YES\n");
|
||||
else printf("NO\n");
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
```
|
||||
27
TangDou/AcWing/Stack/830.cpp
Normal file
27
TangDou/AcWing/Stack/830.cpp
Normal file
@@ -0,0 +1,27 @@
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
int n;
|
||||
const int N = 1e5 + 10;
|
||||
int a[N];
|
||||
int stk[N], tt;
|
||||
int res[N];
|
||||
int main() {
|
||||
cin >> n;
|
||||
for (int i = 1; i <= n; i++)cin >> a[i];
|
||||
for (int i = n; i >= 1; i--) {
|
||||
while (tt && a[stk[tt]] > a[i]) {
|
||||
res[stk[tt]]=i;
|
||||
tt--;
|
||||
}
|
||||
stk[++tt]=i;
|
||||
}
|
||||
for (int i = 1; i <= n; i++)
|
||||
res[i]?printf("%d ",a[res[i]]):printf("-1 ");
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
102
TangDou/AcWing/Stack/830.md
Normal file
102
TangDou/AcWing/Stack/830.md
Normal file
@@ -0,0 +1,102 @@
|
||||
##[$AcWing$ $830$.单调栈](https://www.acwing.com/problem/content/832/)
|
||||
|
||||
### 一、题目描述
|
||||
给定一个长度为 $N$ 的整数数列,输出每个数左边第一个比它小的数,如果不存在则输出 $−1$。
|
||||
|
||||
**输入格式**
|
||||
第一行包含整数 $N$,表示数列长度。
|
||||
|
||||
第二行包含 $N$ 个整数,表示整数数列。
|
||||
|
||||
**输出格式**
|
||||
共一行,包含 $N$ 个整数,其中第 $i$ 个数表示第 $i$ 个数的左边第一个比它小的数,如果不存在则输出 $−1$。
|
||||
|
||||
**数据范围**
|
||||
$1≤N≤10^5$
|
||||
|
||||
$1≤数列中元素≤10^9$
|
||||
|
||||
**输入样例:**
|
||||
```cpp {.line-numbers}
|
||||
5
|
||||
3 4 2 7 5
|
||||
```
|
||||
|
||||
**输出样例:**
|
||||
```cpp {.line-numbers}
|
||||
-1 3 -1 2 2
|
||||
```
|
||||
|
||||
|
||||
### 二、单调栈原理
|
||||
单调栈里装的是什么?
|
||||
|
||||
**动态维护一个栈,把后续问题的答案都维持在这个栈中,把肯定不再有用的数字从栈中干掉。**
|
||||
|
||||
- 动态维护,随进随维护,不需预处理
|
||||
|
||||
- 将$O(N^2)$的时间复杂度降为$O(N)$
|
||||
|
||||
- 此类 **左侧(或右侧)最近**,**比自己大的(或小的)**,用单调栈
|
||||
|
||||
|
||||
### 三、左边第一个比我小
|
||||
```cpp {.line-numbers}
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
const int N = 1e5 + 10;
|
||||
int stk[N], tt;
|
||||
int main() {
|
||||
int n;
|
||||
cin >> n;
|
||||
for (int i = 1; i <= n; i++) {
|
||||
int x;
|
||||
cin >> x;
|
||||
while (tt && x <= stk[tt]) tt--;
|
||||
if (tt == 0)
|
||||
cout << "-1 ";
|
||||
else
|
||||
cout << stk[tt] << " ";
|
||||
stk[++tt] = x;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
```
|
||||
|
||||
### 四、右边第一个比我大
|
||||
```cpp {.line-numbers}
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
const int N = 1e5 + 10;
|
||||
int stk[N], tt;
|
||||
/**
|
||||
测试用例:右边第一个比我大
|
||||
6
|
||||
6 10 3 7 4 12
|
||||
|
||||
结果:
|
||||
10 12 7 12 12 -1
|
||||
*/
|
||||
int a[N], res[N];
|
||||
int main() {
|
||||
freopen("830_RightBigger.in", "r", stdin);
|
||||
|
||||
int n;
|
||||
cin >> n;
|
||||
for (int i = 1; i <= n; i++) cin >> a[i];
|
||||
|
||||
for (int i = n; i; i--) {
|
||||
while (tt && a[i] >= stk[tt]) tt--;
|
||||
if (tt == 0)
|
||||
res[i] = -1;
|
||||
else
|
||||
res[i] = stk[tt];
|
||||
stk[++tt] = a[i];
|
||||
}
|
||||
|
||||
for (int i = 1; i <= n; i++) cout << res[i] << " ";
|
||||
return 0;
|
||||
}
|
||||
```
|
||||
31
TangDou/AcWing/Stack/830_1.cpp
Normal file
31
TangDou/AcWing/Stack/830_1.cpp
Normal file
@@ -0,0 +1,31 @@
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
const int N = 100010;
|
||||
int n;
|
||||
int a[N];
|
||||
int stk[N], tt;
|
||||
int res[N];
|
||||
|
||||
/**
|
||||
测试用例:右边第一个比我大
|
||||
6
|
||||
6 10 3 7 4 12
|
||||
|
||||
结果:
|
||||
10 12 7 12 12 -1
|
||||
*/
|
||||
int main() {
|
||||
cin >> n;
|
||||
for (int i = 1; i <= n; i++) cin >> a[i];
|
||||
//答案是查找右侧的,所以正序遍历
|
||||
for (int i = 1; i <= n; i++) {
|
||||
while (tt && a[stk[tt]] < a[i]) {//栈内装的是没有找到答案的数字
|
||||
res[stk[tt]] = i;
|
||||
tt--;
|
||||
}
|
||||
stk[++tt] = i;
|
||||
}
|
||||
for (int i = 1; i <= n; i++) res[i] ? cout << a[res[i]] << ' ' : cout << "-1 ";
|
||||
return 0;
|
||||
}
|
||||
33
TangDou/AcWing/Stack/830_RightBigger.cpp
Normal file
33
TangDou/AcWing/Stack/830_RightBigger.cpp
Normal file
@@ -0,0 +1,33 @@
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
const int N = 1e5 + 10;
|
||||
int stk[N], tt;
|
||||
/**
|
||||
测试用例:右边第一个比我大
|
||||
6
|
||||
6 10 3 7 4 12
|
||||
|
||||
结果:
|
||||
10 12 7 12 12 -1
|
||||
*/
|
||||
int a[N], res[N];
|
||||
int main() {
|
||||
freopen("830_RightBigger.in", "r", stdin);
|
||||
|
||||
int n;
|
||||
cin >> n;
|
||||
for (int i = 1; i <= n; i++) cin >> a[i];
|
||||
|
||||
for (int i = n; i; i--) {
|
||||
while (tt && a[i] >= stk[tt]) tt--;
|
||||
if (tt == 0)
|
||||
res[i] = -1;
|
||||
else
|
||||
res[i] = stk[tt];
|
||||
stk[++tt] = a[i];
|
||||
}
|
||||
|
||||
for (int i = 1; i <= n; i++) cout << res[i] << " ";
|
||||
return 0;
|
||||
}
|
||||
2
TangDou/AcWing/Stack/830_RightBigger.in
Normal file
2
TangDou/AcWing/Stack/830_RightBigger.in
Normal file
@@ -0,0 +1,2 @@
|
||||
6
|
||||
6 10 3 7 4 12
|
||||
Reference in New Issue
Block a user