zoukankan      html  css  js  c++  java
  • [USACO2011 Feb]Best Parenthesis

    Time Limit: 10 Sec Memory Limit: 128 MB

    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

    HINT

    Source

    Silver

    解:

    期望:100  实际:20

    暴力实现有问题。读题不认真,没有取模。太菜了。

    我的想法:从小括号开始扫,每次将对应包含的一个括号集团消除掉

    不知道哪里写挂了。

    为什么我总是这么弱呢?

    正解:分治。

    预处理出每个左括号所对应的右括号的所在位置。

    然后对于整个问题,分治,用搜索得到部分解,然后更新答案。

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<queue>
     4 #define ll long long
     5 using namespace std;
     6 const ll mod=12345678910ll;
     7 int n,s[200000],top,pos[200000];
     8 ll dfs(int l,int r)
     9 {
    10     ll nw=0,p=pos[l];
    11     if(l+1==p) nw=(nw+1)%mod;
    12     else nw=(nw+2*dfs(l+1,p-1)%mod)%mod;
    13     if(p+1<=r) nw=(nw+dfs(p+1,r)%mod)%mod;
    14     return nw;
    15 }
    16 int main()
    17 {
    18     scanf("%d",&n);
    19     for(int i=1,x;i<=n;++i)
    20     {
    21         scanf("%d",&x);
    22         if(x==0) s[++top]=i;
    23         else if(top) pos[s[top--]]=i;
    24     }
    25     printf("%lld",dfs(1,n));
    26     return 0;
    27 }
    View Code
  • 相关阅读:
    [原创]Office Word 2010如何使用printer drivers输出PostScript文件
    MATLAB启动时报错: pathdef.m not found 问题解决方法
    Ubuntu 下 Matlab R2010a 错误:`GLIBCXX_3.4.11' not found 的解决办法
    [转] 一阶导和二阶导的含义
    win7 搭建ftp 设置用户权限 远程访问设置
    Ubuntu 中软件的安装、卸载以及查看的方法总结
    64位ubuntu12.04 LTS安装oracle10g笔记
    如何在Ubuntu 12.04 LTS中使用低版本gcc/g++
    HTTP与HttpServlet
    EXP00091错误
  • 原文地址:https://www.cnblogs.com/adelalove/p/8569683.html
Copyright © 2011-2022 走看看