Star in Parentheses

题目描述

You are given a string S, which is balanced parentheses with a star symbol '*' inserted.

Any balanced parentheses can be constructed using the following rules:

An empty string is balanced.

Concatenation of two balanced parentheses is balanced.

If T is balanced parentheses, concatenation of '(', T, and ')' in this order is balanced.

For example, '()()' and '(()())' are balanced parentheses. ')(' and ')()(()' are not balanced parentheses.

Your task is to count how many matching pairs of parentheses surround the star.

Let Si be the i-th character of a string S. The pair of Sl and Sr (l<r) is called a matching pair of parentheses if Sl is '(', Sr is ')' and the surrounded string by them is balanced when ignoring a star symbol.

输入

The input consists of a single test case formatted as follows.

S

S is balanced parentheses with exactly one '*' inserted somewhere. The length of S is between 1 and 100, inclusive.

输出

Print the answer in one line.

样例输入

((*)())

样例输出

2

题意+题解

题意:有多少个有效的括号对能把星号包起来,如果能和近的消掉的括号不是有效的

如果星号左边出现( , l ++ ,

星号左边出现 ) ,如果 l !=0,l--;

如果星号右边出现( , pl ++ ,

星号右边出现 ) ,如果 pl !=0,l--; 否则r++

那么l 和 r之间的最小值就是括号对的个数

代码

#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define rep(i,a,n) for(int i=a;i<n;++i)
#define readc(x) scanf("%c",&x)
#define read(x) scanf("%d",&x)
#define sca(x) scanf("%d",&x)
#define sca2(x,y) scanf("%d%d",&x,&y)
#define sca3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define print(x) printf("%d\n",x)
#define mst(a,b) memset(a,b,sizeof(a))
#define lowbit(x) x&-x
#define lson(x) x<<1
#define rson(x) x<<1|1
#define pb push_back
#define mp make_pair
typedef long long ll;
typedef pair<int,int> P;
const int INF =0x3f3f3f3f;
const int inf =0x3f3f3f3f;
const int mod = 1e9+7;
const int MAXN = 105;
const int maxn =1000010;
using namespace std;
char s[15000];
int main(){
scanf("%s",s);
int flag = 0 ;
int r = 0;
int l = 0;
int pl = 0;
for(int i = 0; i < strlen(s);i++){
if(s[i] == '*') {
flag = 1;
}
if(!flag && s[i] =='(')
l++;
else if(!flag && s[i] ==')'){
if(l) l--;
}
else if(flag && s[i] == '('){
pl++;
}
else if(flag && s[i] == ')'){
if(pl) pl --;
else r++;
}
}
printf("%d\n",l < r ? l : r);
}

