计蒜客 43466 B - Bracket Sequence

计蒜客 43466
Two great friends, Eddie John and Kris Cross, are attending the Brackets Are Perfection Conference. They wholeheartedly agree with the main message of the conference and they are delighted with all the new things they learn about brackets.One of these things is a bracket sequence. If you want to do a computation with + and ×, you usually write it like so:

(2 × (2 + 1 + 0 + 1) × 1) + 3 + 2.

The brackets are only used to group multiplications and additions together. This means that you can remove all the operators, as long as you remember that addition is used for numbers outside any parentheses! A bracket sequence can then be shortened to

( 2 ( 2 1 0 1 ) 1 ) 3 2.
在这里插入图片描述
That is much better, because it saves on writing all those operators. Reading bracket sequences is easy, too. Suppose you have the following bracket sequence

5 2 ( 3 1 ( 2 2 ) ( 3 3 ) 1 ).

You start with addition, so this is the same as the following:

5 + 2 + ( 3 1 ( 2 2 ) ( 3 3 ) 1 ).

You know the parentheses group a multiplication, so this is equal to

5 + 2 + (3 × 1 × ( 2 2 ) × ( 3 3 ) × 1).
Then there is another level of parentheses: that groups an operation within a multiplication,so the operation must be addition.

5 + 2 + (3 × 1 × (2 + 2) × (3 + 3) × 1) = 5 + 2 + (3 × 1 × 4 × 6 × 1) = 5 + 2 + 72 = 79.

Since bracket sequences are so much easier than normal expressions with operators, it should be easy to evaluate some big ones. We will even allow you to write a program to do it for you.

Note that ( ) is not a valid bracket sequence, nor a subsequence of any valid bracket sequence.

INPUT:

• One line containing a single integer 1 ≤ n ≤ 3 · 10^5.

• One line consisting of n tokens, each being either (, ), or an integer 0 ≤ x < 10^9 + 7.

It is guaranteed that the tokens form a bracket sequence.

OUTPUT:

Output the value of the given bracket sequence. Since this may be very large, you should print it modulo 10^9 + 7.

Sample Input 4-6 Sample Output 4-6
6
( 2 ) ( 3 ) 5
6
( ( 2 3 ) ) 5
11
1 ( 0 ( 583920 ( 2839 82 ) ) ) 1
Sample Input
2
2 3
Sample Output
5
Sample Input 2
8
( 2 ( 2 1 ) ) 3
Sample Output 2
9
Sample Input 3
4
( 12 3 )
Sample Output 3
36

典型的dfs,不要忘了取模,详见代码

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <algorithm>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
//#include <unordered_map>
//#include <bits/stdc++.h>
#define x first
#define y second
typedef long long ll;
#define PII pair<int, int>
const int N = 100100;
using namespace std;
int cnt;
const ll mod = 1e9 + 7;//注意别忘了取模
ll dfs(ll deep) {  //标记这一层是加法还是乘法
    string s;
    ll ret, flag = 0;  // flag用来标记有没有()的情况发生
    ll add;
    if (deep & 1)  //等价于deep%2
        ret = 0;
    else
        ret = 1;
    while (cnt-- && cin >> s) {  //注意顺序不要反了
        if (s == "(") {
            add = dfs(deep + 1);
            if (add == -1) continue;
            flag = 1;
            if (deep & 1)
                ret += add, ret %= mod;
            else
                ret *= add, ret %= mod;
        } else if (s == ")") {
            if (flag)
                return ret % mod;
            else
                return -1;
        } else {
            flag = 1;
            ll f = stoi(s);  //将字符串(string)转为整数
            if (deep & 1)
                ret += f, ret %= mod;
            else
                ret *= f, ret %= mod;
        }
    }
    return ret % mod;
}
int main() {
    cin >> cnt;
    cout << dfs(1) << "\n";
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值