码迷,mamicode.com
首页 > 其他好文 > 详细

喵哈哈村的括号序列

时间:2017-03-08 23:06:18      阅读:193      评论:0      收藏:0      [点我收藏+]

标签:sample   text   测试数据   data   ret   宝宝   cli   接下来   copy   

描述

喵哈哈村的括号序列和外界的括号序列实际上是一样的。

众所周知"()"这样的,就是一个标准的括号序列;"()()()()"这样也是括号序列;“((()))()”这样也是一个合法的括号序列。但是"((("这样,就不是一个合法的括号序列了。

现在沈宝宝非常好奇,给你一个字符串,请从中找出最长的合法括号序列出来。

不知道你能找到吗?

第一行一个T,表示有T组数据。
接下来T行,每一行都是一个字符串。
保证字符串的长度小于100000。
而且字符串中保证只会出现"(",")"这两种字符之一。
1<=T<=10

对于每一组测试数据,输出最长的合法括号序列的长度。

复制
2
)((())))(()())
)(
6
0

#include <iostream>
#include <stack>
#include <string>
#include <cmath>
#include <math.h>
#include <stdio.h>
using namespace std;
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        string a;
        cin>>a;
        stack<int> s;
        s.push(-1);
        s.push(0);
        for(int i = 1;i < a.length(); i++)
        {
            if(a[s.top()] == ‘(‘ && a[i] == ‘)‘)
            {
                s.pop();
            }
            else{
                s.push(i);
            }
        }
        s.push(a.length());
        int falg = s.top();
        s.pop();
        int cmp;
        int M = -1;
        while(!s.empty())
        {
            cmp = s.top();
            s.pop();
            M = max(M,abs(cmp-falg));
            falg = cmp;
        }
        cout<<M-1<<endl;
    }
    return 0;
}

  

喵哈哈村的括号序列

标签:sample   text   测试数据   data   ret   宝宝   cli   接下来   copy   

原文地址:http://www.cnblogs.com/jxust-jiege666/p/6523136.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!