博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
2015 Multi-University Training Contest 6 hdu 5357 Easy Sequence
阅读量:4881 次
发布时间:2019-06-11

本文共 2427 字,大约阅读时间需要 8 分钟。

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
 1005
 
解题:栈
 
貌似还是有点不明白,代码先放着
 
1 #include 
2 using namespace std; 3 typedef long long LL; 4 const LL mod = 1000000007; 5 const int maxn = 2000010; 6 char str[maxn]; 7 int stk[maxn],match[maxn],pre[maxn],a[maxn],b[maxn],top; 8 LL ans[maxn]; 9 int main() {10 int kase,n;11 scanf("%d",&kase);12 while(kase--) {13 scanf("%s",str + 1);14 top = 0;15 n = strlen(str + 1);16 for(int i = 1; i <= n; ++i) {17 match[i] = pre[i] = 0;18 if(str[i] == '(') stk[++top] = i;19 else if(top) {20 match[stk[top]] = i;21 match[i] = stk[top];22 if(top > 1) pre[match[i]] = stk[top-1];23 stk[top--] = 0;24 }25 }26 ans[0] = a[0] = b[n+1] = 0;27 for(int i = 1; i <= n; i++)28 a[i] = (str[i] == ')' && match[i])?(a[match[i] - 1] + 1):0;29 for(int i = n; i >= 1; i--)30 b[i] = (str[i] == '(' && match[i])?(b[i] = b[match[i] + 1] + 1):0;31 for(int i = 1; i <= n; i++)32 ans[i] = (str[i] == '(')?(ans[pre[i]] + ((LL)b[i]*a[match[i]] % mod) % mod):ans[match[i]];33 LL ret = 0;34 for(int i = 1; i <= n; ++i)35 ret += ans[i]*i%mod;36 printf("%I64d\n",ret);37 }38 return 0;39 }
View Code

 

转载于:https://www.cnblogs.com/crackpotisback/p/4742410.html

你可能感兴趣的文章
正则表达式之字符串验证
查看>>
codeblocks如何支持_tmain?可移植代码的编码推荐
查看>>
省市联动 填坑
查看>>
canvas写的一个小时钟demo
查看>>
原来今天是冬至
查看>>
又混了一天班
查看>>
九度oj 1006
查看>>
HDU6400-2018ACM暑假多校联合训练1004-Parentheses Matrix-构造
查看>>
最短路问题专题
查看>>
《Redis复制与可扩展集群搭建》看后感
查看>>
Jquery Mobile总结
查看>>
223. Rectangle Area
查看>>
spring boot + velocity中文乱码解决方式
查看>>
读罢泪两行,人生成长必须面对的10个残酷事实
查看>>
ASP 32位程序运行与64位问题:ADODB.Connection 错误 '800a0ea9' 未指定提供程序,也没有指派的默认提供程序。...
查看>>
xcode-git笔记
查看>>
TCP和UDP的优缺点及区别
查看>>
MATLAB消除曲线毛刺Outlier Detection and Removal [hampel]
查看>>
MySQL DATE_SUB() 函数
查看>>
在SSH框架下按条件分页查询
查看>>