1. 程式人生 > >Bracket Sequences Concatenation Problem CodeForces - 990C(括號匹配水題)

Bracket Sequences Concatenation Problem CodeForces - 990C(括號匹配水題)

const concat inf sequence 說明 con cin racket 統計

明確一下 一個字符串有x左括號不匹配 和 另一個字符串有x個右括號不匹配 這倆是一定能夠匹配的

腦子有點迷 emm。。。

所以統計就好了 統計x個左括號的有幾個,x個右括號的有幾個 然後 乘一下

如果一個串 同時存在左右括號都不匹配的情況 則忽略 因為這個串需要另外兩個括號去匹配

不要忘了處理左右括號已經匹配的情況

#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6+5, INF = 0x7fffffff;
typedef long long LL;
LL a[maxn], b[maxn];
string
str; int main() { int n; cin>> n; int lef, rig, maxx = -INF; for(int i=0; i<n; i++) { cin>> str; int len = str.size(); maxx = max(len, maxx); lef = INF; rig = 0; for(int j=0; j<len; j++) { if(str[j] ==
() rig++; else rig--; lef = min(lef, rig); } if(lef < 0 && rig > lef) //說明兩邊同時存在不匹配的左右括號 continue; if(lef < 0) //有多的不匹配的左括號 b[abs(lef)]++; else //
有多的不匹配的右括號 a[rig]++; } LL res = 0; for(int i=1; i<=maxx; i++) res += a[i] * b[i]; res += a[0] * a[0]; cout<< res <<endl; return 0; }

Bracket Sequences Concatenation Problem CodeForces - 990C(括號匹配水題)