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 ...
随机推荐
- UVa 12525 Boxes and Stones (dp 博弈)
Boxes and Stones Paul and Carole like to play a game with S stones and B boxes numbered from 1 to B. ...
- PHP图像操作类
基于已给出的各种图像操作方法,这里我总结出了PHP图像操作的一个类,包含给图像加入文字水印.图像水印和压缩图片. 读者可自行加入功能. <? php class Image { private ...
- Palindrome Linked List 234
推断是否为回文链栈 时间复杂度为O(n) 空间复杂度为O(1) : 运用递归 保证空间复杂度为O(1): 时间复杂度为O(n): 注意定义了一个全局变量 flag = true 用此标记来标记是否在推 ...
- spring注入对象类型的属性
一.1.创建service类和Dao类 (1)在service中得到dao对象 2.具体实现过程 (1)在service里边把dao作为类型属性 (2)生成dao类型属性的set方法 public c ...
- mysql视图,索引和存储过程
一:视图 视图又叫虚表.同真实的表一样,视图包含一系列带有名称的列和行数据.但是,视图并不在数据库中以存储的数据值集形式存在.行和列数据来自由定义视图的查询所引用的表,并且在引用视图时动态生成. 具体 ...
- 使用数组实现ArrayList的效果
package day04.d2.shuzu; /** * 通过数组实现类似于集合的功能 * 包含功能有: * * 动态添加元素 * 在指定位置添加元素 * * 删除指定下标的元素 * 删除指定内容的 ...
- 如何正确产看API
看API时,先看的它的父接口自接口,及其相关的抽象类和子类 看完后,看概述的第一段话就行,后面的不用看. 再看构造方法,并到底层去看构造方法里参数的具体含义. 最后,再将包含的方法一个个进行测试. 解 ...
- 文档控件NTKO OFFICE 详细使用说明之预览word编辑保存回服务器
1.在线预览Word文件 (1) 运行环境 ① 浏览器:支持IE7-IE11(平台版本还支持Chrome和Firefox) ② IE工具栏-Internet 选项:将www.ntko.com加入到浏览 ...
- 移动端rem设置(部分安卓机型不兼容)
(function(win) { var doc = win.document; var docEl = doc.documentElement; var tid; function refreshR ...
- 利用js实现进入页面首先执行刷新操作,且只刷新一次
让页面进行刷新,可以使用location.reload()方法,但是这种方法会让页面一直不断的刷新,这是因为当页面加载完成以后,我们让它刷新一次,那么浏览器就会重新向服务器请求数据, 界面会重新加载, ...