I’m out of stories. For years I’ve been writing stories, some rather silly, just to make simple problems look difficult and complex problems look easy. But, alas, not for this one. 
You’re given a non empty string made in its entirety from opening and closing braces. Your task is to find the minimum number of “operations” needed to make the string stable. The definition for being stable is as follows: 
1. An empty string is stable. 
2. If S is stable, then {S} is also stable. 
3. If S and T are both stable, then ST (the concatenation of the two) is also stable. 
All of these strings are stable: {}, {}{}, and {{}{}}; But none of these: }{, {{}{, nor {}{. 
The only operation allowed on the string is to replace an opening brace with a closing brace, or visa-versa. 

InputYour program will be tested on one or more data sets. Each data set is described on a single line. The line is a non-empty string of opening and closing braces and nothing else. No string has more than 2000 braces. All sequences are of even length.
The last line of the input is made of one or more ’-’ (minus signs.)

OutputFor each test case, print the following line: 
k. N 
Where k is the test case number (starting at one,) and N is the minimum number of operations needed to convert the given string into a balanced one. 
Note: There is a blank space before N. 
Sample Input

}{
{}{}{}
{{{}
---

Sample Output

1. 2
2. 0
3. 1
通过代码

#include<iostream>
#include<stdio.h>
#include<stack>
#include<string.h>
using namespace std;
int main()
{
char ch[2000];
stack<int>st;
int t=0;
while(++t)
{
gets(ch);
int leng=strlen(ch);
int jishu=0,sign=0;
for(int i=0;i<leng;i++)
{
if(ch[i]=='-')
{
sign=1;
break;
}
if(ch[i]=='{'&&st.empty())
{
st.push(1);
continue;
}
if(ch[i]=='{'&&st.top()==1)
{
st.push(1);
continue;
}
if(ch[i]=='}'&&st.empty())
{
st.push(1);
jishu++;
continue;
}
if(ch[i]=='}'&&st.top()==1)
{
st.pop();
continue;
}
}
if(sign==1) break;
else
{
if(st.empty()) cout<<t<<". "<<jishu<<endl;
else cout<<t<<". "<<jishu+st.size()/2<<endl;
}
jishu=0;                                //每次做完循环计数归0
while(!st.empty()) st.pop();  //每次做完循环清空栈
}
}

栈 <stack> F - 宋飞正传的更多相关文章

  1. F——宋飞正传(HDU3351)

    题目:   I’m out of stories. For years I’ve been writing stories, some rather silly, just to make simpl ...

  2. HDU 3351 Seinfeld 宋飞正传(水)

    题意: 给出一个串,串内只有大括号,问经过几次改变可使全部括号合法?改变指的是可以将某一方向的括号变成另一方向. 思路: 利用栈的特点,若出现成对的合法括号,直接删掉,留下那些不合法的成为一串.既然不 ...

  3. 栈(stack)、递归(八皇后问题)、排序算法分类,时间和空间复杂度简介

    一.栈的介绍: 1)栈的英文为(stack)2)栈是一个先入后出(FILO-First In Last Out)的有序列表.3)栈(stack)是限制线性表中元素的插入和删除只能在线性表的同一端进行的 ...

  4. BSS段 data段 text段 堆heap 和 栈stack

    BSS段:BSS段(bss segment)通常是指用来存放程序中未初始化的全局变量的一块内存区域.BSS是英文Block Started by Symbol的简称.BSS段属于静态内存分配.   数 ...

  5. [转]JVM 内存初学 (堆(heap)、栈(stack)和方法区(method) )

    这两天看了一下深入浅出JVM这本书,推荐给高级的java程序员去看,对你了解JAVA的底层和运行机制有比较大的帮助.废话不想讲了.入主题: 先了解具体的概念:JAVA的JVM的内存可分为3个区:堆(h ...

  6. 堆heap和栈Stack(百科)

    堆heap和栈Stack 在计算机领域,堆栈是一个不容忽视的概念,堆栈是两种数据结构.堆栈都是一种数据项按序排列的数据结构,只能在一端(称为栈顶(top))对数据项进行插入和删除.在单片机应用中,堆栈 ...

  7. (转)Java里的堆(heap)栈(stack)和方法区(method)(精华帖,多读读)

    [color=red][/color]<一> 基础数据类型直接在栈空间分配, 方法的形式参数,直接在栈空间分配,当方法调用完成后从栈空间回收.   引用数据类型,需要用new来创建,既在栈 ...

  8. STL(标准模板库) 中栈(stack)的使用方法

    STL 中栈的使用方法(stack) 基本操作: stack.push(x)  将x加入栈stack中,即入栈操作 stack.pop()  出栈操作(删除栈顶),只是出栈,没有返回值 stack.t ...

  9. 【Java数据结构学习笔记之二】Java数据结构与算法之栈(Stack)实现

      本篇是java数据结构与算法的第2篇,从本篇开始我们将来了解栈的设计与实现,以下是本篇的相关知识点: 栈的抽象数据类型 顺序栈的设计与实现 链式栈的设计与实现 栈的应用 栈的抽象数据类型   栈是 ...

随机推荐

  1. nyoj--236--心急的C小加(动态规划&&LIS)

    心急的C小加 时间限制:1000 ms  |  内存限制:65535 KB 难度:4 描述 C小加有一些木棒,它们的长度和质量都已经知道,需要一个机器处理这些木棒,机器开启的时候需要耗费一个单位的时间 ...

  2. (转载) 使用DrawerLayout和NavigationView从右侧出现

    使用DrawerLayout和NavigationView从右侧出现 2016-07-21 17:53 957人阅读 评论(0) 收藏 举报  分类: android(9)  版权声明:本文为博主原创 ...

  3. python编程基础

    Date: 2019-05-27 Author: Sun 1. 程序 为了完成某种特定功能,以某种程序设计语言编写的有序指令的集合.程序是指挥cpu工作的"工作手册".计算机只能执 ...

  4. cuDNN编写卷积实例

    转载至http://www.goldsborough.me/cuda/ml/cudnn/c++/2017/10/01/14-37-23-convolutions_with_cudnn/ Convolu ...

  5. Bedrock Linux 0.7.3 发布

    Bedrock Linux是一种元分发,允许用户利用其他通常互斥的Linux发行版的功能,并让它们无缝地一起工作.该项目发布了其0.7.x系列,Bedrock Linux 0.7.3的更新. 新的更新 ...

  6. MHA搭建及故障维护

    MHA是一种方便简单可靠的MySQL高可用架构,具体的介绍我在这里就不多说了,下面是我在网上找的一个教程,我在此基础上进行了一些修改: 大致步骤 (一).环境介绍 (二).用ssh-keygen实现四 ...

  7. python+selenium进行简单验证码获取

    # _*_ coding:utf-8 _*_from PIL import Imagefrom selenium import webdriverimport pytesseractimport ti ...

  8. [转] 经典排序算法 - 基数排序Radix sort

    原理类似桶排序,这里总是需要10个桶,多次使用 首先以个位数的值进行装桶,即个位数为1则放入1号桶,为9则放入9号桶,暂时忽视十位数 例如 待排序数组[62,14,59,88,16]简单点五个数字 分 ...

  9. nested exception is java.lang.NoClassDefFoundError: org/codehaus/jettison/json/JSONObject异常的解决办法

    解决办法:你可以尝试添加一个jar包,因为我加入了一个jar包后错误问题成功解决. 将所需要的jettison-1.2.jar包复制到lib文件夹里面,重启项目,问题搞定.

  10. LeetCode 之 Merge Sorted Array(排序)

    [问题描写叙述] Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array ...