Ural1574
2014-02-24 17:31
246 查看
C - Mathematicians and brackets
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d
& %I64u
Submit Status Practice URAL
1574
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.
An empty string is a regular sequence of brackets.
If a string a is a regular sequence of brackets, then the string (a) is also a regular sequence of brackets.
If strings a and b are regular sequences of brackets, then the string ab is also a regular sequence of brackets.
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
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d
& %I64u
Submit Status Practice URAL
1574
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.
An empty string is a regular sequence of brackets.
If a string a is a regular sequence of brackets, then the string (a) is also a regular sequence of brackets.
If strings a and b are regular sequences of brackets, then the string ab is also a regular sequence of brackets.
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;
}
相关文章推荐
- 从myeclipse导入eclipse,不能识别为web项目(java项目转为web项目) 转
- WebView之加载网页时增加进度提示
- 互联网思维下的传统企业转型思考
- 基于深度优先遍历的随机迷宫算法
- js判断为空Null与字符串为空简写方法
- 下班了晚上请同学吃饭
- 大一上半年
- 结构体和函数
- 电脑快捷键
- mtk android settings
- jQuery Mobile 网格
- 694 - The Collatz Sequence
- Hadoop 0.20.2集群配置
- 黑马程序员_7K面试题_银行业务调度系统
- 高性能服务器架构
- ubuntu VMware 硬盘扩容
- 结构体和函数
- 编程之美 set 5 寻找数组中最大值和最小值
- Oracle中having1子句的用法
- iOS学习之二十:应用的归档打包以及distribution测试和上传到App Store