Ural1574

C - Mathematicians and brackets
Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

Description

Once upon a time three mathematicians met…
  • The first of them wrote a sequence of brackets on a chalkboard.
  • The second one wondered if there was a cyclic shift turning that sequence into a regular one.
  • After thinking for a while, the third mathematician told the number of such shifts.
You are given the sequence of brackets written by the first mathematician and you are to find the number told by the third mathematician. A regular sequence of brackets is defined as follows.
  1. An empty string is a regular sequence of brackets.
  2. If a string a is a regular sequence of brackets, then the string (a) is also a regular sequence of brackets.
  3. If strings a and b are regular sequences of brackets, then the string ab is also a regular sequence of brackets.
  4. There are no other regular sequences of brackets.
A string  a is a cyclic shift of a string  b if  a and  b have the same lengths and  a consists of some (possibly empty) suffix from  b followed by a prefix from  b.

Input

The only line of the input contains the sequence of brackets written by the first mathematician. The sequence is non-empty and its length doesn't exceed 10  5.

Output

Output the number of cyclic shifts turning the given sequence into a regular one.

Sample Input

input output
)(()
1
)()(
2
()
1


#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
using namespace std;
#define MAXN 100010
string input;
int main()
{
    int len, t, NumShift, vMin;
    cin>>input;
    len = input.size();
    t = 0;
    vMin = MAXN;
    for(int i = 0; i < len; i++){
        if(input[i] == '(') t++;
        else if(input[i] == ')') t--;
        if(vMin > t) vMin = t;
    }
    NumShift = 0;
    if(t != 0)
        cout<<0<<endl;
    else {
        for(int i = 0; i < len; i++){
            if(input[i] == '(') vMin--;
            else if(input[i] == ')') vMin++;
            if(vMin >= 0) NumShift++;
        }
        cout<<NumShift<<endl;
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值