a[i] 表示以i字符开头的合法序列有多少个

b[i] 表示以i字符结尾的合法序列有多少个

up表示上一层的'('的相应位置

mt[i] i匹配的相应位置

c[i] 包括i字符的合法序列个数  c[i]=c[up[i]]+a[i]*b[mt[i]]

括号序列不一定是合法的....

Easy Sequence

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)

Total Submission(s): 557    Accepted Submission(s): 165

Problem Description
soda has a string containing only two characters -- '(' and ')'. For every character in the string, soda wants to know the number of valid substrings which contain that character.



Note: 

An empty string is valid. If S is
valid, (S) is
valid. If U,V are
valid, UV is
valid.
 
Input
There are multiple test cases. The first line of input contains an integer T,
indicating the number of test cases. For each test case:



A string s consisting
of '(' or ')' (1≤|s|≤106).
 
Output
For each test case, output an integer m=∑i=1|s|(i⋅ansi mod 1000000007),
where ansi is
the number of valid substrings which contain i-th
character.
 
Sample Input
2
()()
((()))
 
Sample Output
20
42
Hint
For the second case, ans={1,2,3,3,2,1}, then m=1⋅1+2⋅2+3⋅3+4⋅3+5⋅2+6⋅1=42
 
Source
 

/* ***********************************************
Author :CKboss
Created Time :2015年08月10日 星期一 14时24分51秒
File Name :HDOJ5357_2.cpp
************************************************ */ #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <queue>
#include <set>
#include <map> using namespace std; typedef long long int LL; const int maxn=1001000;
const LL mod=1e9+7; int n;
LL a[maxn],b[maxn],mt[maxn];
LL c[maxn];
int up[maxn];
int stk[maxn],top;
char str[maxn]; void init(int n)
{
top=0;
memset(mt,0,sizeof(mt[0])*n);
memset(a,0,sizeof(a[0])*n);
memset(b,0,sizeof(b[0])*n);
memset(c,0,sizeof(c[0])*n);
memset(up,0,sizeof(up[0])*n);
} int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout); int T_T;
scanf("%d",&T_T);
while(T_T--)
{
scanf("%s",str+1); n=strlen(str+1);
init(n+10); for(int i=1;i<=n;i++)
{
if(str[i]=='(')
{
up[i]=stk[top];
stk[++top]=i;
}
else if(top)
{
int u=stk[top--];
mt[u]=i; mt[i]=u;
b[i]=b[mt[i]-1]+1;
}
} while(top) mt[stk[top--]]=0; for(int i=n;i>=1;i--)
{
if(str[i]=='('&&mt[i])
{
a[i]=a[mt[i]+1]+1;
}
} LL ans=0; c[0]=0;
for(int i=1;i<=n;i++)
{
if(str[i]=='('&&mt[i])
{
c[mt[i]]=c[i]=c[up[i]]+(LL)a[i]*b[mt[i]];
}
ans+=c[i]*i%mod;
} cout<<ans<<endl;
} return 0;
}

