-
-
Save avijitagarwal/4db4c75845d4fbddb264147fb28c238b 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
#include<bits/stdc++.h> | |
using namespace std; | |
const int MAXN = 1e6 + 5; | |
const long long mod = 1e9 + 7; | |
int n, q; | |
string s; | |
int queries[MAXN]; | |
void solve(){ | |
n = s.length(); | |
stack<int> st; | |
vector<int> nxt(n, -2); | |
int i = 0; | |
for(i = 0; i < n; i++){ | |
if(s[i] == '(') | |
st.push(i); | |
else if(st.size() > 0) | |
nxt[st.top()] = i, st.pop(); | |
} | |
for(i = n - 2; i >= 0; i--){ | |
if(s[i] == ')') | |
nxt[i] = nxt[i + 1]; | |
} | |
for(i = 0; i < q; i++) | |
cout << nxt[queries[i] - 1] + 1 << "\n"; | |
} | |
int main(){ | |
ios_base::sync_with_stdio(false); | |
cin.tie(NULL); | |
int t; | |
cin >> t; | |
while(t--){ | |
cin >> s >> q; | |
int i = 0; | |
for(i = 0; i < q; i++) | |
cin >> queries[i]; | |
solve(); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment