結果

問題 No.708 (+ー)の式
ユーザー ransewhaleransewhale
提出日時 2018-07-13 23:00:09
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 882 bytes
コンパイル時間 1,665 ms
コンパイル使用メモリ 167,940 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-17 11:22:23
合計ジャッジ時間 2,598 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 1 ms
5,376 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
#define INF (1<<30)
#define INFLL (1ll<<60)
typedef pair<double, int> P;
typedef pair<int, P> E;
#define MOD (1000000007ll)
#define l_ength size
#define EPS (1e-10)

void add_mod(ll &a, ll b){
	a += b;
	a %= MOD;
}

void mul_mod(ll &a, ll b){
	a *= b;
	a %= MOD;
}

string s;
stack<int> t;
int p[123];

int calc(int l, int r){
	int ret=0,d=1,v,i;
	for(i=l; i<=r; ++i){
		if(s[i] == '+'){
			d = 1;
			continue;
		}else if(s[i] == '-'){
			d = -1;
			continue;
		}
		if(s[i] == '('){
			v = calc(i+1,p[i]-1);
			i = p[i];
		}else{
			v = s[i]-'0';
		}
		ret += d*v;
	}
	return ret;
}

int main(void){
	int n,i,q;
	cin >> s;
	n = s.l_ength();
	for(i=0; i<n; ++i){
		if(s[i] == '('){
			t.push(i);
		}else if(s[i] == ')'){
			q = t.top();
			t.pop();
			p[q] = i;
		}
	}
	cout << ((calc(0,n-1))) << endl;
	return 0;
}
0