AOJ2340 Carpenter's Language

Carpenters' Language

International Carpenters Professionals Company (ICPC) is a top construction company with a lot of expert carpenters. What makes ICPC a top company is their original language. The syntax of the language is simply given in CFG as follows: S -> SS | (S) | )S( | ε In other words, a right parenthesis can be closed by a left parenthesis and a left parenthesis can be closed by a right parenthesis in this language.

ICPC’s languageは以下の法則で作られる.

どんなで構成される文字列も の数が合っていれば構成出来るので, とし合計が になる場合にYES,そうでない場合にNOを出力した.

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <vector>
#include <string>
#include <cstring>
#include <algorithm>
#include <sstream>
#include <map>
#include <set>

#define REP(i,k,n) for(int i=k;i<n;i++)
#define rep(i,n) for(int i=0;i<n;i++)
#define INF 1<<30
#define pb push_back
#define mp make_pair

using namespace std;
typedef long long ll;
typedef pair<int,int> P;

int main() {
  int n;
  cin >> n;

  ll sum = 0;
  rep(i, n) {
      int a, b;
      char c;

      cin >> a >> c >> b;

      if(c == '(') sum += b;
      else sum -= b;

      if(sum == 0) cout << "Yes" << endl;
      else cout << "No" << endl;
  }

  return 0;
}
Mar 22nd, 2016