結果
| 問題 |
No.457 (^^*)
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2016-12-20 22:42:41 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 27 ms / 2,000 ms |
| コード長 | 4,099 bytes |
| コンパイル時間 | 1,984 ms |
| コンパイル使用メモリ | 169,168 KB |
| 実行使用メモリ | 6,820 KB |
| 最終ジャッジ日時 | 2024-12-14 11:37:01 |
| 合計ジャッジ時間 | 2,707 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 20 |
ソースコード
#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;
}