Easy Sequence

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 766    Accepted Submission(s): 208

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 \leq |s| \leq 10^6)$.

Output
For each test case, output an integer $m=\sum_{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 \cdot 1 + 2 \cdot 2 + 3 \cdot 3 + 4 \cdot 3 + 5 \cdot 2 + 6 \cdot 1 = 42$

Author
zimpha@zju

Source
 
解题:栈
 
貌似还是有点不明白,代码先放着
 
 #include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL mod = ;
const int maxn = ;
char str[maxn];
int stk[maxn],match[maxn],pre[maxn],a[maxn],b[maxn],top;
LL ans[maxn];
int main() {
int kase,n;
scanf("%d",&kase);
while(kase--) {
scanf("%s",str + );
top = ;
n = strlen(str + );
for(int i = ; i <= n; ++i) {
match[i] = pre[i] = ;
if(str[i] == '(') stk[++top] = i;
else if(top) {
match[stk[top]] = i;
match[i] = stk[top];
if(top > ) pre[match[i]] = stk[top-];
stk[top--] = ;
}
}
ans[] = a[] = b[n+] = ;
for(int i = ; i <= n; i++)
a[i] = (str[i] == ')' && match[i])?(a[match[i] - ] + ):;
for(int i = n; i >= ; i--)
b[i] = (str[i] == '(' && match[i])?(b[i] = b[match[i] + ] + ):;
for(int i = ; i <= n; i++)
ans[i] = (str[i] == '(')?(ans[pre[i]] + ((LL)b[i]*a[match[i]] % mod) % mod):ans[match[i]];
LL ret = ;
for(int i = ; i <= n; ++i)
ret += ans[i]*i%mod;
printf("%I64d\n",ret);
}
return ;
}

2015 Multi-University Training Contest 6 hdu 5357 Easy Sequence的更多相关文章

  1. 2015 Multi-University Training Contest 2 hdu 5306 Gorgeous Sequence

    Gorgeous Sequence Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Othe ...

  2. 2016 Multi-University Training Contest 10 || hdu 5860 Death Sequence(递推+单线约瑟夫问题)

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=5860 题目大意:给你n个人排成一列编号,每次杀第一个人第i×k+1个人一直杀到没的杀.然后 ...

  3. 2015 Multi-University Training Contest 8 hdu 5390 tree

    tree Time Limit: 8000ms Memory Limit: 262144KB This problem will be judged on HDU. Original ID: 5390 ...

  4. 2015 Multi-University Training Contest 8 hdu 5383 Yu-Gi-Oh!

    Yu-Gi-Oh! Time Limit: 2000ms Memory Limit: 65536KB This problem will be judged on HDU. Original ID:  ...

  5. 2015 Multi-University Training Contest 8 hdu 5385 The path

    The path Time Limit: 2000ms Memory Limit: 65536KB This problem will be judged on HDU. Original ID: 5 ...

  6. 2015 Multi-University Training Contest 3 hdu 5324 Boring Class

    Boring Class Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tota ...

  7. 2015 Multi-University Training Contest 3 hdu 5317 RGCDQ

    RGCDQ Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submi ...

  8. 2015 Multi-University Training Contest 10 hdu 5406 CRB and Apple

    CRB and Apple Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)To ...

  9. 2015 Multi-University Training Contest 10 hdu 5412 CRB and Queries

    CRB and Queries Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Other ...

随机推荐

  1. 学习Mockito - Mockito对Annotation的支持

    学习Mockito - Mockito对Annotation的支持 博客分类: test junit工作  Mockito支持对变量进行注解,例如将mock对象设为测试类的属性,然后通过注解的方式@M ...

  2. 热修复JSPatch之实战教程

      接上篇<热修复JSPatch之接口设计>,在这篇文章主要给大家讲述一下怎样高速具备热修复能力,当然了假设有人有志于把JSPatch系统的学习,甚至用JSPatch进行开发的.就没有必要 ...

  3. Java内存管理及垃圾回收总结

    概述 Java和C++的一个很重要的差别在于对内存的管理.Java的自己主动内存管理及垃圾回收技术使得Java程序猿不须要释放废弃对象的内存.从而简化了编程的过程.同一时候也避免了因程序猿的疏漏而导致 ...

  4. eclipse中打字中文突然变成繁体

    eclipse中打字中文突然变成繁体 在用eclipse做android项目的时候,发现打出来的字全部是繁体,而且QQ等其他位置又是简体. 原因:eclipse的快捷点ctrl+alt+f(forma ...

  5. [xPlugins] jQuery Contextmenu右键菜单

    [2012-04-12] Contextmenu 右键菜单 v0.1 版本发布 [功能] 在特定区域弹出右键菜单 [功能] 可以在弹出右键菜单区域内,再屏蔽某个小区域. [功能] 有两种方式添加右键菜 ...

  6. [Javascript] 5个最佳的Javascript日期处理类库

    在大家日常网站开发和web应用开发中,我们往往需要有效的调用Javascript处理日期和时间格式相关的函数,在Javascript中已经包含了部分最基本的内建处理方法. 在大家日常网站开发和web应 ...

  7. Django之缓存机制

    1.1 缓存介绍 1.缓存的简介 在动态网站中,用户所有的请求,服务器都会去数据库中进行相应的增,删,查,改,渲染模板,执行业务逻辑,最后生成用户看到的页面. 当一个网站的用户访问量很大的时候,每一次 ...

  8. BZOJ 3991 set维护dfs序

    思路: set按照dfn排序 两点之间的距离可以O(logn)算出来 加一个点-> now ans+=dis(pre,now)+dis(now,next)-dis(pre-next); 删一个点 ...

  9. 大数字运算——1、BigInteger

    package com.wh.BigInteger; import java.math.BigInteger; import java.util.Arrays; /** * @author 王恒 * ...

  10. 开发辅助 | 阿里图标库iconfont入门使用

    目前大多数的互联网公司,前端开发和UI设计师配合中,针对设计师给图的效果图,前端开发工程师不再像往常一样对于细小图标进行切图,取而代之的是引用阿里图标库(http://iconfont.cn/):简单 ...