hdoj 3351 Seinfeld 【栈的简单应用】
Seinfeld
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.
of even length.
The last line of the input is made of one or more ’-’ (minus signs.)
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.
}{
{}{}{}
{{{}
---
1. 2
2. 0
3. 1
用栈将全部满足配对的都删去。最后剩下的就是}}}{{{或{{{{{或}}}}}统计一下左括号和右括号的数目,处理一下就好了
代码:
#include <stdio.h>
#include <string.h>
#include <stack>
#include <algorithm>
using namespace std; char c[1005], temp[1005];
stack<char> s;
int main(){
int v = 1;
while(gets(c), c[0] != '-'){
int i, len = strlen(c);
s.push(c[0]);
for(i = 1; i < len; i ++){
if(!s.empty()){
if(s.top() == '{'&&c[i] == '}'){
s.pop(); continue;
}
else s.push(c[i]);
}
else s.push(c[i]);
}
int sum1 = 0, sum2 = 0;
while(!s.empty()){
if(s.top() == '}') ++sum1;
else ++sum2;
s.pop();
}
int ans = (sum1+1)/2+(sum2+1)/2;
printf("%d. %d\n", v++, ans);
}
return 0;
}
hdoj 3351 Seinfeld 【栈的简单应用】的更多相关文章
- hdu Train Problem I(栈的简单应用)
Problem Description As the new term comes, the Ignatius Train Station is very busy nowadays. A lot o ...
- 栈回溯简单实现(x86)
0x01 栈简介 首先局部变量的分配释放是通过调整栈指针实现的,栈为函数调用和定义局部变量提供了一块简单易用的空间,定义在栈上的变量不必考虑内存申请和释放.只要调整栈指针就可以分配和释放内存. ...
- Javascript的堆和栈的简单理解
<!doctype html> <html> <head> <meta charset="UTF-8"> <title> ...
- 栈及其简单应用(二)(python代码)
一.括号判定 前一篇文章我们介绍了栈的简单应用中,关于括号的判定,但那只是一种括号的判定,下面我们来介绍多种括号混合使用时,如何判断括号左右一一对应. 比如“{}{(}(][”这种情况,需要对一种括号 ...
- 栈及其简单应用(python代码)
栈属于线性结构(Linear Struncture),要搞清楚这个概念,首先要明白”栈“原来的意思,如此才能把握本质."栈“者,存储货物或供旅客住宿的地方,可引申为仓库.中转站,所以引入到计 ...
- HDU 3351 Seinfeld(括号匹配)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3351 解题报告:输入一个只有'{'跟'}'的字符串,有两种操作,一种是把'{'变成'}',另一种是'} ...
- HDOJ 1022 模拟栈
Train Problem I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- UVA442 Matrix Chain Multiplication 矩阵运算量计算(栈的简单应用)
栈的练习,如此水题竟然做了两个小时... 题意:给出矩阵大小和矩阵的运算顺序,判断能否相乘并求运算量. 我的算法很简单:比如(((((DE)F)G)H)I),遇到 (就cnt累计加一,字母入栈,遇到) ...
- HDU 3351 Seinfeld 宋飞正传(水)
题意: 给出一个串,串内只有大括号,问经过几次改变可使全部括号合法?改变指的是可以将某一方向的括号变成另一方向. 思路: 利用栈的特点,若出现成对的合法括号,直接删掉,留下那些不合法的成为一串.既然不 ...
随机推荐
- mkdir touch vim
vim和touch都用于新建文件 mkdir用于新建文件夹
- 在CNN网络中roi从原图映射到feature map中的计算方法
在使用fast rcnn以及faster rcnn做检测任务的时候,涉及到从图像的roi区域到feature map中roi的映射,然后再进行roi_pooling之类的操作.比如图像的大小是(600 ...
- 关于apache access log 统计的那些事儿
统计APACHE ACCESS.LOG IP访问记录 可以根据自己的需要,统计很多,每个IP访问多少个页面等等! cat access.log-20090904 |awk '{print $3}'|s ...
- python关于入参中,传入的是指针还是引用
偶然看到别人的代码,发现有的会传入参数之后,做一次copy,试验一下,关于入参中,传入的是指针还是引用先说自己的结论:1.如果传入的是简单的类型,那么传入应该是引用的数值,2.假如传入的是df这种类型 ...
- 1、C编程预备计算机知识
一.数据类型 基本数据类型 1.整数 整形 -- int -- 4 短整型 -- short int -- 2 长整型 -- long int --8 2.浮点数(实数) 单精度浮点数 -- floa ...
- layui二次封装
最近一直用layui进行页面的重构,这个框架十分适合我们后台人员开发.简单易用,但是layui本身不支持双向绑定,所以很多情况下,我们在支持动态的控件加载时,需要反复刷新.这里我自己封装了一个comm ...
- 树莓派 - RasberryPi推送数据到cloudMQTT
创建用户 在https://www.cloudmqtt.com/上创建一个帐户 转到右上角的控制面板 点击"创建"按钮 安装lib sudo pip install paho-mq ...
- 杭电 2141 Can you find it? (二分法)
Description Give you three sequences of numbers A, B, C, then we give you a number X. Now you need t ...
- LeetCode 304. Range Sum Query 2D – Immutable
Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper lef ...
- Java学习之File类理解
File类是io包中唯一代表磁盘文件本身的对象.File类定义了一些与平台无关的方法来操作文件,可以通过调用File类中的方法,实现创建.删除.重命名文件等.File类的对象主要用来获取文件本身的一些 ...