前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >CF思维联系– Codeforces-990C Bracket Sequences Concatenation Problem(括号匹配+模拟)

CF思维联系– Codeforces-990C Bracket Sequences Concatenation Problem(括号匹配+模拟)

作者头像
风骨散人Chiam
发布2020-10-29 09:44:45
3690
发布2020-10-29 09:44:45
举报
文章被收录于专栏:CSDN旧文

ACM思维题训练集合 A bracket sequence is a string containing only characters “(” and “)”.

A regular bracket sequence is a bracket sequence that can be transformed into a correct arithmetic expression by inserting characters “1” and “+” between the original characters of the sequence. For example, bracket sequences “()()”, “(())” are regular (the resulting expressions are: “(1)+(1)”, “((1+1)+1)”), and “)(” and “(” are not.

You are given n bracket sequences s1,s2,…,sn. Calculate the number of pairs i,j(1≤i,j≤n) such that the bracket sequence si+sj is a regular bracket sequence. Operation + means concatenation i.e. “()(” + “)()” = “()()()”.

If si+sj and sj+si are regular bracket sequences and i≠j, then both pairs (i,j) and (j,i) must be counted in the answer. Also, if si+si is a regular bracket sequence, the pair (i,i) must be counted in the answer.

Input The first line contains one integer n(1≤n≤3⋅105) — the number of bracket sequences. The following n lines contain bracket sequences — non-empty strings consisting only of characters “(” and “)”. The sum of lengths of all bracket sequences does not exceed 3⋅105.

Output In the single line print a single integer — the number of pairs i,j(1≤i,j≤n) such that the bracket sequence si+sj is a regular bracket sequence.

Examples Input 3 ) () ( Output 2 Input 2 () () Output 4 Note In the first example, suitable pairs are (3,1) and (2,2).

In the second example, any pair is suitable, namely (1,1),(1,2),(2,1),(2,2). 模拟稍微有一下就可以了

代码语言:javascript
复制
#include <bits/stdc++.h>
using namespace std;
template <typename t>
void read(t &x)
{
    char ch = getchar();
    x = 0;
    t f = 1;
    while (ch < '0' || ch > '9')
        f = (ch == '-' ? -1 : f), ch = getchar();
    while (ch >= '0' && ch <= '9')
        x = x * 10 + ch - '0', ch = getchar();
    x *= f;
}

#define wi(n) printf("%d ", n)
#define wl(n) printf("%lld ", n)
#define rep(m, n, i) for (int i = m; i < n; ++i)
#define rrep(m, n, i) for (int i = m; i > n; --i)
#define P puts(" ")
typedef long long ll;
#define MOD 1000000007
#define mp(a, b) make_pair(a, b)
#define N 1005
#define fil(a, n) rep(0, n, i) read(a[i])
//---------------https://lunatic.blog.csdn.net/-------------------//
map<LL, LL> mp;
char con[N];
int main()
{
    LL i, p, j, n, check;
    LL cont = 0, ans = 0, len1, len2;
    scanf("%lld", &n);
    getchar();
    for (j = 1; j <= n; j++)
    {
        p = check = 0;
        len1 = len2 = 0;
        memset(con, 0, sizeof(0));
        scanf("%s", con);
        for (i = 0; i < 300009; i++)
        {
            if (con[i] == 0)
                break;
            if (con[i] == '(')
            {
                len1++;
                p++;
            }
            else
            {
                p--;
                if (len1)
                    len1--;
                else
                    len2++;
            }
        }
        if (len1 == 0 && len2 == 0)
            cont++;
        else
        {
            if (len1 == 0)
                mp[p]++;
            if (len2 == 0)
                mp[p]++;
        }
    }
    ans = cont * cont;

    map<LL, LL>::iterator it1;
    for (it1 = mp.begin(); it1 != mp.end(); it1++)
    {
        if (it1->first > 0)
            break;
        if (mp[-(it1->first)] > 0)
            ans += (it1->second) * mp[-(it1->first)];
    }
    printf("%lld\n", ans);
    return 0;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020/03/01 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档