CodeForces - 5C (thinking + matching brackets)

The meaning of problems

https://vjudge.net/problem/CodeForces-5C

A bracket given sequence, and obtains its longest substring legitimate number. Legal definition: this sequence about matching brackets.

Thinking

The general problem and the bracket matching distinct, parallel bracket matching may also be present, such as () () (), which is the length of the answer is 6.

If a matching record for each array location, with a stack simulation, when confronted with a '(' subscript stack directly encounter '') to see if there are no stacks '(', and if this location there will he matched position (top of the stack) is set to 10 and then pop, not to continue.

This array is then a 01, and you can find the longest continuous 1, because 1 indicates that the position can be matched.

Code

#include <bits/stdc++.h>
using namespace std;
#define inf 0x3f3f3f3f
#define ll long long
const int N = 1e6 + 5;
const int mod = 1e9 + 7;
const double eps = 1e-8;
const double PI = acos(-1.0);
#define lowbit(x) (x & (-x))
int a[N];
int main()
{
    std::ios::sync_with_stdio(false);
    string s;
    cin >> s;
    stack<int> st;
    int l = s.length();
    for (int i = 0; i < l; i++)
    {
        if (s[i] == '(')
            st.push(i);
        else
        {
            if (st.size())
                a[st.top()] = a[i] = 1, st.pop();
        }
    }
    int mx = 0, cnt = 0;
    map<int, int> mp;
    for (int i = 0; i < l; i++)
    {
        if (a[i])
        {
            cnt++;
        }
        else
        {
            if (cnt >= mx)
            {
                mx = cnt;
                mp[mx]++;
            }
            cnt = 0;
        }
    }
    if (cnt >= mx)
        mx = cnt, mp[mx]++;
    if (mx == 0)
        mp[mx] = 1;
    cout << mx << " " << mp[mx] << endl;
    return 0;
}

  

Guess you like

Origin www.cnblogs.com/mcq1999/p/12028663.html