codeforces-918字符串'(' '('处理

 

题面:

As Will is stuck in the Upside Down, he can still communicate with his mom, Joyce, through the Christmas lights (he can turn them on and off with his mind). He can't directly tell his mom where he is, because the monster that took him to the Upside Down will know and relocate him.

Thus, he came up with a puzzle to tell his mom his coordinates. His coordinates are the answer to the following problem.

A string consisting only of parentheses ('(' and ')') is called a bracket sequence. Some bracket sequence are called correct bracket sequences. More formally:

  • Empty string is a correct bracket sequence.
  • if s is a correct bracket sequence, then (s) is also a correct bracket sequence.
  • if s and t are correct bracket sequences, then st (concatenation of s and t) is also a correct bracket sequence.

A string consisting of parentheses and question marks ('?') is called pretty if and only if there's a way to replace each question mark with either '(' or ')' such that the resulting string is a non-empty correct bracket sequence.

Will gave his mom a string s consisting of parentheses and question marks (using Morse code through the lights) and his coordinates are the number of pairs of integers (l, r) such that 1 ≤ l ≤ r ≤ |s| and the string slsl + 1... sr is pretty, where si is i-th character of s.

Joyce doesn't know anything about bracket sequences, so she asked for your help.

Input

The first and only line of input contains string s, consisting only of characters '(', ')' and '?' (2 ≤ |s| ≤ 5000).

Output

Print the answer to Will's puzzle in the first and only line of output.

Examples

Input

((?))

Output

4

Input

??()??

Output

7

    这个题特别好,是个锻炼思维超好的题,是一个很好的可以叫贪心的题目:

对于第一个样例:
((?))
1.(())->("?"->")") 
2.()->("?"->")")
3.()->("?"->"(") 
4.(())->("?"->"(")

这个题的贪心思路是使用L记录“(”的个数使用“R”记录对于L来说需要多少“)”这里有一个特别重要的点就是把“?”首先看形成“)”;

代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<queue>

using namespace std;
typedef long long ll;
const ll maxn=1e7+10;
string s;
int main()
{
	ios::sync_with_stdio(false);
	while(cin>>s){
		int len = s.length();
		int l,r;
		int ans = 0;
		for(int i=0;i<len;i++){
			l=0;r=0;
			for(int j=i;j<len;j++){
				if(s[j]=='(') l++,r++;
				else if(s[j] ==')')l--,r--;
				else l--,r++;
				if(r<0)break;
				if(l<0) l+=2;
				if(l==0)ans++;
			}
		}
		cout<<ans<<endl;
	}
 	return 0;
}

在网上还看到一个跟好理解的题解;使用另一个变量来记录“?”的个数就情况来定“?”是“(”还是“)”;

代码如下:

string s;
int main()
{
    ios::sync_with_stdio(false);
    cin>>s;
    int r,t;
	int ans=0;
	int len = s.length();
    for(int j=0;j<len;j++){
    	r=0;t=0;
    	for(int i=j;i<len;i++){
        	if(s[i]=='(')r++;
       		else if(s[i]==')')r--;
       	 	else r--,t++;
       	 	if(r==0)ans++;
        	else if(r<0&&t>0)r+=2,t--;
        	else if(r>0) continue;
        	else if(r<0&&!t)break;
 		}
    }
    cout<<ans<<endl;
    return 0;
}

好好弥补自己的不足吧!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值