zoukankan      html  css  js  c++  java
  • Codeforces 990C

    题意略。

    思路:

    本题实质上就是在问一共有多少种合理的匹配,使得括号匹配合法。我给每种情况编号,用一个cnt来记录,( 则cnt += 1;) 出现则cnt -= 1。

    最后只要把相反数的个数相乘后相加就可以得到最后的结果。但是要注意,像  )(  这样的串是没有意义的,

    因为不可能有别的串来匹配它来使这两个串合法。当然,没有意义的串有可能不会这么显式,也有可能出现   ()))(  这种。

    我们在检查这个串的时候,记录它cnt的最小值,如果后来这个cnt变大了,就说明这个串已经变得没有意义了。

    详见代码:

    #include<bits/stdc++.h>
    #define maxn 300005
    using namespace std;
    typedef long long LL;
    
    LL cnt[maxn * 2];
    char str[maxn];
    
    int main(){
        int n;
        scanf("%d",&n);
        for(int i = 0;i < n;++i){
            scanf("%s",str);
            int len = strlen(str);
            int lowest = 0;
            int temp = 0;
            for(int j = 0;j < len;++j){
                if(str[j] == '(') temp += 1;
                else temp -= 1;
                lowest = min(lowest,temp);
            }
            if(lowest < 0 && lowest < temp) continue;
            cnt[temp + maxn] += 1;
        }
        LL sum = 0;
        for(int i = 0;i <= maxn;++i){
            if(cnt[i] == 0) continue;
            int cur = i - maxn;
            int nxt = -1 * cur + maxn;
            sum += cnt[i] * cnt[nxt];
        }
        printf("%lld
    ",sum);
        return 0;
    }
  • 相关阅读:
    linux系统中不同颜色的文件夹及根目录介绍
    linux命令学习 随笔
    Happiness
    Sequence Number
    base64加密解密c++代码
    Wooden Sticks
    出租车费
    关系推断
    如何在Ubuntu中安装中文输入法
    c语言中printf("%x",-1);为什么会输出-1的十六进制补码??
  • 原文地址:https://www.cnblogs.com/tiberius/p/9301855.html
Copyright © 2011-2022 走看看