Seinfeld

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1624    Accepted Submission(s): 792

Problem Description
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
 
 
题解:

在读入的过程中,把相匹配的删除,最后会存在下面三种形态:

1.    }}}...

2.    {{{...

3.    }}}...{{{...

对于每种形态就很容易求了。

题解:

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
using namespace std;
const int INF=0x3f3f3f3f;
#define mem(x,y) memset(x,y,sizeof(x))
#define SI(x) scanf("%d",&x)
#define PI(x) printf("%d",x)
#define SD(x,y) scanf("%lf%lf",&x,&y)
#define P_ printf(" ")
const int MAXN=;
int dp[MAXN];
char q[MAXN];
typedef long long LL;
int main(){
char s[MAXN];
int kase=;
while(scanf("%s",s),s[]!='-'){
int top=;
for(int i=;s[i];i++){
if(top==||s[i]=='{')q[++top]=s[i];
else if(q[top]=='{'&&s[i]=='}')--top;
else q[++top]=s[i];
}
// for(int i=1;i<=top;i++)printf("%c",q[i]);puts("");
int ans;
if(top==||q[top]=='}')ans=top/;
else if(q[]=='{'&&q[top]=='{')ans=top/;
else{
int ans1=,ans2=,k=;
while(q[k]=='}')k++;
ans1=k-;
ans2=top-k+;
ans=(ans1+)/+(ans2+)/;
}
++kase;
printf("%d. %d\n",kase,ans);
}
return ;
}

Seinfeld(栈模拟)的更多相关文章

  1. HDU 1022 Train Problem I(栈模拟)

    传送门 Description As the new term comes, the Ignatius Train Station is very busy nowadays. A lot of st ...

  2. UVALive 3486/zoj 2615 Cells(栈模拟dfs)

    这道题在LA是挂掉了,不过还好,zoj上也有这道题. 题意:好大一颗树,询问父子关系..考虑最坏的情况,30w层,2000w个点,询问100w次,貌似连dfs一遍都会TLE. 安心啦,这肯定是一道正常 ...

  3. UVALive 7454 Parentheses (栈+模拟)

    Parentheses 题目链接: http://acm.hust.edu.cn/vjudge/contest/127401#problem/A Description http://7xjob4.c ...

  4. poj1363Rails(栈模拟)

    主题链接: id=1363">啊哈哈,点我点我 思路: 这道题就是一道简单的栈模拟. .. .我最開始认为难处理是当出栈后top指针变化了. .当不满足条件时入栈的当前位置怎么办.这时 ...

  5. 【LintCode·容易】用栈模拟汉诺塔问题

    用栈模拟汉诺塔问题 描述 在经典的汉诺塔问题中,有 3 个塔和 N 个可用来堆砌成塔的不同大小的盘子.要求盘子必须按照从小到大的顺序从上往下堆 (如:任意一个盘子,其必须堆在比它大的盘子上面).同时, ...

  6. 51Nod 1289 大鱼吃小鱼 栈模拟 思路

    1289 大鱼吃小鱼 栈模拟 思路 题目链接 https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1289 思路: 用栈来模拟 ...

  7. Code POJ - 1780(栈模拟dfs)

    题意: 就是数位哈密顿回路 解析: 是就算了...尼玛还不能直接用dfs,得手动开栈模拟dfs emm...看了老大半天才看的一知半解 #include <iostream> #inclu ...

  8. HDOJ 4699 Editor 栈 模拟

    用两个栈模拟: Editor Time Limit: 3000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) ...

  9. 吐泡泡(2018年全国多校算法寒假训练营练习比赛(第二场)+栈模拟)+Plug-in(codeforces81A+栈模拟)

    吐泡泡题目链接:https://www.nowcoder.com/acm/contest/74/A 题目: 思路: 这种题目当初卡了我很久,今天早训时遇到一个一样得题,一眼就想到用栈模拟,就又回来把这 ...

  10. 【栈模拟dfs】Cells UVALive - 3486

    题目链接:https://cn.vjudge.net/contest/209473#problem/D 题目大意:有一棵树,这棵树的前n个节点拥有子节点,告诉你n的大小,以及这n个节点各有的子节点个数 ...

随机推荐

  1. jcSQL简明执行流程图

    赶着"黑色七月"的最后一天发一篇记点东西,这个月一共掉了三架飞机,我一直很害怕坐着一架人造的东西飞在几万米的高空,相比自己长出一对翅膀,前者应该要脆弱很多.这些人每个人都因为不同的 ...

  2. Linq to sql 操作

    1.往数据库添加数据 NorthwindDataContext abc = new NorthwindDataContext(); abc.Log = Console.Out; User a = ne ...

  3. yum 安装软件时报Public key for * is not installed

    这个是由于没有导入rpm签名信息引起的 解决方案: rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release

  4. openjpa框架入门_项目框架搭建(二)

    Openjpa2.2+Mysql+Maven+Servlet+JSP 首先说明几点,让大家更清楚整体结构: 官方source code 下载:http://openjpa.apache.org/dow ...

  5. <Win32_20>纯c语言版的打飞机游戏出炉了^_^

    经过昨天的苦战,终于完成了纯C版的打飞机游戏——使用微信打飞机游戏的素材,不过玩法有些不同,下面会有详述 一.概述游戏的玩法.实现效果 1. 游戏第一步,简单判断一下,给你一个准备的时间: 2.选择& ...

  6. 打包ipa分发给测试机安装步骤

    1.确定可以打包的Mac电脑,即该Mac电脑已经具备可以打包的权限. 需要上传一份Mac电脑的描述文件,即csr文件. 2.创建bundle id 3.添加测试设备 4.生成证明描述文件 5.Xcod ...

  7. 获取枚举Name,Value,Description两种方法

    using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI ...

  8. uva 10051 Tower of Cubes(DAG最长路)

    题目连接:10051 - Tower of Cubes 题目大意:有n个正方体,从序号1~n, 对应的每个立方体的6个面分别有它的颜色(用数字给出),现在想要将立方体堆成塔,并且上面的立方体的序号要小 ...

  9. Linux网桥介绍

    网桥的功能类似于二层交换机,作用都是划分冲突域,它们之前且一些细微的差别,此处不展开. Linux网桥作为一个特殊的网桥的实现,有一些自己的特点,因为没有看代码,只能从功能上简单分析一下.个人认为,L ...

  10. jQuery源码笔记——延迟对象

    提供一种方法来执行一个或多个对象的回调函数, Deferred对象通常表示异步事件. 它是回调对象的拓展运用,在jQuery当中非常依赖回调对象. 一个简单的,只解决成功状态下的缓存实例 functi ...