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. 支持中文的基于词为基本粒度的前缀树(prefix trie)python实现

    Trie树,也叫字典树.前缀树.可用于"predictive text"和"autocompletion".亦可用于统计词频(边插入Trie树边更新或加入词频) ...

  2. 高效使用hive

    工作中常常使用hive.熟练使用hvie的配置參数能够更加高效的使用Hive Hive option: hive -f   script.hql : 从文件script.hql中的读取hql运行 hi ...

  3. 彻底弄懂px em rem

    国内的设计师大都喜欢用px,而国外的网站大都喜欢用em和rem,那么三者有什么区别,又各自有什么优劣呢? PX特点 1. IE无法调整那些使用px作为单位的字体大小: 2. 国外的大部分网站能够调整的 ...

  4. HUST 1585 排队

    2019-05-21 10:15:00 加油,加油 !!! #include <bits/stdc++.h> using namespace std; int main() { int n ...

  5. ViewData与ViewBag的使用区别

    在Asp.net MVC 3 web应用程序中,我们会用到ViewData与ViewBag,对比一下: ViewData ViewBag 它是Key/Value字典集合 它是dynamic类型对像 从 ...

  6. vue2.x阅读笔记

    v-once 作用: 只赋值一次 注意: 包括子项都是只赋值一次 模板 1.v-text v-html 2.{}语法 3.如果是html的属性,则用v-bind绑定,简写: 注意: 1.可以使用jav ...

  7. for循环和数组的应用

    <html> <head> <meta charset="utf-8"> <title>无标题文档</title> &l ...

  8. Surrogate data 代理数据

    附一篇science论文,待啃: 附Surrogate time series and fields,matlab:https://www.sogou.com/link?url=DSOYnZeCC_p ...

  9. JavaScript回顾一下js的基础知识,以及学习一下在项目中了解到的新知识

    学习文档:https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Guide/Functions https://www.cnblogs.com ...

  10. MongoDB 学习笔记(七):主从复制与副本集

    一.主从复制 1.主从复制是一个简单的数据库同步备份的集群技术,如下图:要明确的知道主服务器与从服务器,且从服务器要明确的知道主服务器的存在. 2.在MongoDB中在启动数据库服务时,可以用mast ...