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 ...
随机推荐
- 利用runtime动态生成对象?
利用runtime我们能够动态生成对象.属性.方法这特性 假定我们要动态生成DYViewController,并为它创建属性propertyName 1)对象名 NSString *class = @ ...
- javascript的==和===,以及if(xxx)总结
转载请注明 本文出自:http://blog.csdn.net/nancle 首先说==和=== 首先说明一个非常特殊的值NaN, typeof(Nav)得到'number',可是NaN不等于不论什么 ...
- C++求解汉字字符串的最长公共子序列 动态规划
近期,我在网上看了一些动态规划求字符串最长公共子序列的代码.可是无一例外都是处理英文字符串,当处理汉字字符串时.常常会出现乱码或者不对的情况. 我对代码进行了改动.使用wchar_t类型存储字 ...
- 点击TButton后的执行OnClick和OnMouseDown两个事件的过程(其实是通过WM_COMMAND执行程序员的代码)
问题的来源:在李维的<深入浅出VCL>一书中提到了点击TButton会触发WM_COMMAND消息,正是它真正执行了程序员的代码.也许是我比较笨,没有理解他说的含义.但是后来经过追踪代码和 ...
- C C++每个头文件的功能说
C/C++每个头文件的功能说明 传统 C++ #include <assert.h> //设定插入点 #include <ctype.h> //字符处理 #include &l ...
- Windows 10彻底关闭自动更新
关键点:把流量计费开启.
- .net几种文件下载的方法
.Net文件下载方式.... 之前在写上传文件.下载文件的时候,发现Response对象里面有好几种下载文件的方式,之后自己亲自实践了这几种方法,记录下以便以后复习... WriteFile文件下载 ...
- laydate.js时间选择
例子: <asp:HiddenField ID="hfdDateBuid3" runat="server" /> <script type=& ...
- sublime3 install python3
链接地址:https://blog.csdn.net/Ti__iT/article/details/78830040
- Struts2框架学习(三)——配置详解
一.struts.xml配置 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts ...