zoukankan      html  css  js  c++  java
  • 括号匹配 CodeForces 5C

    Description

    This is yet another problem dealing with regular bracket sequences.

    We should remind you that a bracket sequence is called regular, if by inserting «+» and «1» into it we can get a correct mathematical expression. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not.

    You are given a string of «(» and «)» characters. You are to find its longest substring that is a regular bracket sequence. You are to find the number of such substrings as well.

    Input

    The first line of the input file contains a non-empty string, consisting of «(» and «)» characters. Its length does not exceed 106.

    Output

    Print the length of the longest substring that is a regular bracket sequence, and the number of such substrings. If there are no such substrings, write the only line containing "0 1".

    Sample Input

    Input
    )((())))(()())
    Output
    6 2
    Input
    ))(
    Output
    0 1
    #include <cstdio>
    #include <queue>
    #include <stack>
    #include <cmath>
    #include <cstring>
    #include <cstdlib>
    #include <iostream>
    #include <algorithm>
    using namespace std;
    typedef long long LL;
    const int oo = 1e9;
    const double PI = acos(-1);
    const int N = 1e6+7;
    char str[N];
    int len[N];/**< 如果这个位置是')' 保存从字符串头部开始到这个位置的最大匹配数量 */
    int main()
    {
        int i, ans=0, sum=0;
        stack <int >sta;
        scanf("%s", str);
        int M = strlen(str);
        for(i = 0; i < M; i++)
        {
            if(str[i] == '(') sta.push(i);
            else if(sta.size())
            {
                int top = sta.top();
    
                sta.pop();
    
                if(str[top-1] == ')')
                    len[i] = (i-top+1) + len[top-1];
    
                else
                    len[i] = (i-top+1);
    
    
                ans = max(ans, len[i]);
            }
        }
        for(i = M-1; i >= 0; i--)
        {
            if(str[i] == ')' && len[i] && len[i] == ans)
            sum++;
        }
        if(sum == 0) sum = 1;
        printf("%d %d
    ", ans, sum);
        return 0;
    }
    

      

  • 相关阅读:
    kyeremal-bzoj2038-[2009国家集训队]-小z的袜子(hose)-莫队算法
    移位操作之旋转移位
    leetcode 二分查找 Search in Rotated Sorted Array
    背包算法练习--求小于某数字的数组最大和:
    Splunk 会议回想: 大数据的关键是机器学习
    CSDN个人空间、问答频道停站维护公告
    HDFS主要节点解说(一)节点功能
    24点
    【Nginx】事件驱动框架和异步处理
    OC中字符串的提取与替换-四种不同方法实现
  • 原文地址:https://www.cnblogs.com/PersistFaith/p/4853109.html
Copyright © 2011-2022 走看看