C. 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 n

bracket sequences s1,s2,,sn. Calculate the number of pairs i,j(1i,jn) 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 ij, 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(1n3105)

— 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 3105

.

Output

In the single line print a single integer — the number of pairs i,j(1i,jn)

such that the bracket sequence si+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)

and (2,2)

.

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

.

思路:在存的时候就消掉左右括号。

注意要用longlong

代码:

#include<bits/stdc++.h>
using namespace std;
string h;
struct node
{
    long long l,r;
}X[300010];
long long lala_r[300010];
long long lala_l[300010];
int main()
{
    int n;
    scanf("%d",&n);
    memset(X,0,sizeof(X));
    memset(lala_r,0,sizeof(lala_r));
    memset(lala_l,0,sizeof(lala_l));
    long long cnt1=0;
    long long maxx=0;
    for(int i=1;i<=n;i++)
    {
        cin>>h;
        for(int j=0;j<h.length();j++){
            if(h[j]=='(')
                X[i].r++;
            else
            {
                if(X[i].r>=1)
                    X[i].r--;
                else
                {
                    X[i].l++;
                }
            }
        }
        maxx=max(max(maxx,X[i].l),X[i].r);
        ///cout<<X[i].l<<"%%%%%%"<<X[i].r<<endl;
        if(X[i].l==0&&X[i].r==0)
            cnt1++;
        else if(X[i].l!=0&&X[i].r!=0)
            continue;
        else{
            if(X[i].l==0&&X[i].r!=0)
                lala_r[X[i].r]++;
            else if(X[i].r==0&&X[i].l!=0)
                lala_l[X[i].l]++;
        }
    }
    cnt1=cnt1*cnt1;
   /// cout<<cnt1<<"&&&"<<endl;
    for(int i=1;i<=maxx;i++)
    {
       if(lala_r[i]>0&&lala_l[i]>0)
            cnt1=cnt1+lala_r[i]*lala_l[i];
    }
    cout<<cnt1<<endl;
    return 0;
}

帮忙把keycodes.lua文件里的所有按键都补上, 用16进制; local M = {} -- 字母键 M.A = 65; M.B = 66; M.C = 67; M.D = 68; M.E = 69; M.F = 70; M.G = 71 M.H = 72; M.I = 73; M.J = 74; M.K = 75; M.L = 76; M.M = 77; M.N = 78 M.O = 79; M.P = 80; M.Q = 81; M.R = 82; M.S = 83; M.T = 84; M.U = 85 M.V = 86; M.W = 87; M.X = 88; M.Y = 89; M.Z = 90 -- 数字键 M.ZERO = 48; M.ONE = 49; M.TWO = 50; M.THREE = 51; M.FOUR = 52 M.FIVE = 53; M.SIX = 54; M.SEVEN = 55; M.EIGHT = 56; M.NINE = 57 -- 功能键 M.F1 = 112; M.F2 = 113; M.F3 = 114; M.F4 = 115; M.F5 = 116; M.F6 = 117 M.F7 = 118; M.F8 = 119; M.F9 = 120; M.F10 = 121; M.F11 = 122; M.F12 = 123 -- 控制键 M.BACKSPACE = 8 -- 退格键 M.TAB = 9 -- 制表符 M.ENTER = 13 -- 回车键 M.SHIFT = 16 -- Shift键 M.CTRL = 17 -- Control键 M.ALT = 18 -- Alt键 M.PAUSE = 19 -- 暂停键 M.CAPSLOCK = 20 -- 大写锁定键 M.ESCAPE = 27 -- ESC键 M.SPACE = 32 -- 空格键 M.PAGEUP = 33 -- PageUp M.PAGEDOWN = 34 -- PageDown M.END = 35 -- End键 M.HOME = 36 -- Home键 M.LEFT = 37 -- 左箭头 M.UP = 38 -- 上箭头 M.RIGHT = 39 -- 右箭头 M.DOWN = 40 -- 下箭头 M.INSERT = 45 -- Insert键 M.DELETE = 46 -- Delete键 -- 小键盘键 M.NUMPAD0 = 96; M.NUMPAD1 = 97; M.NUMPAD2 = 98; M.NUMPAD3 = 99 M.NUMPAD4 = 100; M.NUMPAD5 = 101; M.NUMPAD6 = 102; M.NUMPAD7 = 103 M.NUMPAD8 = 104; M.NUMPAD9 = 105 M.NUMPAD_MULTIPLY = 106 -- * M.NUMPAD_ADD = 107 -- + M.NUMPAD_SUBTRACT = 109 -- - M.NUMPAD_DECIMAL = 110 -- . M.NUMPAD_DIVIDE = 111 -- / M.NUMLOCK = 144 -- 数字锁定 -- 符号键 M.SEMICOLON = 186 -- ; M.EQUAL = 187 -- = M.COMMA = 188 -- , M.MINUS = 189 -- - M.PERIOD = 190 -- . M.SLASH = 191 -- / M.BACKQUOTE = 192 -- ` M.LEFTBRACKET = 219 -- [ M.BACKSLASH = 220 -- \ M.RIGHTBRACKET = 221 -- ] M.QUOTE = 222 -- ' -- 特殊功能键 M.WIN = 91 -- Windows键 M.CONTEXT_MENU = 93 -- 右键菜单键 M.PRINTSCREEN = 44 -- 打印屏幕 M.SCROLLLOCK = 145 -- 滚动锁定 -- 媒体键 M.VOLUME_MUTE = 173 -- 静音 M.VOLUME_DOWN = 174 -- 音量减 M.VOLUME_UP = 175 -- 音量加 M.MEDIA_NEXT = 176 -- 下一曲 M.MEDIA_PREV = 177 -- 上一曲 M.MEDIA_STOP = 178 -- 停止 M.MEDIA_PLAY_PAUSE = 179 -- 播放/暂停
最新发布
11-08
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值