AOJ1326 Stylish

Stylish

Stylish is a programming language whose syntax comprises names, that are sequences of Latin alphabet letters, three types of grouping symbols, periods ('.'), and newlines. Grouping symbols, namely round brackets ('(' and ')'), curly brackets ('{' and '}'), and square brackets ('[' and ']'), must match and be nested properly.

i番目までの括弧の量を先に計算しておく., , を総当りしてインデントが合う組み合わせを見つける. つ以上ある場合は にする.

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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#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, m;
  while(cin >> n >> m) {
      if(n == 0 && m == 0) break;

      vector<string> v(n), v2(m);
      rep(i, n) cin >> v[i];
      rep(i, m) cin >> v2[i];

      int r[105], c[105], s[105];
      memset(r, 0, sizeof(r));
      memset(c, 0, sizeof(c));
      memset(s, 0, sizeof(s));

      REP(i, 1, n) {
          r[i] = r[i-1];
          c[i] = c[i-1];
          s[i] = s[i-1];
          rep(j, v[i-1].size()) {
              char ch = v[i-1][j];
              if(ch == '(') r[i]++;
              if(ch == ')') r[i]--;
              if(ch == '{') c[i]++;
              if(ch == '}') c[i]--;
              if(ch == '[') s[i]++;
              if(ch == ']') s[i]--;
          }
      }

      int r2[105], c2[105], s2[105];
      memset(r2, 0, sizeof(r2));
      memset(c2, 0, sizeof(c2));
      memset(s2, 0, sizeof(s2));

      REP(i, 1, m) {
          r2[i] = r2[i-1];
          c2[i] = c2[i-1];
          s2[i] = s2[i-1];
          rep(j, v2[i-1].size()) {
              char ch = v2[i-1][j];
              if(ch == '(') r2[i]++;
              if(ch == ')') r2[i]--;
              if(ch == '{') c2[i]++;
              if(ch == '}') c2[i]--;
              if(ch == '[') s2[i]++;
              if(ch == ']') s2[i]--;
          }
      }

      int d[105];
      memset(d, 0, sizeof(d));

      rep(i, n) {
          rep(j, v[i].size()) {
              if(v[i][j] != '.') {
                  d[i] = j;
                  break;
              }
          }
      }

      vector<int> ans(m);
      rep(i, m) ans[i] = INF;

      REP(i, 1, 21) {
          REP(j, 1, 21) {
              REP(k, 1, 21) {
                  bool flag = true;

                  rep(l, n) {
                      if(d[l] == i * r[l] + j * c[l] + k * s[l]) continue;
                      flag = false;
                  }

                  if(flag) {
                      rep(l, m) {
                          int d = i * r2[l] + j * c2[l] + k * s2[l];

                          if(ans[l] == INF) ans[l] = d;
                          else if(ans[l] != d) ans[l] = -1;
                      }
                  }
              }
          }
      }

      rep(i, m) {
          cout << ans[i];
          if(i == m-1) cout << endl;
          else cout << " ";
      }
  }

  return 0;
}
Mar 26th, 2016