M - 13 HDU - 3351 (栈)

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.

Input

Your 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.)
Output
For 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<algorithm>
#include<cstring>
#include<cstdio>
#include<stack>
using namespace std;
stack<char>s;
char str[2005];
int main()
{int t=1;while(gets(str),str[0]!='-'){int i,len=strlen(str);s.push(str[0]);for(int i=1;i<len;i++){if(s.empty()==0){if(s.top()=='{'&&str[i]=='}')s.pop();else s.push(str[i]);}else{s.push(str[i]);} }int sum1=0,sum2=0;while(s.empty()==0){if(s.top()=='{')sum1++;else sum2++;s.pop();}int ans=(sum1+1)/2+(sum2+1)/2;printf("%d. %d\n", t++, ans);}return 0;
} 

M - 13 HDU - 3351 (栈)相关推荐

  1. 欧尼酱讲JVM(13)——本地方法栈

    位置图解 本地方法栈在运行时数据区中,三个灰色的部分是线程私有的. 本地方法栈作用 Java虚拟机栈用于管理Java方法的调用,而本地方法栈用于管理本地方法的调用. 本地方法栈是线程私有了,允许被线程 ...

  2. HDU 4358 树状数组+思路

    http://acm.hdu.edu.cn/showproblem.php?pid=4358 如图所示,当k==3时,如果我们扫描到红线所在的位置. 则符合条件的区间就是从红线到两条紫线所包含的区间( ...

  3. Callable、Future阻塞队列阻塞栈

    Callable.Future   简单应用 在Java5之前,线程是没有返回值的,常常为了"有"返回值,破费周折,而且代码很不好写.或者干脆绕过这道坎,走别的路了.现在Java终 ...

  4. 数据结构第八篇——链栈

    ♥注:未经博主同意,不得转载. 链栈 链式存储的栈称为链栈.可用单链表来实现链栈,因此其节点结构与单链表的结构相同.下面给出这种结构的定义: 若要了解顺序栈的内容请跳转至http://www.cnbl ...

  5. Java线程详解(15)-阻塞队列和阻塞栈

    Java线程:新特征-阻塞队列 阻塞队列是Java5线程新特征中的内容,Java定义了阻塞队列的接口java.util.concurrent.BlockingQueue,阻塞队列的概念是,一个指定长度 ...

  6. java 取栈顶元素_《Java实战之内存模型》详解篇

    内存是非常重要的系统资源,是硬盘和CPU的中间仓库及桥梁,承载着操作系统和应用程序的实时运行 JVM内存布局规定了Java在运行过程中内存申请.分配.管理的策略,保证了JVM的高效稳定运行 不同的JV ...

  7. c语言堆栈基本代码入栈出栈_C/C++语言栈的出栈入栈操作实现(完整代码)

    /* 完整代码 */ #include using namespace std; #define MAX_SIZE 5 //数组大小 struct stack { int top; int ans[M ...

  8. 顺序栈的基本操作c语言源代码,顺序栈的栈基本操作(C语言版)

    1 #include 2 #include 3 #define STACK_INIT_SIZE 100//储存空间初始分配量 4 #define STACKINCREMENT 10//存储空间分配增量 ...

  9. Java岗最全面试攻略,吃透这些技术栈Offer拿到手软

    前言 我分享的这份 Java 后端开发面试总结包含了 JavaOOP.Java 集合容器.Java 异常.并发编程.Java 反射.Java 序列化.JVM.Redis.Spring MVC.MyBa ...

  10. 《数据结构》实验三:栈和队列实验 (实验报告)

    一.实验目的 巩固栈和队列数据结构,学会运用栈和队列. 1.回顾栈和队列的逻辑结构和受限操作特点,栈和队列的物理存储结构和常见操作. 2.学习运用栈和队列的知识来解决实际问题. 3.进一步巩固程序调试 ...

最新文章

  1. [渝粤教育] 西南科技大学 高频电子线路 在线考试复习资料2021版
  2. 客户端获取游客IP,获取客户地理信息,展示地图
  3. 设置UIImage的边框和圆角大小以及颜色
  4. go语言导入git包_使用go module导入本地包的方法教程详解
  5. 迁移至Kubernetes的三种主要方式对比
  6. VALSE学习(八):矿视-轻量级深度模型的研究与实践
  7. adobe出的cookbook
  8. MapXtreme 2005 学习心得 了解新建MapXtreme项目结构(二)
  9. js 的arguments的一些理解资料
  10. sql server2014使用BULK INSERT导入UTF-8数据中文乱码问题
  11. 梦幻西游易语言辅助教程
  12. cygwin安装指南
  13. DirectX12初始化三——DirectX图形基础结构,功能支持检测,资源驻留
  14. 关于monitor模式
  15. 一个简单的多线程实现
  16. 侦察系列之IP查询网址推荐
  17. 对接淘宝天猫平台的第一篇
  18. MySQL 关闭过程
  19. shinelon自动修复此计算机,炫龙 shinelonV5 Pro 笔记本改win7详细图文教程
  20. fastapi 大型应用_FastAPI使用小结

热门文章

  1. C#两种创建快捷方式的方法
  2. [转载] ANTLR——语法分析
  3. 踩坑事件:windows操作系统下的eclipse中编写SparkSQL不能从本地读取或者保存parquet文件
  4. 使用dialog插件弹出提示和确定信息对话框8-8
  5. 《Arduino开发实战指南:机器人卷》一1.3 安装Arduino Uno驱动
  6. Codeforces Round #364 (Div. 1) 700B(树)
  7. 4.4 Hibernate高级功能
  8. Android JSON 解析库的使用 - Gson 和 fast-json
  9. 追随你心 —— 聊聊VMCloud
  10. spring和mybatis整合代码