【codechef】括号匹配,求子串最大值(灵活题)
来源:互联网 发布:tomcat 域名绑定 编辑:程序博客网 时间:2024/05/21 15:43
Input34()()-1 -2 3 44(()]-1 -2 3 44[{]{1 2 3 4Sample Output710
Explanation
For first test case take last 2 elements: 3 + 4 = 7.
For second test case take the middle 2 elements: -2 + 3 = 1
#include<iostream>#include<algorithm>#include<string>#include<map>//int dx[4]={0,0,-1,1};int dy[4]={-1,1,0,0};#include<set>//int gcd(int a,int b){return b?gcd(b,a%b):a;}#include<vector>#include<cmath>#include<stack>#include<string.h>#include<stdlib.h>#include<cstdio>#define mod 1e9+7#define ll long longusing namespace std;map<char,int> r;ll x[100010];ll sum[100010];char y[100010];int n;int main(){r[']']='[';r['}']='{';r['>']='<';r[')']='(';int t;scanf("%d",&t);while(t--){//memset(sum,0,sizeof(sum)); 会超时 scanf("%d", &n);getchar();for(int i=1;i<=n;++i) scanf("%c",&y[i]); for(int i=1;i<=n;++i){ scanf("%lld",&x[i]); sum[i]=0; x[i]+=x[i-1]; } stack<int> st; ll s=0; for(int i=1;i<=n;++i){ int u=0; if(st.size()>0){ if(r[y[i]]==y[st.top()]) u=1; } if(u==1){ sum[i]=max(0ll,sum[st.top()-1]+x[i]-x[st.top()-1]);//((())()) s=max(s,sum[i]); st.pop(); } else st.push(i); } printf("%lld\n", s);} return 0;}
0 0
- 【codechef】括号匹配,求子串最大值(灵活题)
- 【codechef】Gcd Queries(灵活题,分类)
- 【codechef】 Zeroes(大数灵活题)
- 【codechef】Arush Challenge(灵活题)
- 【codechef】Strings Classes(枚举灵活题)
- 【codechef】Set Difference (灵活题,超时)
- 【codechef】The Warehouse(灵活题)
- 【codechef】Chef and Strange Formula(找规律,灵活题)
- 【codechef】Chef and Polygons(灵活题,坑题)
- 括号匹配一道题
- 括号匹配(未完成)
- 括号匹配(二)
- 括号匹配(二)
- 括号匹配(二)
- 括号匹配(二)
- 括号匹配(二)
- 括号匹配(二)
- 括号匹配(二)
- MATLAB中Legend的一些控制方法
- 插入排序
- XCode 之快捷键
- 垃圾收集器和内存分配策略
- groovy日期格式的转换
- 【codechef】括号匹配,求子串最大值(灵活题)
- css定位
- apply()原理以及作用
- Java并发编程-30-阻塞式线程安全列表-LinkedBlockingDeque
- 当才华配不上梦想,那就静心反思和修炼吧!
- android如何将一个字符串生成二维码
- Socket剖析
- ACM 头文件与宏定义 (以后有好的会随时更新)
- Redis Sentinel机制与用法[转]