【codechef】括号匹配,求子串最大值(灵活题)

 Input
3
4
()()
-1 -2 3 4
4
(()]
-1 -2 3 4
4
[{]{
1 2 3 4

Sample Output
7
1
0

Explanation

For first test case take last 2 elements: 3 + 4 = 7. 
For second test case take the middle 2 elements: -2 + 3 = 1

http://www.codechef.com/problems/SUBARRAY/

#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 long
using 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值