问题 A: Star in Parentheses

时间限制: 1 Sec  内存限制: 128 MB

题目描述

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);
}

Star in Parentheses的更多相关文章

  1. upc组队赛7 Star in Parentheses

    Star in Parentheses 题目描述 You are given a string S, which is balanced parentheses with a star symbol ...

  2. 【Star CCM+实例】开发一个简单的计算流程.md

    流程开发在CAE过程中处于非常重要的地位. 主要的作用可能包括: 将一些经过验证的模型隐藏在流程中,提高仿真的可靠性 将流程封装成更友好的界面,降低软件的学习周期 流程开发实际上需要做非常多的工作,尤 ...

  3. github中的watch、star、fork的作用

    [转自:http://www.jianshu.com/p/6c366b53ea41] 在每个 github 项目的右上角,都有三个按钮,分别是 watch.star.fork,但是有些刚开始使用 gi ...

  4. [deviceone开发]-Star分享的几个示例

    一.简介 这个是star早期分享的几个示例,都非常实用,包括弹出的菜单,模拟支付密码输入等.初学者推荐.也可以直接使用.二.效果图 三.相关下载 https://github.com/do-proje ...

  5. 时隔一年再读到the star

    The Star Arthur C. Clarke It is three thousand light-years to the Vatican. Once, I believed that spa ...

  6. [LeetCode] Remove Invalid Parentheses 移除非法括号

    Remove the minimum number of invalid parentheses in order to make the input string valid. Return all ...

  7. [LeetCode] Different Ways to Add Parentheses 添加括号的不同方式

    Given a string of numbers and operators, return all possible results from computing all the differen ...

  8. [LeetCode] Longest Valid Parentheses 最长有效括号

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  9. [LeetCode] Generate Parentheses 生成括号

    Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...

随机推荐

  1. oracle_dataGuard_11G

    [李红]--切记_从库只安装实例_不需要 dbca 创建数据库 但是 netca 创建监听看个人喜欢,我反正是创建了.[DataGuard_主数据库的参数配置]1.启用 force logging 功 ...

  2. Java安装及基础01

    Java特性: (1)java语言是面向对象的语言 (2)编译一次,到处运行(跨平台) (3)高性能 配置环境变量: JAVA命名规则: (1)常量命名规则:每个字母都大写(POEPLE_PRE_NO ...

  3. 共享文件 Ubuntu下安装Samba与Windows

    Author:Xianghai Ding.Date:2019/01/08**************************************************************安装 ...

  4. Docker Machine批量安装docker host

    Dokcer Machine Docker Machine 可以批量安装和配置 docker host   提高docker的安装效率   同时减少人工安装操作的失误 [root@localhost ...

  5. 【转】mysql保存图片技术决定:保存二进制文件还是只保存图片相对路径,图片放在硬盘上面?

    最近遇到上面这个问题,一开始我就果断否决了数据库保存图片的策略,主要是太蠢!事实上我的决定是正确的,我仅仅理解为mysql读写性能提高的境界,具体为什么可以提高?很模糊,知道我看到了这里: 大佬做的实 ...

  6. cocos 水果机,老Tiger虎机流水灯,博彩大转盘效果

    原(http://www.cnblogs.com/zisou/p/cocos2d-xZhuanpan.html) 博彩大转盘,转盘抽奖的小系统,这是一个很有意思的游戏模块,游戏中增加这样一些趣味的小模 ...

  7. Linux系统——MHA-Atlas-MySQL高可用集群

    Linux系统——MHA-Atlas-MySQL高可用集群 MHA MHA介绍MHA(Master High Availability)目前在MySQL高可用方面是一个相对成熟的解决方案,是一套优秀的 ...

  8. IOT

    文档地址:http://www.owasp.org.cn/owasp-project/owasp-things?searchterm=iot 今天面试,面试官问到iot

  9. docker运行jar文件

    一.环境 本地虚拟机安装的centos7 二.安装docker yum install docker-engine 三.开启阿里云加速 docker的镜像仓库在国外,下载会很慢,启用阿里云加速. 在/ ...

  10. 贝叶斯公式由浅入深大讲解—AI基础算法入门

    1 贝叶斯方法 长久以来,人们对一件事情发生或不发生的概率,只有固定的0和1,即要么发生,要么不发生,从来不会去考虑某件事情发生的概率有多大,不发生的概率又是多大.而且概率虽然未知,但最起码是一个确定 ...