Skip to content

Commit 2d62e3f

Browse files
committed
ABB to BA (Hard) 풀이
1 parent 25c8b62 commit 2d62e3f

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

BAEKJOON/3Gold/ABB to BA (Hard).py

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# 자료 구조, 문자열, 스택
2+
# https://www.acmicpc.net/problem/32293
3+
4+
"""
5+
4
6+
3
7+
ABB
8+
9
9+
ABABABBBB
10+
12
11+
AAAAAABBBBBB
12+
12
13+
AAAABBBBBBBB
14+
15+
BA
16+
BAABA
17+
AAAABABA
18+
ABAAA
19+
"""
20+
21+
import sys
22+
from collections import deque
23+
24+
input = sys.stdin.readline
25+
T = int(input())
26+
A = []
27+
28+
def logic(size, stack1, stack2):
29+
if size > 1:
30+
if stack1[size] == "B" and stack1[size-1] == "B" and stack1[size-2] == "A":
31+
stack2.append("A")
32+
stack2.append("B")
33+
stack1.pop()
34+
stack1.pop()
35+
stack1.pop()
36+
return size-3
37+
return size
38+
39+
for _ in range(T):
40+
l = int(input())
41+
S = list(input().rstrip())
42+
43+
size = -1
44+
stack1 = deque()
45+
stack2 = deque() # 삽입대기
46+
47+
for s in S:
48+
size += 1
49+
stack1.append(s)
50+
51+
if size < 2:
52+
continue
53+
54+
size = logic(size, stack1, stack2)
55+
while stack2:
56+
size += 1
57+
stack1.append(stack2.pop())
58+
size = logic(size, stack1, stack2)
59+
60+
A.append("".join(stack1))
61+
62+
print("\n".join(A))

0 commit comments

Comments
 (0)