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 +  ;
#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 = ;
if( l != qr - ) ans = ( ans + ( dfs( l + , qr - ) * ) % mod ) % mod ;
else if( l == qr - ) ans = ( ans + ) % mod ;
if( qr + <= r ) ans = ( ans + ( dfs( qr + , r ) % mod ) ) % mod ;
return ans ;
} int main() {
scanf( "%d" , &n ) ;
for( int i = ; i <= n ; i ++ ) {
int x ;
scanf( "%d" , &x ) ;
if( !x ) st[ ++ top ] = i ;
else a[ st[ top -- ] ] = i ;
}
printf( "%lld\n" , dfs( , n ) ) ;
return ;
}

BZOJ3300: [USACO2011 Feb]Best Parenthesis 模拟的更多相关文章

  1. BZOJ3300: [USACO2011 Feb]Best Parenthesis

    3300: [USACO2011 Feb]Best Parenthesis Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 89  Solved: 42 ...

  2. B3300 [USACO2011 Feb]Best Parenthesis 模拟

    这是我今天遇到最奇怪的问题,希望有人帮我解释一下... 一开始我能得90分: #include<iostream> #include<cstdio> #include<c ...

  3. 【BZOJ】3300: [USACO2011 Feb]Best Parenthesis(模拟)

    http://www.lydsy.com/JudgeOnline/problem.php?id=3300 这个细节太多QAQ 只要将所有的括号'('匹配到下一个')'然后dfs即可 简单吧,,, #i ...

  4. [USACO2011 Feb]Best Parenthesis

    Time Limit: 10 Sec Memory Limit: 128 MB Description Recently, the cows have been competing with stri ...

  5. 3301: [USACO2011 Feb] Cow Line

    3301: [USACO2011 Feb] Cow Line Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 82  Solved: 49[Submit ...

  6. 【BZOJ】【3301】【USACO2011 Feb】Cow Line

    康托展开 裸的康托展开&逆康托展开 康托展开就是一种特殊的hash,且是可逆的…… 康托展开计算的是有多少种排列的字典序比这个小,所以编号应该+1:逆运算同理(-1). 序列->序号:( ...

  7. BZOJ2274: [Usaco2011 Feb]Generic Cow Protests

    2274: [Usaco2011 Feb]Generic Cow Protests Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 196  Solve ...

  8. BZOJ3301: [USACO2011 Feb] Cow Line

    3301: [USACO2011 Feb] Cow Line Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 67  Solved: 39[Submit ...

  9. 2272: [Usaco2011 Feb]Cowlphabet 奶牛文字

    2272: [Usaco2011 Feb]Cowlphabet 奶牛文字 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 138  Solved: 97 ...

随机推荐

  1. (四)Web应用开发---系统架构图

    系统宏观架构:EASYUI+MVC 系统架构图一. 系统架构图二.

  2. Chart控件的使用实例

    ChartTest.aspx: <%@ Page Language="C#" AutoEventWireup="true" CodeBehind=&quo ...

  3. Lodash 中文文档 (v4.16.1) 手机版

    http://lodash.swift.ren/ 手机扫描二维码直接进入

  4. 【剑指offer】调整数组顺序使奇数位于偶数前面

    一.题目: 输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有的奇数位于数组的前半部分,所有的偶数位于位于数组的后半部分,并保证奇数和奇数,偶数和偶数之间的相对位置不变. 二.思路: 用 ...

  5. IP追踪

    cmd里输入:tracert www.baidu.com 上图箭头方框中就是对应公司的总网IP

  6. [LeetCode] 258. Add Digits_Easy tag: Math

    Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. ...

  7. [LeetCode] 198. House Robber _Easy tag: Dynamic Programming

    You are a professional robber planning to rob houses along a street. Each house has a certain amount ...

  8. http协议基础(七)通用首部字段

    通用首部字段的意思,就是:请求和响应报文双方都会使用的首部 1.Cache-Control 通过指定它的指令,能操作缓存的工作机制 指令参数是可选的,多个指令通过“,”分隔 Cache-Control ...

  9. Q_UNUSED

    Q_UNUSED() 没有实质性的作用,用来避免编译器警告 void func( int a) { Q_UNUSED(a); //函数体内没有使用a,避免编译器警告 }

  10. js数组之有已有数组创建新的数组

    concat()和splice()方法允许通过已经有的数组创建新的数组 concat()这个方法可以合并多个数组创建一个数组 splice()这个方法是获得截取一个数组中的子集创建一个新的数组. 理论 ...