QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#53351#2927. Bracket Pairingthomas_harmeyerAC ✓70ms3732kbC++1.5kb2022-10-04 23:43:062022-10-04 23:43:06

Judging History

This is the latest submission verdict.

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-10-04 23:43:06]
  • Judged
  • Verdict: AC
  • Time: 70ms
  • Memory: 3732kb
  • [2022-10-04 23:43:06]
  • Submitted

answer

#include <bits/stdc++.h>
using namespace std;

#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define repo(i, b) for (int i = 0; i < (b); ++i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

bool isOpen(char c) { return c == '(' || c == '{' || c == '<' || c == '['; }
char other(char c) { return c == '(' ? (char)')' : (c + 2); }

ll solve() {
  string s;
  cin >> s;
  ll ans = 0;
  int q = [&]() -> int {
    int cnt = 0;
    for (char& c : s) {
      if (c == '?') cnt++;
    }
    return cnt;
  }();
  ll big = 1 << q;
  // for every combination of open/close for '?'
  rep(i, 0, big) {
    bitset<20> bs(i);
    int ind = 0;
    ans += [&]() -> ll {
      ll res = 1;
      stack<char> st;
      for (char c : s) {
        if (isOpen(c)) {
          st.push(c);
        } else if (c != '?') {
          if (st.empty()) {
            return 0;
          }
          if (c == other(st.top()) || st.top() == '?') {
            st.pop();
          } else {
            return 0;
          }
        } else {
          if (bs.test(ind++)) {
            if (st.empty()) {
              return 0;
            }
            if (st.top() == '?') {
              res *= 4;
            }
            st.pop();
          } else {
            st.push('?');
          }
        }
      }
      if (st.empty()) {
        return res;
      } else
        return 0;
    }();
  }
  return ans;
}

int main() {
  cin.tie(0)->sync_with_stdio(0);
  cin.exceptions(cin.failbit);
  cout << solve();
  return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 3680kb

input:

??

output:

4

result:

ok single line: '4'

Test #2:

score: 0
Accepted
time: 2ms
memory: 3564kb

input:

????

output:

32

result:

ok single line: '32'

Test #3:

score: 0
Accepted
time: 2ms
memory: 3544kb

input:

????????

output:

3584

result:

ok single line: '3584'

Test #4:

score: 0
Accepted
time: 2ms
memory: 3540kb

input:

]????]

output:

0

result:

ok single line: '0'

Test #5:

score: 0
Accepted
time: 2ms
memory: 3620kb

input:

