【2022 icpc 网络赛】

H Step Debugging

Rikka is a contestant in C-- Collegiate Programming Contest (CCPC), a competition for writing programs using a popular language C--.

A C-- expression is inductively defined in the following way.

  • arithmetic performs some arithmetic calculations.

  • library invokes a function in the standard library of C--.

  • For any C-- expression e and integer w∈[1,100], repeat e for w times repeats expression e for w times.

  • For any two C-- expressions e1​,e2​, e1​ e2​ runs expressions e1​,e2​ in order.

A C-- program is a C-- expression followed by token fin, representing the end of the program.

Today, Rikka takes part in the online selection of CCPC. She writes a C-- program, but it fails in passing all test cases. Now, Rikka wants to
figure out what goes wrong in this program using step debugging. However, such a choice is risky: Stepping into a library function of C-- may be judged as cheating.

Therefore, before debugging, Rikka wants to mature the risk of stepping into library functions. She wants to calculate how many times the C-- program invokes library functions.

输入格式:

The input contains a single line, representing the C-- program written by Rikka.

The input guarantees the following four points.

  • The input C-- program is valid.

  • The input file is no larger than 10kB.

  • There is no extra space. Concretely, there is no leading space and succeeding space, and there is exactly one space between any two tokens.

  • There is a line break right after the end of the program, i.e., token fin.

输出格式

Output a single number, representing the times of invoking library functions.

The answer may be very large. You only need to output the answer module 20220911.

输入样例:

library arithmetic library library fin
repeat library arithmetic for 5 times library arithmetic fin
repeat library repeat repeat library for 3 times arithmetic library for 3 times for 100 times fin
repeat library repeat arithmetic library for 3 times repeat library for 2 times arithmetic for 4 times arithmetic fin

输出样例:

3
6
1300
24

#include <bits/stdc++.h>

# define IO ios::sync_with_stdio(false);
# define Icin cin.tie(0);cout.tie(0);
# define int long long
# define fi first
# define se second
# define endl '\n'
# define pb push_back

using namespace std;

typedef long long ll;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;

const int N=3e5+10,M=18,mod=20220911,inf=0x3f3f3f3f;
const double eps=1e-8;

int n;

stack<int> num;
stack<char> op;

int calculate(string s) {
        int n = s.size(), num = 0, curRes = 0, res = 0;
        char op = '+';
        for (int i = 0; i < n; ++i) {
            char c = s[i];
            if (c >= '0' && c <= '9') {
                num = num * 10 + c - '0';
            } else if (c == '(') {
                int j = i, cnt = 0;
                for (; i < n; ++i) {
                    if (s[i] == '(') ++cnt;
                    if (s[i] == ')') --cnt;
                    if (cnt == 0) break;
                }
                num = calculate(s.substr(j + 1, i - j - 1));
            }
            if (c == '+' || c == '-' || c == '*' || c == '/' || i == n - 1) {
                switch (op) {
                    case '+': curRes += num; break;
                    case '-': curRes -= num; break;
                    case '*': curRes *= num; break;
                    case '/': curRes /= num; break;
                }
                if (c == '+' || c == '-' || i == n - 1) {
                    res += curRes;
                    curRes = 0;
                }
                op = c;
                num = 0;
            }
        }
        return res%mod;
    }

void slove(int icx)
{
	string s,a;
	
	while(cin>>s)
	{
		if(isdigit(s[0]))
			a+=s;
		else if(s=="repeat")
			a+="+(";
		else if(s=="for")
			a+=")*";
		else if(s=="library")
			a+="+1";
	}
//	cout<<a<<endl;
	string b;
	for(unsigned int i=0;i<a.size();i++)
	{
		if(a[i]=='+')
			if(!(i-1>=0&&isdigit(a[i-1])))
			continue;
		b+=a[i];
	 } 
	 
	 cout<<calculate(b)%mod<<endl;

}
signed main()
{
	IO
	Icin
//	cout<<endl;
	int t=1;
//	scanf("%d",&t);
//	cin>>t;

	for(int i=1; i<=t; i++)
		slove(i);

	return 0;
}





参考:[LeetCode] 772. Basic Calculator III 基本计算器之三 - Grandyang - 博客园 (cnblogs.com)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值