-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathabc307_d.py
More file actions
87 lines (84 loc) · 2.05 KB
/
abc307_d.py
File metadata and controls
87 lines (84 loc) · 2.05 KB
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
###############################################
###############################################
###############################################
###############################################
###############################################
###############################################
###############################################
###############################################
[my ai AC]
N=int(input())
S=list(input())
opencount=0
T=[]
for s in S:
if s=='(':
opencount+=1
T+=[s]
elif s==')':
if opencount>0:
while T:
t=T.pop()
if t!='(':
continue
else:
opencount-=1
break
else:
T+=[s]
else:
T+=[s]
print(''.join(T))
###############################################
[ai AC]
N = int(input())
S = input()
result = []
open_count = 0
for char in S:
if char == '(':
open_count += 1
result.append(char)
elif char == ')':
if open_count > 0:
open_count -= 1
# 対応する'('までを削除
while result and result[-1] != '(':
result.pop()
if result:
result.pop() # '('を削除
else:
result.append(char)
else:
result.append(char)
print(''.join(result))
###############################################
###############################################
###############################################
###############################################
[my WA]
N=int(input())
S=list(input())
stack=[]
for i,s in enumerate(S):
if s=='(':
stack+=[i]
elif s==')':
if stack:
idx=stack.pop()
for j in range(idx,i+1):
S[j]='#'
else:
break
ans=''
for s in S:
if s!='#':
ans+=s
print(ans)
###############################################
###############################################
###############################################
###############################################
###############################################
###############################################
###############################################