HDOJ 5357 Easy Sequence DP的更多相关文章

  1. 2015 Multi-University Training Contest 6 hdu 5357 Easy Sequence

    Easy Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)T ...

  2. HDOJ(HDU).2844 Coins (DP 多重背包+二进制优化)

    HDOJ(HDU).2844 Coins (DP 多重背包+二进制优化) 题意分析 先把每种硬币按照二进制拆分好,然后做01背包即可.需要注意的是本题只需要求解可以凑出几种金钱的价格,而不需要输出种数 ...

  3. HDOJ(HDU).1059 Dividing(DP 多重背包+二进制优化)

    HDOJ(HDU).1059 Dividing(DP 多重背包+二进制优化) 题意分析 给出一系列的石头的数量,然后问石头能否被平分成为价值相等的2份.首先可以确定的是如果石头的价值总和为奇数的话,那 ...

  4. HDOJ(HDU).2159 FATE (DP 带个数限制的完全背包)

    HDOJ(HDU).2159 FATE (DP 带个数限制的完全背包) 题意分析 与普通的完全背包大同小异,区别就在于多了一个个数限制,那么在普通的完全背包的基础上,增加一维,表示个数.同时for循环 ...

  5. HDOJ(HDU).1114 Piggy-Bank (DP 完全背包)

    HDOJ(HDU).1114 Piggy-Bank (DP 完全背包) 题意分析 裸的完全背包 代码总览 #include <iostream> #include <cstdio&g ...

  6. HDOJ 1501 Zipper 【DP】【DFS+剪枝】

    HDOJ 1501 Zipper [DP][DFS+剪枝] Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Ja ...

  7. HDU 4359——Easy Tree DP?——————【dp+组合计数】

    Easy Tree DP? Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)To ...

  8. Rabin_Karp(hash) HDOJ 1711 Number Sequence

    题目传送门 /* Rabin_Karp:虽说用KMP更好,但是RK算法好理解.简单说一下RK算法的原理:首先把模式串的哈希值算出来, 在文本串里不断更新模式串的长度的哈希值,若相等,则找到了,否则整个 ...

  9. HDU 4359 Easy Tree DP?

    Easy Tree DP? Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)To ...

随机推荐

  1. Vuex-一个专为 Vue.js 应用程序开发的状态管理模式

    为什么会出现Vuex 非父子关系的组件如何进行通信?(Event Bus)bus.js import Vue from 'vue'; export default new Vue(); foo.vue ...

  2. 雅礼集训1-9day爆零记

    雅礼集训1-9day爆零记 先膜一下虐爆我的JEFF巨佬 Day0 我也不知道我要去干嘛,就不想搞文化科 (文化太辣鸡了.jpg) 听李总说可以去看(羡慕)各路大佬谈笑风声,我就报一个名吧,没想到还真 ...

  3. 如何在Google Play上通过电脑下载apk

    操作步骤: 1.首先打开翻 墙软件. 2.键入网址:http://apps.evozi.com/apk-downloader/ 3.将Google Play里apk的网址,复制到“Package na ...

  4. CMSIS-RTOS 时间管理之虚拟定时器Virtual Timers

    虚拟定时器Virtual Timers CMSIS-RTOS API里有几个向下计数的虚拟定时器,它们实现计数完成时用户的回调功能.每个定时器都可以配置成单次计数或重复计数模式,它们可以在定义定时器结 ...

  5. vs2010和qt4.8.4配置

    最近项目要求在vs中开发qt程序,安装过后发现代码每天提示功能.由于本人记忆力有限,特在网上收罗了些配置方法. vs安装目录采用默认,qt安装目录:C:\Qt\4.8.4vs 在系统环境变量新建QTD ...

  6. 洛谷 P3467 [POI2008]PLA-Postering

    P3467 [POI2008]PLA-Postering 题目描述 All the buildings in the east district of Byteburg were built in a ...

  7. linux添加开机启动项的方法介绍

    使用chkconfig命令可以查看在不同启动级别下课自动启动的服务(或是程序),命令格式如下:chkconfig --list可能输出如下:openvpn 0:关闭 1:开启 ...... 6:关闭 ...

  8. Gym - 100625F Count Ways 快速幂+容斥原理

    题意:n*m的格子,中间有若干点不能走,问从左上角到右下角有多少种走法. 思路:CountWay(i,j) 表示从 i 点到 j 点的种数.然后用容斥原理加加减减解决 #pragma comment( ...

  9. 网络地图WebMap介绍

    WebMap是从ArcGIS Online或者ArcGIS for Portal item上获取显示到用户的界面中. 需要的是地图的ID. 创建一个新的网络地图需要设置ID号,然后再用地图底图MapV ...

  10. Yeslab华为安全HCIE七门之-防火墙UTM技术(5篇)

    课程目录:     华为安全HCIE-第四门-防火墙UTM技术(5篇)\1_内容安全_内容过滤.avi 华为安全HCIE-第四门-防火墙UTM技术(5篇)\2_内容安全-url过滤.avi 华为安全H ...