結果

問題 No.457 (^^*)
ユーザー naimonon77naimonon77
提出日時 2016-12-20 22:42:41
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 26 ms / 2,000 ms
コード長 4,099 bytes
コンパイル時間 1,598 ms
コンパイル使用メモリ 168,844 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-05-08 08:50:43
合計ジャッジ時間 2,435 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 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 5 ms
5,376 KB
testcase_11 AC 23 ms
5,376 KB
testcase_12 AC 25 ms
5,376 KB
testcase_13 AC 24 ms
5,376 KB
testcase_14 AC 3 ms
5,376 KB
testcase_15 AC 26 ms
5,376 KB
testcase_16 AC 25 ms
5,376 KB
testcase_17 AC 2 ms
5,376 KB
testcase_18 AC 2 ms
5,376 KB
testcase_19 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES

#include "bits/stdc++.h"

#define REP(i,a,b) for(int i=a;i<b;++i)
#define rep(i,n) REP(i,0,n)
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
#define all(a) begin(a),end(a)
#define ifnot(a) if(not (a))
#define dump(x)  cerr << #x << " = " << (x) << endl
using namespace std;

// #define int ll
#ifdef _MSC_VER
const bool test = true;
#else 
const bool test = false;
#endif

int dx[] = { 0,1,0,-1 };
int dy[] = { 1,0,-1,0 };
#define LINF ((ll)1 << 60)
#define INF (1 << 28)
ull mod = (int)1e9 + 7;
//.....................
const int MAX = (int)2e5 + 5;

string s;

void ch_parenthesis(int start, int& right_parenthesis_id
,int& right_parenthesis_cnt) {
	right_parenthesis_id = INF;
	REP(i, start + 1, s.size()) {
		if (s[i] == ')') {
			// right_parenthesis_cnt--;
			right_parenthesis_id = i; return;
		}
	}
}

void chstar(int start, int& star_id, int &right_parenthesis_id
,int& right_parenthesis_cnt) {
	star_id = INF;
	REP(i, start+1, s.size()) {
		if (s[i] == '*') {
			star_id = i; break;
		}
		if (i >= right_parenthesis_id && s[i] == ')') {
			right_parenthesis_cnt--;
		}
	}
	if (right_parenthesis_id < star_id) {
		// cerr << "!" << endl;
		ch_parenthesis(star_id, right_parenthesis_id,
			right_parenthesis_cnt);
	}
}


ll left_calc() {
	ll res = 0;
	int hat_id1 = INF;
	int hat_id2 = INF;
	int star_id = INF;
	int right_parenthesis_id = INF;
	int right_parenthesis_cnt = 0;
	rep(i, s.size()) {
		if (s[i] == '^') {
			if (hat_id1 == INF) hat_id1 = i;
			else if (hat_id2 == INF) hat_id2 = i;
		}
		if (s[i] == '*' && hat_id1 != INF && hat_id2 != INF) {
			if (star_id == INF) {
				star_id = i;
			}
		}
		if (s[i] == ')' &&
			hat_id1 != INF && hat_id2 != INF && 
			star_id != INF) {
			if (right_parenthesis_id == INF) {
				right_parenthesis_id = i;
			}
			right_parenthesis_cnt++;
		}
	}
	rep(i, s.size()) {
		if (test) {
			dump(i);
			dump(s[i]);
			dump(hat_id1);
			dump(hat_id2);
			dump(star_id);
			dump(right_parenthesis_id);
			dump(right_parenthesis_cnt);
			dump(res);
			cerr << endl;
		}
		if (s[i] == '^') {
			hat_id1 = hat_id2;
			hat_id2 = INF;
			REP(i, hat_id1 + 1, s.size()) {
				if (s[i] == '^') {
					hat_id2 = i; break;
				}
				if (i >= right_parenthesis_id && s[i] == ')') {
					right_parenthesis_cnt--;
				}
			}
			if (star_id < hat_id2) {
				chstar(hat_id2, star_id, right_parenthesis_id,
					right_parenthesis_cnt);
			}
		}

		if (s[i] == '*') {
			if (star_id <= i) {
				chstar(star_id, star_id, right_parenthesis_id,
					right_parenthesis_cnt);
			}
		}
		if (s[i] == ')') {
			if (right_parenthesis_id <= i) {
				ch_parenthesis(right_parenthesis_id,
					right_parenthesis_id,
					right_parenthesis_cnt);
			}
		}
		if (s[i] == '(' && hat_id1 != INF && hat_id2 != INF &&
			star_id != INF) {
			res += right_parenthesis_cnt;
		}
	}
	dump(right_parenthesis_cnt);
	return res;
}

//int left_calc() {
//	int res = 0;
//	rep(i, s.size()) {
//		if (s[i] == '(') {
//			int stage = 0;
//		  int right_parenthesis_cnt = 0;
//			REP(j, i, s.size()) {
//				if (stage == 0 && s[j] == '^') stage = 1;
//				else if (stage == 1 && s[j] == '^') stage = 2;
//				else if (stage == 2 && s[j] == '*') stage = 3;
//				else if (stage == 3 && s[j] == ')') {
//					right_parenthesis_cnt++;
//				}
//			}
//			res += right_parenthesis_cnt;
//		}
//	}
//	return res;
//}

int right_calc() {
	int res = 0;
	rep(i, s.size()) {
		if (s[i] == '(') {
			int stage = 0;
			int right_parenthesis_cnt = 0;
			REP(j, i, s.size()) {
				if (stage == 0 && s[j] == '*') stage = 1;
				else if (stage == 1 && s[j] == '^') stage = 2;
				else if (stage == 2 && s[j] == '^') stage = 3;
				else if (stage == 3 && s[j] == ')') {
					right_parenthesis_cnt++;
				}
			}
			res += right_parenthesis_cnt;
		}
	}
	return res;
}

void solve() {
	cin >> s;
	ll res_left = left_calc();
	ll res_right = right_calc();
	cout << res_left << " " << res_right << endl;
}

signed main() {
	freopen("debug_log.txt", "w", stderr);
	cout << fixed << setprecision(20);
	rep(i, 1) solve();
	return 0;
}
0