hdu 5357 2015-08-10 22:52 7人阅读 评论(0) 收藏


括号匹配,栈


开始感觉题意很奇怪,表示这道题不会做。。。然而看了题解,原来并不是太难啊~!


我太弱了,还是直接贴代码吧。。。


#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <string>
#include <map>
#include <vector>
#include <stack>
#include <queue>
#include <utility>
#include <iostream>
#include <algorithm>

template<class Num>void read(Num &x)
{
    char c; int flag = 1;
    while((c = getchar()) < '0' || c > '9')
        if(c == '-') flag *= -1;
    x = c - '0';
    while((c = getchar()) >= '0' && c <= '9')
        x = (x<<3) + (x<<1) + (c-'0');
    x *= flag;
    return;
}
template<class Num>void write(Num x)
{
    if(x < 0) putchar('-'), x = -x;
    static char s[20];int sl = 0;
    while(x) s[sl++] = x%10 + '0',x /= 10;
    if(!sl) {putchar('0');return;}
    while(sl) putchar(s[--sl]);
}

const int maxn = 1e6 + 50, Mod = 1e9+7;

char str[maxn];
int T, n, a[maxn], b[maxn];
int pre[maxn], match[maxn];
long long ans[maxn];

int stack[maxn], top;

void prework()
{
    top = 0;

    for(int i = 1; i <= n; i++)
    {
        match[i] = pre[i] = 0;

        if(str[i] == '(')
            stack[++top] = i;
        else
        {
            if(top)
            {
                match[stack[top]] = i;
                match[i] = stack[top];

                if(top > 1) pre[match[i]] = stack[top - 1];

                stack[top--] = 0;
            }
        }
    }

}

void solve()
{
    a[0] = b[n + 1] = ans[0] = 0;

    for(int i = 1; i <= n; i++)
        a[i] = (str[i] == ')' && match[i])?(a[match[i] - 1] + 1):0;

    for(int i = n; i >= 1; i--)
        b[i] = (str[i] == '(' && match[i])?(b[i] = b[match[i] + 1] + 1):0;

    for(int i = 1; i <= n; i++)
         ans[i] = (str[i] == '(')?(ans[pre[i]] + ((long long)b[i]*a[match[i]] % Mod) % Mod):ans[match[i]];  

}

long long calcu()
{
    long long tot = 0;

    for(int i = 1; i <= n; i++)
        tot = tot + ans[i]*i % Mod;

    return tot; 
}


int main()
{
#ifndef ONLINE_JUDGE
    freopen("5357.in","r",stdin);
    freopen("5357.out","w",stdout);
#endif

    read(T);

    while(T--)
    {
        scanf("%s", str + 1);

        n = strlen(str + 1);

        prework(), solve();

        write(calcu()), puts("");
    }

#ifndef ONLINE_JUDGE
    fclose(stdin);
    fclose(stdout);
#endif
    return 0;            
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

posted @ 2015-08-10 22:52  <Dash>  阅读(180)  评论(0编辑  收藏  举报