A.洛谷 T642387
题意
给定一个包含 个正整数 ,只有运算符 +、-、* 和括号的表达式。
求表达式的值对 取模后的结果。
思路
考虑模拟用栈表达式计算过程。首先读入一个字符串,对于每个字符如果是数字字符,就不断转十进制。如果遇到运算符就将之前的数字插入栈中,并把在该运算符之前的所有优先级比它小的运算符、数依次弹出计算,最后将该运算符插入栈,优先级要加上括号带来的。优先级见下表。
| + | - | * | ( | ) |
|---|---|---|---|---|
代码
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int mod = 1e9 + 7, N = 2e6 + 10;
struct P{
int div;
int val;
char sym;
}st[N];
vector<P> rec;
int get(char ch){
if(ch == '*'){
return 2;
}
if(ch == '-' || ch == '+'){
return 1;
}
return 0;
}
int t = 0;
void calc(P op){
if(op.sym != '#'){
while(t >= 3 && st[t - 1].div >= op.div){
if(st[t - 1].sym == '+'){
st[t - 2].val = (st[t - 2].val + st[t].val) % mod;
}
else if(st[t - 1].sym == '-'){
st[t - 2].val = (st[t - 2].val - st[t].val + mod) % mod;
}
else if(st[t - 1].sym == '*'){
st[t - 2].val = (st[t - 2].val * st[t].val) % mod;
}
t -= 2;
}
}
st[++t] = op;
}
signed main(){
string s;
cin >> s;
s += '?';
for(int i = 0, sum = 0, o = 0; i < s.size(); i++){
if(s[i] == '?'){
rec.push_back({0, sum, '#'});
rec.push_back({-1, 0, '!'});
continue;
}
if(s[i] == '('){
o += 2;
continue;
}
if(s[i] == ')'){
o -= 2;
continue;
}
if(isdigit(s[i])){
sum = sum * 10 + (s[i] - '0');
}
else{
rec.push_back({0, sum, '#'});
sum = 0;
rec.push_back({get(s[i]) + o, 0, s[i]});
}
}
for(P i : rec){
calc(i);
}
cout << st[1].val;
return 0;
}
B.洛谷 P1449
题意
给定一的后缀表达式,@ 为表达式的结束符号。. 为操作数的结束符号。请求出表达式的值。
后缀表达式:式中不再引用括号,运算符号放在两个运算对象之后,所有计算按运算符号出现的顺序,严格地由左而右新进行(不用考虑运算符的优先级)。
思路
题目给定的是后缀表达式,所以是从前往后依次枚举字符,如果是数字字符,则累加转十进制,遇到 . 就插入栈。如果遇到运算符就将栈的前两个元素弹出、运算,并重新压入栈内。最后输出栈内唯一的元素,即答案。
代码
#include<bits/stdc++.h>
using namespace std;
string s;
stack<int> st;
int sum = 0;
int main(){
cin >> s;
for(char ch : s){
if(ch == '@') continue;
if(isdigit(ch)){
sum = sum * 10 + (ch - '0');
}
else if(ch == '.'){
st.push(sum);
sum = 0;
}
else{
if(ch == '-'){
int y = st.top();
st.pop();
int x = st.top();
st.pop();
st.push(x - y);
}
if(ch == '+'){
int y = st.top();
st.pop();
int x = st.top();
st.pop();
st.push(x + y);
}
if(ch == '/'){
int y = st.top();
st.pop();
int x = st.top();
st.pop();
st.push(x / y);
}
if(ch == '*'){
int y = st.top();
st.pop();
int x = st.top();
st.pop();
st.push(x * y);
}
}
}
cout << st.top();
return 0;
}
C.洛谷 T642390
题意
有一个包含 个节点的表达式树,其中只包含整数、运算符 + 和 *,给出其后缀表达式,请输出它的中缀表达式。
每一次运算都用括号包裹起来,以明确优先级。
思路
因为题目给定的是后缀表达式,所以从前往后处理每个字符,如果为运算符就建树——把当前结点的左右儿子设为栈内的前两个元素,并将它们从栈中弹出。接着就将运算符和数字压入栈内。最后后递归左右儿子求出中缀表达式。
代码
#include<bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
int n, t, st[N];
string s[N];
vector<string> ans;
struct P{
int x = 0, y = 0;
}a[N];
void dfs(int x){
if(x < 1 || x > n){
return ;
}
if(a[x].x != 0 || a[x].y != 0) ans.push_back("(");
dfs(a[x].x);
ans.push_back(s[x]);
dfs(a[x].y);
if(a[x].x != 0 || a[x].y != 0) ans.push_back(")");
}
int main(){
cin >> n;
for(int i = 1; i <= n; i++){
cin >> s[i];
if(s[i][0] == '+' || s[i][0] == '*'){
a[i] = {st[t - 1], st[t]};
t -= 2;
}
st[++t] = i;
}
dfs(st[1]);
for(auto i : ans){
if(i[0] == '*' || i[0] == '+'){
cout << " ";
}
cout << i;
if(i[0] == '*' || i[0] == '+'){
cout << " ";
}
}
return 0;
}