시간 제한 메모리 제한 제출 정답 맞은 사람 정답 비율
1 초 128 MB 2023 217 143 16.067%

문제

여는 괄호와 닫는 괄호로만 이루어져 있으면서, 다음 규칙을 지키는 문자열을 올바른 괄호 문자열이라고 한다.

  1. 빈 문자열은 올바른 괄호 문자열이다.
  2. A가 올바른 괄호 문자열이라면, (A), [A], {A}도 올바른 괄호 문자열이다.
  3. A와 B가 올바른 괄호 문자열이라면, AB도 올바른 괄호 문자열이다.

예를 들어, [({})], [](){}, [{}]()[{}]는 올바른 괄호 문자열이다. 하지만, [({{)[, []({)}, [{}])([{}]는 올바른 괄호 문자열이 아니다.

상근이는 책상 서랍에서 올바른 괄호 문자열을 적은 종이를 찾았다. 이 종이는 오래되었기 때문에, 일부 문자는 알아 볼 수 없었다.

알아볼 수 없는 문자를 적절히 괄호로 바꿔서 만들 수 있는 올바른 괄호 문자열의 수를 구하는 프로그램을 작성하시오.

입력

첫째 줄에 문자열의 길이 N이 주어진다. (2 ≤ N ≤ 200)

둘째 줄에는 문자열이 주어진다. 알아볼 수 없는 문자는 '?'로 표시되어 있다.

출력

첫째 줄에 만들 수 있는 올바른 괄호 문자열의 수를 출력한다. 개수가 다섯 자리를 넘어가는 경우에는 마지막 다섯 자리만 출력한다.

예제 입력 1

10
(?([?)]?}?

예제 출력 1

3

예제 입력 2

6
()()()

예제 출력 2

1

예제 입력 3

16
???[???????]????

예제 출력 3

92202

힌트

예제 1의 경우 다음이 가능하다. ({([()])}), ()([()]{}), ([([])]{})

W3sicHJvYmxlbV9pZCI6IjMwMTIiLCJwcm9ibGVtX2xhbmciOiIwIiwidGl0bGUiOiJcdWM2MmNcdWJjMTRcdWI5NzggXHVhZDA0XHVkNjM4IFx1YmIzOFx1Yzc5MFx1YzVmNCIsImRlc2NyaXB0aW9uIjoiPHA+XHVjNWVjXHViMjk0IFx1YWQwNFx1ZDYzOFx1YzY0MCBcdWIyZWJcdWIyOTQgXHVhZDA0XHVkNjM4XHViODVjXHViOWNjIFx1Yzc3N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