Skip to content

Instantly share code, notes, and snippets.

@avijitagarwal
Created July 13, 2020 17:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save avijitagarwal/4db4c75845d4fbddb264147fb28c238b to your computer and use it in GitHub Desktop.
Save avijitagarwal/4db4c75845d4fbddb264147fb28c238b to your computer and use it in GitHub Desktop.
#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