Educational Codeforces Round 45 990C. Bracket Sequences Concatenation Problem [思维]

C. Bracket Sequences Concatenation Problem
time limit per test:2 seconds
memory limit per test:256 megabytes
input:standard input
output:standard output

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 nn bracket sequences s1,s2,,sns1,s2,…,sn. Calculate the number of pairs i,j(1i,jn)i,j(1≤i,j≤n) such that the bracket sequence si+sjsi+sj is a regular bracket sequence. Operation ++ means concatenation i.e. "()(" + ")()" = "()()()".

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

Input

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

Output

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

Examples
input
Copy
3
)
()
(
output
Copy
2
input
Copy
2
()
()
output
Copy
4
Note

In the first example, suitable pairs are (3,1)(3,1) and (2,2)(2,2).

In the second example, any pair is suitable, namely (1,1),(1,2),(2,1),(2,2)(1,1),(1,2),(2,1),(2,2).

题目:给n(3e5)个字符串,求(i, j)顺着组合是合法的括号序列的个数。

分析:考虑用降重的思想,用stack将每个序列中规则的括号去掉最后剩下四中序列,①序列为空,②序列只有'(',③序列只有')',④序列包含两种括号任意个。

这样子一分析就很好理解啦,只需要3e5遍历所有字符串,然后(①, ①),(②,③)这样子组合一下就很容易啦。

代码:

 

#include<bits/stdc++.h>
#define ll long long
#include<stack>
#include<map>
#define INF 0x3f3f3f
using namespace std;
const int maxn = 3e5+7;
char y[maxn];
int tot, n;
stack<char> st;
map<ll, ll> mp, mp1; //注意long long,
ll ans = 0;

void solve(char x[]) {
    int len = strlen(x);
    while(!st.empty()) st.pop();
    for(int i = 0; i < len; i++) {
        if(st.empty()) st.push(x[i]);
        else  {
            char c = st.top();
            if(x[i] == ')'&&c == '(') st.pop();
            else st.push(x[i]);
        }
    }
    if(st.empty()) {
        ans++;
        return ;
    }
    len = st.size(); char c = st.top(); st.pop();
    bool f = true;
    while(!st.empty()) {
        char aa = st.top();
        if(aa != c) {
            f = false; break;
        }
        st.pop();
    }
    if(c == '('&&f) mp[len]++;
    if(c == ')'&&f) mp1[len]++;
}

int main()
{
    //freopen("in.txt", "r", stdin);
    while(~scanf("%d", &n)) {
        ans = 0; mp1.clear(); mp.clear();
        for(int i = 0; i < n; i++) scanf("%s", y), solve(y);
        ans = ans*ans;
        for(auto it : mp) {
            int a = it.first, b = it.second;
            ans += b*mp1[a];
        }
        printf("%lld\n", ans);
    }
    return 0;
}


猜你喜欢

转载自blog.csdn.net/weixin_39792252/article/details/80653413