栈 <stack> F - 宋飞正传
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 - 宋飞正传的更多相关文章
- F——宋飞正传(HDU3351)
题目: I’m out of stories. For years I’ve been writing stories, some rather silly, just to make simpl ...
- HDU 3351 Seinfeld 宋飞正传(水)
题意: 给出一个串,串内只有大括号,问经过几次改变可使全部括号合法?改变指的是可以将某一方向的括号变成另一方向. 思路: 利用栈的特点,若出现成对的合法括号,直接删掉,留下那些不合法的成为一串.既然不 ...
- 栈(stack)、递归(八皇后问题)、排序算法分类,时间和空间复杂度简介
一.栈的介绍: 1)栈的英文为(stack)2)栈是一个先入后出(FILO-First In Last Out)的有序列表.3)栈(stack)是限制线性表中元素的插入和删除只能在线性表的同一端进行的 ...
- BSS段 data段 text段 堆heap 和 栈stack
BSS段:BSS段(bss segment)通常是指用来存放程序中未初始化的全局变量的一块内存区域.BSS是英文Block Started by Symbol的简称.BSS段属于静态内存分配. 数 ...
- [转]JVM 内存初学 (堆(heap)、栈(stack)和方法区(method) )
这两天看了一下深入浅出JVM这本书,推荐给高级的java程序员去看,对你了解JAVA的底层和运行机制有比较大的帮助.废话不想讲了.入主题: 先了解具体的概念:JAVA的JVM的内存可分为3个区:堆(h ...
- 堆heap和栈Stack(百科)
堆heap和栈Stack 在计算机领域,堆栈是一个不容忽视的概念,堆栈是两种数据结构.堆栈都是一种数据项按序排列的数据结构,只能在一端(称为栈顶(top))对数据项进行插入和删除.在单片机应用中,堆栈 ...
- (转)Java里的堆(heap)栈(stack)和方法区(method)(精华帖,多读读)
[color=red][/color]<一> 基础数据类型直接在栈空间分配, 方法的形式参数,直接在栈空间分配,当方法调用完成后从栈空间回收. 引用数据类型,需要用new来创建,既在栈 ...
- STL(标准模板库) 中栈(stack)的使用方法
STL 中栈的使用方法(stack) 基本操作: stack.push(x) 将x加入栈stack中,即入栈操作 stack.pop() 出栈操作(删除栈顶),只是出栈,没有返回值 stack.t ...
- 【Java数据结构学习笔记之二】Java数据结构与算法之栈(Stack)实现
本篇是java数据结构与算法的第2篇,从本篇开始我们将来了解栈的设计与实现,以下是本篇的相关知识点: 栈的抽象数据类型 顺序栈的设计与实现 链式栈的设计与实现 栈的应用 栈的抽象数据类型 栈是 ...
随机推荐
- xBIM 高级03 更改日志创建
系列目录 [已更新最新开发文章,点击查看详细] 模型中发生的每一个变化都是事务的一部分,这是我们设计的核心.所有事务都是由 IModel 的实现创建的,并且从中被弱引用,因此当使用 using ...
- IP地址的正则表达式写法
这里讲的是IPv4的地址格式,总长度 32位=4段*8位,每段之间用.分割, 每段都是0-255之间的十进制数值. 将0-255用正则表达式表示,可以分成一下几块来分别考虑: 取值区间 特点 正则写法 ...
- Java获取环境变量和系统属性
Java获取服务器环境变量和JVM系统变量 当程序中需要使用与操作系统相关的变量(例如:文件分隔符.换行符)时,Java提供了System类的静态方法getenv()和getProperty() ...
- 第三方库requests
requests库 # 1.记得安装 第三方 模块 requests # pip install requests import requests url = 'http://www.baidu.co ...
- PHP中使用DOM读取解析XML属性值一例
先看XML文件结构,与常见的文件略有不同,数据并不是用闭合标签保存的,而是直接保存在属性值中. <?xml version="1.0" encoding="utf- ...
- U盘安装CentOS 7系统
首先,本文适合新的不能再新的新手 小白出身的轩轩,由于最近正在学习Linux的centos系统,所以突发奇想,为什么不把轩轩的本机也安装一个centos系统呢,让两个系统互不干扰,想到就做到,遂开始动 ...
- 紫书 例题8-17 UVa 1609 (构造法)(详细注释)
这道题用构造法, 就是自己依据题目想出一种可以得到解的方法, 没有什么规律可言, 只能根据题目本身来思考. 这道题的构造法比较复杂, 不知道刘汝佳是怎么想出来的, 我想的话肯定想不到. 具体思路紫书上 ...
- JAVA关于byte数组与String转换的问题
1 public class ToString{ public static void main(String[] args){ String aa = "hellow"; byt ...
- js模拟支付宝提交表单
弄过支付宝的程序猿可能都知道,里面有非常多地方都用到了自提交表单的方式,支付宝的接口通过请求API的形式取得server返回的表单字符串,使用out.print("表单字符串")在 ...
- Linux/UNIX线程(1)
线程(1) 本文将介绍怎样使用多个控制线程在单个进程环境中运行多个任务. 一个进程中的全部线程都能够訪问该进程的组成部件(如文件描写叙述符和内存). 线程包含了表示进程内运行环境必须的信息,当中包含进 ...