Codeforces Round #459 (Div. 2) C. The Monster

题目链接
C. The Monster
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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

Note

For the first sample testcase, the pretty substrings of s are:

"(?" which can be transformed to "()".
"?)" which can be transformed to "()".
"((?)" which can be transformed to "(())".
"(?))" which can be transformed to "(())". 

For the second sample testcase, the pretty substrings of s are:

"??" which can be transformed to "()".
"()".
"??()" which can be transformed to "()()".
"?()?" which can be transformed to "(())".
"??" which can be transformed to "()".
"()??" which can be transformed to "()()".
"??()??" which can be transformed to "()()()". 

大意:给一个只包含左括号,右括号和可变为任意括号的问号的字符串,找正确括号串的个数,其形式为:1.空串 2.(正确括号串)即正确括号串外面加一层括号3.两个正确括号串的链接,注意的是记录的是区间数,而不是组合数

解析:进行固定左端点的枚举,用sum当前缀和,如果出现左括号和问号时,都把sum++(暂且把问号当成左括号),出现右括号时sum–,如果sum小于0,是可以直接跳出循环,因为前面不可能满足条件了。用cnt记录问号的个数,如果cnt*2大于等于sum,说明多出来的(的一半都可以被抵消掉,就满足条件

#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define inf 0x3f3f3f3f
#define rep(i,a,b) for(int i=a;i<b;i++)
#define rep1(i,a,b) for(int i=a;i>=b;i--)
using namespace std;
const int N=1e2+100;
char arr[N][N];
int main()
{
    ios::sync_with_stdio(false);
    string s;
    cin>>s;
    int len=s.size();
    int sum=0,cnt=0,ans=0;
    rep(i,0,len)
    {
        cnt=0,sum=0;
        rep(j,i,len)
        {
            if(s[j]=='(')
                sum++;//左括号
            else if(s[j]==')')
            {
                sum--;
                if(sum<0)//如果)多了,必然不满足条件
                    break;
            }
            else
            {
                sum++,cnt++;//cnt记录问号的个数
            }
            cnt=min(cnt,sum/2);//这一步到下一步之后可用的问号数最多为sum/2
                                //大于sum/2的问号是已经被固定了的
                                //如果后面的枚举再让这些问号变化前面就不再满足条件
            if(sum%2==0&&cnt*2>=sum)//可变性
                ans++;
        }
    }
    cout<<ans<<endl;
    return 0;
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值