-
-
Save avijitagarwal/6f1774fb85ec517ed5439cb1d99348be to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.IOException; | |
import java.io.InputStream; | |
import java.util.Stack; | |
class CLBRKT { | |
public static void main(String[] args) throws IOException{ | |
int i,j; | |
FastReader in=new FastReader(System.in); | |
StringBuilder sb=new StringBuilder(); | |
int t=in.nextInt(); | |
while(t-->0){ | |
char arr[]=in.next().toCharArray(); | |
int n=arr.length; | |
int brac[]=new int[n+1]; | |
int ans[]=new int[n+1]; | |
int next=n+1; | |
for(i=n-1;i>=0;i--){ | |
if(arr[i]=='(') | |
next=i+1; | |
brac[i+1]=next; | |
} | |
Stack<Integer> stack=new Stack<>(); | |
for(i=0;i<n;i++){ | |
if(arr[i]=='(') | |
stack.push(i+1); | |
else if(!stack.isEmpty()) | |
ans[stack.pop()]=i+1; | |
} | |
int q=in.nextInt(); | |
while(q-->0){ | |
int x=in.nextInt(); | |
int st=brac[x]; | |
if(st==n+1) | |
sb.append("-1\n"); | |
else{ | |
if(ans[st]==0) | |
sb.append("-1\n"); | |
else | |
sb.append(ans[st]).append("\n"); | |
} | |
} | |
} | |
System.out.print(sb); | |
} | |
} | |
class FastReader { | |
byte[] buf = new byte[2048]; | |
int index, total; | |
InputStream in; | |
FastReader(InputStream is) { | |
in = is; | |
} | |
int scan() throws IOException { | |
if (index >= total) { | |
index = 0; | |
total = in.read(buf); | |
if (total <= 0) { | |
return -1; | |
} | |
} | |
return buf[index++]; | |
} | |
String next() throws IOException { | |
int c; | |
for (c = scan(); c <= 32; c = scan()) ; | |
StringBuilder sb = new StringBuilder(); | |
for (; c > 32; c = scan()) { | |
sb.append((char) c); | |
} | |
return sb.toString(); | |
} | |
int nextInt() throws IOException { | |
int c, val = 0; | |
for (c = scan(); c <= 32; c = scan()) ; | |
boolean neg = c == '-'; | |
if (c == '-' || c == '+') { | |
c = scan(); | |
} | |
for (; c >= '0' && c <= '9'; c = scan()) { | |
val = (val << 3) + (val << 1) + (c & 15); | |
} | |
return neg ? -val : val; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment