寻找最长有效括号对(leetcode)

Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring.

For "(()", the longest valid parentheses substring is "()", which has length = 2.

Another example is ")()())", where the longest valid parentheses substring is "()()", which has length = 4.

题目即给出一组括号序列,求其最长连续有效括号长度,括号对的组成有多种可能,按照常规思路解题很容易思路混淆

故可以换一种思路解题,即遍历整个序列,如果为‘)',则在其之前的序列中寻找与其对应的第一个’(‘,然后将两个字符都进行标记,

遍历完成后再从头遍历,寻找最长标记序列即可。这种解法虽然时间复杂度为O(n2),如(((()))))这样的序列在找配对时需要回溯较远,

不过在一般情况下均可较好工作。最后附上O(n)解法,需要借助一个自构的栈保存中间变量,不过过程较为复杂了点。

#include
#include
using namespace std;
class LVP{
	public:
	int lvp( string s ){
		int len = s.size();
		if( !len )			//字符串为空则直接返回零。
			return 0;
		for( int i = 0;i < len;i++ )	
			if( s[i] == ')' )	
				for( int j = i - 1;j >= 0;j-- )		// 对其回溯,找出第一个与')'配对的'(',将两者均转化
					if( s[j] == '(' )	{			// 为标记字符'0'
						s[i] = '0';
						s[j] = '0';
						break;
					}
		int max = 0,temp = 0;						
		for( int i = 0;i < len;i++ )	{			// 寻找最长连续标记字符
			if( s[i] == '0' )
				temp++;
			else	{
				max = temp > max ? temp : max;
				temp = 0;
			}
		}
		max = temp > max ? temp : max;				// 最长标记字符可能在最后的temp中
		return max;
	}
};
int main()
{
	string s;
	LVP test;
	while( cin >> s )	
		cout << test.lvp(s) << endl;
	return 0;
}
#include
#include
using namespace std;
// 设置一个结构体变量来保存每次有效括号对的左右边界值
struct mypair{
	int left;
	int right;
};
// 设置一个保存结构体的栈空间,具有findtop,push,pop,max的功能。
class mystack{
	private:
		int top;
		mypair array[100];
	public:
		mystack():top(-1){}
		mypair findtop(){
			return array[top];
		}
		bool push( mypair x )	{
			top++;
			if( top >= 100 )	return false;
			array[top].left = x.left;
			array[top].right = x.right;
			return true;
		}
		mypair pop()	{
			mypair x = array[top];
			top--;
			return x;
		}
		int max(){
			if( top == -1 )	return 0;
			int temp = 0;
			for( int i = 0;i <= top;i++ )
				if( array[i].right - array[i].left + 1 > temp )
					temp = array[i].right - array[i].left + 1;
			return temp;
		}
		void empty()	{	top = -1; }
		bool IsEmpty()	{	return top == -1; }
};
class LVP{
	public:
		int lvp( string s )	{
			int len = s.size();
			if( !len )
				return 0;
			int l = 1,r = 0,index = 0;
			int max = 0;
			enum{ OFF,ON }state;
			state = OFF;
			mypair temp;
			mystack stk;
			for( int i = 0;i < len;i++ )	{
				cout << "locate:" << l << ' ' << r << endl;
				switch( s[i] )	{
					case '(':
						index++;
						if( state )	{
							temp.left = l;
							temp.right = r;
							stk.push(temp);
							state = OFF;
							cout << "in stack: " << l << ' ' << r << endl;
						}		
						l = i + 1;
						break;
					case ')':
						index--;
						state = ON;
						if( index >= 0 )	{						
							r = i;
							l--;
							if( !stk.IsEmpty() && stk.findtop().right + 1 == l )	{
								l = stk.findtop().left;
								stk.pop();
								cout << "happen at " << i << endl;
							}
						}
						else	{
							max = max > r - l + 1 ? max : r - l + 1;
							index = 0;
							stk.empty();
						}
						break;
				}
			}	
			max = max > stk.max() ? max : stk.max();
			max = max > r - l + 1 ? max : r - l + 1;
			return max;
		}
};	
int main()
{
	string s;
	LVP test;
	while( cin >> s )
		cout << test.lvp(s) << endl;
	return 0;
}


你可能感兴趣的:(刷题)