日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2023/12/15 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 CF思维联系– Codeforces-990C Bracket Sequences Concatenation Problem(括号匹配+模拟) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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).
模擬稍微有一下就可以了

#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--;elselen2++;}}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; } 創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的CF思维联系– Codeforces-990C Bracket Sequences Concatenation Problem(括号匹配+模拟)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。