-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJ08021.java
31 lines (29 loc) · 893 Bytes
/
J08021.java
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
import java.util.Scanner;
import java.util.Stack;
public class J08021 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while (t-- > 0) {
String s = sc.next();
Stack<Integer> st = new Stack<>();
st.push(-1);
int res = 0;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == '(') {
st.push(i);
} else {
if (!st.isEmpty()) {
st.pop();
if (!st.isEmpty()) {
res = Math.max(res, i - st.peek());
} else {
st.push(i);
}
}
}
}
System.out.println(res);
}
}
}