cf 917A 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:

  1. "(?" which can be transformed to "()".
  2. "?)" which can be transformed to "()".
  3. "((?)" which can be transformed to "(())".
  4. "(?))" which can be transformed to "(())".

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

  1. "??" which can be transformed to "()".
  2. "()".
  3. "??()" which can be transformed to "()()".
  4. "?()?" which can be transformed to "(())".
  5. "??" which can be transformed to "()".
  6. "()??" which can be transformed to "()()".
  7. "??()??" which can be transformed to "()()()".


二 分析

给定一个由'(', '?', ')'三种字符组成的字符串s,长度为n(2<=n<=5000),问有多少个合法的子串。一个字符串合法是说,把其中的'?'替换成'('或')',能够使得每一个左括号都找到唯一与之对应的右括号。


第一反应是可以动态规划判断一个子串[l, r]是否合法,合法的字符串满足以下两个条件之一:

1)s[l],s[r]组成一对括号,并且子串[l + 1, r - 1]合法;

2)存在某个l < k < r,使得子串[l, k]合法,子串[k + 1, r]也合法。

但条件2)的判断是O(n^3)的,输入串长度5000的话并不能过。


事实上,一个字符串s(长度为n)合法等价于如下三个条件:

1)2 | n;

2)对于所有1 <= i <= n:字串[1, i]中所有'('的数量 + 所有'?'的数量 >= 所有')'的数量;

3)对于所有1 <= i <= n:字串[i, n]中所有')'的数量 + 所有'?'的数量 >= 所有'('的数量; 


必要性显然,充分性的话如果没有'?'的话等于说只要把一个串任意分成左右两半,左边的'('数量不少于')'数量,右边的')'数量不少于'('数量,整个串里他们数量相等。那么这个串就是合法的(可以想象一个栈,左括号就把一个物品压栈,右括号就出栈,这样的条件保证任何时候不会让空栈做出栈操作,并且所有操作完毕后栈为空)。


三 代码

/*
PROB: cf 917A
LANG: c++
AUTHOR: maxkibble
*/

#include <iostream>

using namespace std;

const int maxn = 5005;

string s;
int n, ans;
bool ok[maxn][maxn];

int main() {
    cin >> s;
    int n = s.length();
    for(int i = 0; i < n; i++) {
        int l = 0, r = 0, q = 0;
        for(int j = 0; i + j < n; j++) {
            if(s[i + j] == '(') l++;
            else if(s[i + j] == '?') q++;
            else r++;
            if(l + q < r) break;
            ok[i][i + j] = true;
        }
    }
    for(int i = n - 1; i >= 0; i--) {
        int l = 0, r = 0, q = 0;
        for(int j = 0; i - j >= 0; j++) {
            if(s[i - j] == '(') l++;
            else if(s[i - j] == '?') q++;
            else r++;
            if(r + q < l) break;
            if(ok[i - j][i] && j % 2 == 1) {
                ans++;
            }
        }
    }
    cout << ans << endl;
    return 0;
}


猜你喜欢

转载自blog.csdn.net/max_kibble/article/details/79212306
今日推荐