結果

問題 No.708 (+ー)の式
ユーザー stone_725stone_725
提出日時 2018-07-03 14:30:39
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 955 bytes
コンパイル時間 2,562 ms
コンパイル使用メモリ 164,288 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-05-07 17:45:34
合計ジャッジ時間 3,154 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

int dfs(string str) {
	stack<int> s;
	stack<char> c;
	int score = 0;
	char nowc = '+';
	for (int i = 0; i < str.size(); i++) {
		if (str[i] == '(') {
			for (int j = i + 1; j < str.size(); j++) {
				if (str[j] == '(') {
					s.push(j);
				}
				if (str[j] == ')') {
					if (s.size()) {
						s.pop();
						continue;
					}
					switch (nowc) {
					case '+':
						score += dfs(str.substr(i + 1, j - i - 1));
						break;
					case '-':
						score -= dfs(str.substr(i + 1, j - i - 1));
					}
					i = j;
				}
			}
		}
		else if (str[i] == '+' || str[i] == '-') {
			nowc = str[i];
		}
		else {
			switch (nowc)
			{
			case '+':
				score += str[i] - '0';
				break;
			case '-':
				score -= str[i] - '0';
				break;
			}
		}
	}
	return score;
}

void hawawa()
{
	string str;
	cin >> str;
	cout << dfs(str) << "\n";
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	hawawa();
	return 0;
}
0