upc组队赛7 Star in Parentheses的更多相关文章

  1. Star in Parentheses

    问题 A: Star in Parentheses 时间限制: 1 Sec  内存限制: 128 MB 题目描述 You are given a string S, which is balanced ...

  2. upc组队赛3 Chaarshanbegaan at Cafebazaar

    Chaarshanbegaan at Cafebazaar 题目链接 http://icpc.upc.edu.cn/problem.php?cid=1618&pid=1 题目描述 Chaars ...

  3. upc组队赛1 过分的谜题【找规律】

    过分的谜题 题目描述 2060年是云南中医学院的百年校庆,于是学生会的同学们搞了一个连续猜谜活动:共有10个谜题,现在告诉所有人第一个谜题,每个谜题的答案就是下一个谜题的线索....成功破解最后一个谜 ...

  4. upc组队赛1 不存在的泳池【GCD】

    不存在的泳池 题目描述 小w是云南中医学院的同学,有一天他看到了学校的百度百科介绍: 截止到2014年5月,云南中医学院图书馆纸本藏书74.8457万册,纸质期刊388种,馆藏线装古籍图书1.8万册, ...

  5. upc组队赛1 黑暗意志【stl-map】

    黑暗意志 题目描述 在数千年前潘达利亚从卡利姆多分离之时,迷雾笼罩着这块新形成的大陆,使它不被外来者发现.迷雾同样遮蔽着这片大陆古老邪恶的要塞--雷神的雷电王座.在雷神统治时期,他的要塞就是雷电之王力 ...

  6. upc组队赛1 闪闪发光 【优先队列】

    闪闪发光 题目描述 一所位于云南昆明的中医药本科院校--云南中医学院. 因为报考某专业的人数骤减,正面临着停招的危机. 其中有九名少女想到一条妙计--成为偶像, 只要她们成为偶像,学校的名气便会增加, ...

  7. upc组队赛1 流连人间的苏苏

    流连人间的苏苏 题目描述 苏苏在做红尘仙的任务时,发现坐落于风景秀丽.四季如春的昆明市的云南中医学院. 没过多久,苏苏就喜欢上了这个学校.以致于苏苏忘了回涂山的时间,现在她只剩下d天的时间待在云南中医 ...

  8. upc组队赛18 THE WORLD【时间模拟】

    THE WORLD 题目链接 题目描述 The World can indicate world travel, particularly on a large scale. You mau be l ...

  9. upc 组队赛18 STRENGTH【贪心模拟】

    STRENGTH 题目链接 题目描述 Strength gives you the confidence within yourself to overcome any fears, challeng ...

随机推荐

  1. 17. Jmeter-取样器一

    jmeter-sampler介绍与使用 HTTP请求 Test Action Debug Sampler AJP/1.3 Sampler Access Log Sampler BeanShell Sa ...

  2. MongoDB简单认识

    MongoDB 为何物 NoSQL 泛指非关系型数据库,该词是关系型数据库(即 SQL)的相对称呼.MongoDB 是非关系型数据库中较为人熟知的一种. 它拥有很多优秀特性,例如高性能.高可用.支持丰 ...

  3. VB - 修改注册表

    Vbs中修改注册表的语句主要有: 1.读注册表的关键词和值: 可以通过把关键词的完整路径传递给wshshell对象的regread方法.例如: set ws=wscript.createobject( ...

  4. Ubuntu安装护眼程序

    目录 1.安装 2.配置 参考资料 一开始想在Ubuntu下安装在Windows下使用的f.lux,但是折腾了很久f.lux也没能正常运作.于是打开另一台Ubuntu电脑,将上面使用的Redshift ...

  5. if语句的嵌套使用之获取三个数据的最大值

    获取三个数据的最大值: class Hello2 { public static void main(String[] args) { int a = 10; int b = 20; int c = ...

  6. FastJSON 远程执行漏洞,速速升级!

    相信大家用 FastJSON 的人应该不少,居然有漏洞,还不知道的赶紧往下看,已经知道此漏洞的请略过-- 2019年6月22日,阿里云云盾应急响应中心监测到FastJSON存在0day漏洞,攻击者可以 ...

  7. python基础--快速排序

    1.快速排序 快速排序是一种交换排序. 快速排序由C. A. R. Hoare在1962年提出. 它的基本思想是:通过一趟排序将要排序的数据分割成独立的两部分:分割点左边都是比它小的数,右边都是比它大 ...

  8. Idea maven项目不能新建package和class的解决【转】

    如图,新建的maven项目不能新建package 这是因为java是普通的文件夹,要设置为 现在就可以了 博客原链接:http://blog.csdn.net/qq_24949727/article/ ...

  9. 【记录】解决前端form表单回车禁止刷新页面

    最近弄前端 有form表单的情况下 按回车会自动刷新当前页面. 现记录解决方案如下: 1.去掉表单 2.不要让表单中只有一个文本框(增加一个隐藏的文本框就行) 3.以上两点都不想使用,那么就还可以在表 ...

  10. Java 代码规范,你应该知道的一些工具和用法(转)

    转自:http://yifeng.studio/2017/06/30/coding-with-code-style/ Java 代码规范,你应该知道的一些工具和用法 2017-06-30 从事编程这个 ...