栈 <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篇,从本篇开始我们将来了解栈的设计与实现,以下是本篇的相关知识点: 栈的抽象数据类型 顺序栈的设计与实现 链式栈的设计与实现 栈的应用 栈的抽象数据类型 栈是 ...
随机推荐
- bzoj2748: [HAOI2012]音量调节(背包)
2748: [HAOI2012]音量调节 题目:传送门 题解: sb省选题..呵呵一眼背包: f[i][j]表示第i时刻能否为音量j 代码: #include<cstdio> #inclu ...
- zzulioj--1799--wrz的压岁钱(贪心)
1799: wrz的压岁钱 Time Limit: 1 Sec Memory Limit: 128 MB Submit: 407 Solved: 71 SubmitStatusWeb Boa ...
- zzuoj--10400--海岛争霸(并查集)
10400: B.海岛争霸 Time Limit: 2 Sec Memory Limit: 128 MB Submit: 128 Solved: 46 [Submit][Status][Web B ...
- 编程与算法中的端点问题(linspace(a, b, n),endpoint)
左闭右开,[0, n) ⇒ [0, n-1],共 n 个元素: 1. 列表长与编号 列表(seq,也可以是数组等线性结构)的长度要比末尾元素的编号多 1,比如一个列表内含有 5 个元素,最后一个元素的 ...
- ORA-00980:synonym translation is no longer valid
今天要把测试环境DB的数据更新成最新Production环境的数据,期间发生了一些问题: 1.首先从正式环境exp出想要用户的dmp档 2.drop掉测试环境底下相应用户 3.create测试环境底下 ...
- (转载)所有分类 > 开发语言与工具 > 移动开发 > Android开发 Android中的Service:默默的奉献者 (1)
前言 这段时间在看一些IPC相关的东西,这里面就不可避免的要涉及到service,进程线程这些知识点,而且在研究的过程中我惊觉自己对这些东西的记忆已经开始有些模糊了——这可要不得.于是我就干脆花了点心 ...
- QT笔记 -- (2) 文件相关操作、中文路径乱码
1.显示文件对话框,选择一个目录,显示选中目录中的所有图片的代码如下 主要class: QFileDialog QStringList QFileInfoList QDir void open(){ ...
- lua_pcall与lua_call之间的区别
lua_pcall与lua_call之间的区别 定义: void lua_call (lua_State *L, int nargs, int nresults); int lua_pcall (lu ...
- 常见Json字符串反序列化处理方式总结
常用来处理Json字符串序列化 反序列化组件:Newtonsoft.Json (https://www.newtonsoft.com/json) 参考资料https://www.cnblogs.com ...
- UE4.18.3 C++项目无法打开C++类问题(VS2017)
升级VS2017后,突然出现UE4创建C++项目无法打开VS问题.经过测试为VS2017升级后C++的桌面开发工具集采用了VC++2017 v141工具集,而UE4对该工具集尚未兼容,故找不到vs打开 ...