栈 <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篇,从本篇开始我们将来了解栈的设计与实现,以下是本篇的相关知识点: 栈的抽象数据类型 顺序栈的设计与实现 链式栈的设计与实现 栈的应用 栈的抽象数据类型 栈是 ...
随机推荐
- [JZOJ 5437] [NOIP2017提高A组集训10.31] Sequence 解题报告 (KMP)
题目链接: http://172.16.0.132/senior/#main/show/5437 题目: 题解: 发现满足上述性质并且仅当A序列的子序列的差分序列与B序列的差分序列相同 于是我们把A变 ...
- (一)Eureka 服务的注册与发现
(一)服务的注册于发现(eureka); Eureka Server: 服务注册中心,负责服务列表的注册.维护和查询等功能 在Idea里,新建项目,选择Spring initializer. 下面的p ...
- Mysql表引擎的切换
转换MYSQL表的引擎 1.方法一:Alter table 将1个表的引擎修改为另一个引擎最简单的方法是使用Alter table语句. 下面的语句将myTable的引擎修改为InnoDB mysql ...
- lua_pcall与lua_call之间的区别
lua_pcall与lua_call之间的区别 定义: void lua_call (lua_State *L, int nargs, int nresults); int lua_pcall (lu ...
- pace.js 原理(转)
pace.js监控了什么: pace.js对于加载进度监控了什么呢?通过阅读源码,我们看到整体的进度有四个部分组成:document,elements,eventLag和ajax这四种监视器(Moni ...
- tree编译
没有tree命令,就需要下载源代码 [root@fyc tree-1.7.0]#cd /opt/src [root@fyc tree-1.7.0]# wget ftp://mama.indstate. ...
- 洛谷 P1052 过河 (离散化+dp)
dp非常好想, f[i] = min(f[i-len] + stone[i]) s <= len <= t 然后因为L非常大,所以我就不知道该怎么搞了 我看到m只有100,而L有1e9,我 ...
- 锐捷SNMp注意:
http://www.ruijie.com.cn/fw/wt/18855 1)设备名称.运行时间.sysOID.设备描述.联系人.设备位置:分别来自RFC-1213-MIB的system组中的sysN ...
- C#-类 字段 属性 方法 特性
方法的签名 包括参数个数.参数类型.方法返回值 base和this关键字 1.使用base关键字,就可以使用父类中的字段.属性.方法 2.一旦父类中存在有参数的构造函数,那么子类就必须使用base来覆 ...
- [MST] Loading Data from the Server using lifecycle hook
Let's stop hardcoding our initial state and fetch it from the server instead. In this lesson you wil ...