sicily9457. Typo

Description

 Bessie has just purchased a new laptop computer, but she unfortunately finds herself unable to type well, given the size of her large hooves relative to the small keyboard.  Bessie has just attempted to type in one of her favorite patterns -- a balanced string of parentheses.  However, she realizes that she might have mis-typed one character, accidentally replacing ( with ) or vice versa.  Please help Bessie compute the number of locations in the string such that reversing the single parenthesis at that location would cause the entire string to become balanced.

There are several ways to define what it means for a string of parentheses to be "balanced".  Perhaps the simplest definition is that there must be the same total number of ('s and )'s, and for any prefix of the string, there must be at least as many ('s as )'s. 

For example, the following strings are all balanced:

()

(())

()(()())

while these are not:

)(

())(

((())))

Input

* Line 1: A string of parentheses of length N (1 <= N <= 100,000).

 

Output

 * Line 1: The number of positions within the input string (if any) such that reversing the parenthesis at that single position would cause the entire string to become balanced.

Sample Input
 Copy sample input to clipboard
()(())))
Sample Output
4
Hint

If we look at the input string closely: ()(())))

we find that reversing the direction of the parenthesis at position 2 results in a balanced string: (((())))

Similarly, reversing the parenthesis at position 5, at position 6, or at position 7, also results in a balanced string.

很水的通过代码,就是通过stack来处理一下,把匹配的去掉,然后找到不匹配的那个符号,把中间的相同的符号数一下有多少个,就是有多少种,详细看下面的代码。的确很水。

#include <iostream>
#include <stdio.h>
#include <string>
#include <stack>
#include <cstring>
using namespace std;

int main()
{
    string str;
    cin >> str;
    int len = str.size();
    stack<int> sk;
    for (int i = 0; i < len; ++ i)
    {
        if (sk.empty())
        {
            sk.push(i);
        }
        else if (str[sk.top()] == '(' && str[i] == ')')
        {
            sk.pop();
        }
        else
        {
            sk.push(i);
        }
    }
    int m_count = 0;
    if (sk.size() >= 2)
    {
        int temp = sk.top();
        sk.pop();
            int temp2 = sk.top();
        if (str[temp] == ')')
        {
            for (int i = 0; i < temp; ++ i)
            {
                if (str[i] == ')')
                {
                    ++m_count;
                }
            }
        }
        else
        {
            for (int i = temp; i < len; ++ i)
            {
                if (str[i] == '(')
                {
                    ++ m_count;
                }
            }
        }
    }
    cout << m_count << endl;
   // system("pause");
}                                 


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值