?????[

output:

0

result:

ok single line: '0'

Test #6:

score: 0
Accepted
time: 2ms
memory: 3560kb

input:

([???]

output:

1

result:

ok single line: '1'

Test #7:

score: 0
Accepted
time: 2ms
memory: 3636kb

input:

??({?]

output:

0

result:

ok single line: '0'

Test #8:

score: 0
Accepted
time: 1ms
memory: 3636kb

input:

??({?]??

output:

4

result:

ok single line: '4'

Test #9:

score: 0
Accepted
time: 2ms
memory: 3560kb

input:

?[()?)()]]

output:

1

result:

ok single line: '1'

Test #10:

score: 0
Accepted
time: 2ms
memory: 3640kb

input:

?()?????

output:

320

result:

ok single line: '320'

Test #11:

score: 0
Accepted
time: 2ms
memory: 3676kb

input:

?????()?

output:

320

result:

ok single line: '320'

Test #12:

score: 0
Accepted
time: 0ms
memory: 3716kb

input:

???([]){}???

output:

320

result:

ok single line: '320'

Test #13:

score: 0
Accepted
time: 70ms
memory: 3620kb

input:

????????????????????

output:

17611882496

result:

ok single line: '17611882496'

Test #14:

score: 0
Accepted
time: 2ms
memory: 3564kb

input:

<{[{()}]}<>{}>{?()[]

output:

1

result:

ok single line: '1'

Test #15:

score: 0
Accepted
time: 2ms
memory: 3636kb

input:

[<{}<><?[]>?[]{}<{}>

output:

1

result:

ok single line: '1'

Test #16:

score: 0
Accepted
time: 0ms
memory: 3512kb

input:

()<{}{}><???[]>>[()]

output:

9

result:

ok single line: '9'

Test #17:

score: 0
Accepted
time: 0ms
memory: 3564kb

input:

([{?}}<??){<>}[?}<>]

output:

1

result:

ok single line: '1'

Test #18:

score: 0
Accepted
time: 1ms
memory: 3680kb

input:

{}(?(?()[]??]<>){[]?

output:

21

result:

ok single line: '21'

Test #19:

score: 0
Accepted
time: 0ms
memory: 3560kb

input:

<??>]??{{}[]}>>()?{}

output:

1

result:

ok single line: '1'

Test #20:

score: 0
Accepted
time: 2ms
memory: 3556kb

input:

{???{(({?><>?>})){}}

output:

8

result:

ok single line: '8'

Test #21:

score: 0
Accepted
time: 1ms
memory: 3552kb

input:

?<()[]?<{?>??>>())[]

output:

11

result:

ok single line: '11'

Test #22:

score: 0
Accepted
time: 0ms
memory: 3568kb

input:

?[()?)()][]{[?}>?]{?

output:

1

result:

ok single line: '1'

Test #23:

score: 0
Accepted
time: 2ms
memory: 3552kb

input:

{?(?>)<>>{}<?{}}?]{?

output:

1

result:

ok single line: '1'

Test #24:

score: 0
Accepted
time: 1ms
memory: 3716kb

input:

[?}[[???[??????]??>?

output:

34880

result:

ok single line: '34880'

Test #25:

score: 0
Accepted
time: 3ms
memory: 3560kb

input:

??<???????<>??[??]{?

output:

629760

result:

ok single line: '629760'

Test #26:

score: 0
Accepted
time: 4ms
memory: 3564kb

input:

?????????[?)?}?>[???

output:

889856

result:

ok single line: '889856'

Test #27:

score: 0
Accepted
time: 16ms
memory: 3652kb

input:

????????????}(??????

output:

275382272

result:

ok single line: '275382272'

Test #28:

score: 0
Accepted
time: 7ms
memory: 3648kb

input:

?[??]?{??)??????????

output:

8609792

result:

ok single line: '8609792'

Test #29:

score: 0
Accepted
time: 6ms
memory: 3716kb

input:

???????(??)??{?????>

output:

16621568

result:

ok single line: '16621568'

Test #30:

score: 0
Accepted
time: 7ms
memory: 3712kb

input:

????[]?[<???????????

output:

21446656

result:

ok single line: '21446656'

Test #31:

score: 0
Accepted
time: 6ms
memory: 3628kb

input:

???????}>??????)?)??

output:

4677632

result:

ok single line: '4677632'

Test #32:

score: 0
Accepted
time: 4ms
memory: 3732kb

input:

[??????(??????????{}

output:

41435136

result:

ok single line: '41435136'

Test #33:

score: 0
Accepted
time: 7ms
memory: 3648kb

input:

(?>?[???????????}???

output:

8740864

result:

ok single line: '8740864'

Test #34:

score: 0
Accepted
time: 1ms
memory: 3644kb

input:

()()(?

output:

1

result:

ok single line: '1'

Test #35:

score: 0
Accepted
time: 2ms
memory: 3636kb

input:

(??)

output:

5

result:

ok single line: '5'

Test #36:

score: 0
Accepted
time: 0ms
memory: 3716kb

input:

(<{}>??]

output:

1

result:

ok single line: '1'

Test #37:

score: 0
Accepted
time: 2ms
memory: 3728kb

input:

(?]]

output:

0

result:

ok single line: '0'