• BZOJ3300: [USACO2011 Feb]Best Parenthesis 模拟


    Description

    Recently, the cows have been competing with strings of balanced 
    parentheses and comparing them with each other to see who has the 
    best one. 
    Such strings are scored as follows (all strings are balanced): the 
    string "()" has score 1; if "A" has score s(A) then "(A)" has score 
    2*s(A); and if "A" and "B" have scores s(A) and s(B), respectively, 
    then "AB" has score s(A)+s(B). For example, s("(())()") = 
    s("(())")+s("()") = 2*s("()")+1 = 2*1+1 = 3. 
    Bessie wants to beat all of her fellow cows, so she needs to calculate 
    the score of some strings. Given a string of balanced parentheses 
    of length N (2 <= N <= 100,000), help Bessie compute its score. 
     
    计算“平衡字符串”的分数,“平衡字符串”是指由相同数量的‘(’和‘)’组成, 
    且以‘(’开头,以‘)’结尾的字符串。 
    计算规则: 
    字符串“()”的得分是1. 
    如果,平衡字符串“A”的得分是是S(A),那么字符串“(A)”得分是2*S(A) ; 
    如果,“A”,“B” 得分分别是S(A)和S(B),那么平衡字符串“AB”得分为S(A)+S(B) 
    例如:s("(())()") =s("(())")+s("()") = 2*s("()")+1 = 2*1+1 = 3.

    Input

    * Line 1: A single integer: N 
    * Lines 2..N + 1: Line i+1 will contain 1 integer: 0 if the ith 
    character of the string is '(', and 1 if the ith character of the string is ')' 
    第1行:N,平衡字符串长度 
    第2至N+1行:Linei+1 整数0或1,0代表字符‘(’,1代表‘)’

    Output

    * Line 1: The score of the string. Since this number can get quite large, output the score modulo 12345678910. 
    计算字符串得分,结果对12345678910取模

    Sample Input

    6
    0
    0
    1
    1
    0
    1
    INPUT DETAILS:
    This corresponds to the string "(())()".

    Sample Output

    3

    Solution

    挺简单的,就是不会写而已(雾

    细节很多

    预处理出每个左括号对应的右括号(这个用栈就可以处理了)

    然后$dfs$一遍求出答案,分类讨论一下就行

    #include <bits/stdc++.h>
    
    using namespace std ;
    
    const int N = 1e5 + 10 ;
    #define mod 12345678910 
    #define ll long long
    
    int n ;
    int a[ N ] , top  , st[ N ] ;
    
    ll dfs( int l , int r ) {
        int qr = a[ l ] ;
        ll ans = 0 ;
        if( l != qr - 1 ) ans = ( ans + ( dfs( l + 1 , qr - 1 ) * 2 ) % mod ) % mod ;
        else if( l == qr - 1 ) ans = ( ans + 1 ) % mod ;
        if( qr + 1 <= r ) ans = ( ans + ( dfs( qr + 1 , r ) % mod ) ) % mod ;
        return ans ;
    }
    
    int main() {
        scanf( "%d" , &n ) ;
        for( int i = 1 ; i <= n ; i ++ ) {
            int x ;
            scanf( "%d" , &x ) ;
            if( !x ) st[ ++ top ] = i ;
            else a[ st[ top -- ] ] = i ;
        }
        printf( "%lld
    " , dfs( 1 , n ) ) ;
        return 0 ;
    }
  • 相关阅读:
    dubbo开发中使用到的一些服务配置方式
    jedis连接池详解(Redis)
    《Java线程池》:任务拒绝策略
    BlockingQueue(阻塞队列)详解
    洛谷 P3275 BZOJ 2330 [SCOI2011]糖果
    LaTeX的简单使用方法
    洛谷 P1131 BZOJ 1060 [ZJOI2007]时态同步
    洛谷 P2587 BZOJ 1034 [ZJOI2008]泡泡堂
    苦酒入喉心作痛,红酒入鹅鹅想哭——震惊!勒索病毒想哭靠wine感染了Ubuntu16.04
    洛谷 P2144 BZOJ 1003 [FJOI2007]轮状病毒
  • 原文地址:https://www.cnblogs.com/henry-1202/p/BZOJ3300.html
Copyright © 2020-2023  润新知