kikit
7/7/2016 - 5:16 PM

Length of the longest valid substring

Length of the longest valid substring

/*
http://ideone.com/rdL7A2
http://www.geeksforgeeks.org/length-of-the-longest-valid-substring/
http://www.practice.geeksforgeeks.org/problem-page.php?pid=960
*/

#include<bits/stdc++.h>
using namespace std; 

int validSubString(string str){
	int len = str.size();
	stack<char> s;
	s.push(-1);
	int count = 0;
	for(int i=0; i<len; i++){
		if(str[i] == '(')
			s.push(i);
		else{
			s.pop();
			if(!s.empty()){
				count = max(count, i - s.top());
			}else{
				s.push(i);
			}
		}
	}
	return count;
}
int main() {
	int t;
	cin >> t;
	while(t--){
		string s;
		cin >> s;
		cout << validSubString(s) <<endl;
	}
	return 0;
}