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)
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
#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的更多相关文章
- 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 ...
- 2016 Multi-University Training Contest 10 || hdu 5860 Death Sequence(递推+单线约瑟夫问题)
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=5860 题目大意:给你n个人排成一列编号,每次杀第一个人第i×k+1个人一直杀到没的杀.然后 ...
- 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 ...
- 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: ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- android监听虚拟按键的显示与隐藏【转】
本文转载自:http://blog.csdn.net/u014583590/article/details/55263141 虚拟按键在华为手机中大量存在,而虚拟按键的存在无疑增加了屏幕适配的难度,往 ...
- [jqpolt] formatString 日期格式化列表
// 年 %Y 2008 %y 08 // 月 %m 09 %#m 9 %B September %b Sep // 日 %d 05 %#d 5 %e 5 %A ...
- html基础知识整理
html 标题 <h1>这是一级标题</h1> <h2>这是二级标题</h2> <h3>这是三级标题</h3> html注释: ...
- json属性(Jackson)
Jackson相关:使用Jackson相关的注解时一定要注意自己定义的属性命名是否规范. 命名不规范时会失去效果.(例如Ename ,Eage 为不规范命名.“nameE”,“ageE”为规范命名). ...
- 关于KO信息
最近写大论文查到KO也是可以用于分类的一种信息. 如何使用KEGG进行通路富集http://blog.sciencenet.cn/blog-364884-779116.html kegg 数据库学习笔 ...
- vuex的状态管理模式
1.store.js Vuex 通过 store 选项,提供了一种机制将状态从根组件“注入”到每一个子组件中(需调用 Vue.use(Vuex)) state:存放数据. mutations:提交状态 ...
- XML DTD跟SCHEMA约束 语法了解
dtd语法 元素: <!Element 元素名称 数据类型|包含内容> 数据类型: #PCDATA:普通文本 使用的时候一般用()引起来 包含内容: 该元素下可以出现哪些元素, 用()引起 ...
- knockout.js(js)代码在IE中出现“意外地调用了方法或属性”的错误
var CartListViewModel = function () { var self = this; self.payment = [ { name: "", value: ...
- 使用postman模拟登录请求
Cookie 可以写在Headers里面
- Angular ocLazyLoad 与ui-router的配合使用
1.resolve state(配置路由时)的resolve参数: resolve:object,将会被注入controller去执行的函数,<string,function>形式. 基于 ...