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. 客户端持久化解决方案:indexedDB

    客户端持久化解决方案:indexedDB indexedDB适合大量的结构化的数据存储:打开数据库和获取数据对象都是异步的: 需要开启事务,访问的objectStore都要是在开启的事务中. 数据库结 ...

  2. linux 进程(一)---基本概念

    一.进程的定义         进程是操作系统的概念,每当我们执行一个程序时,对于操作系统来讲就创建了一个进程,在这个过程中,伴随着资源的分配和释放.可以认为进程是一个程序的一次执行过程.   二.进 ...

  3. Codeforces 325E

    Codeforces 325E 原题 题目描述:给出\(n\)个点, 编号为\(0 \text ~ n-1\),每个点连出两条边分别为\(2i\)和\(2i+1\)(\(mod n\)),现从\(0\ ...

  4. cdoj 791 Frozen Rose-Heads

    //本来想做白书上一题 结果发现又要二染色 又要dp的 想了两个小时没想通 然后做了个傻逼题安慰自己 解:不多说,就是递归到叶节点,然后回来的时候在解决子树和直接删边的代价中间取个最小值 #inclu ...

  5. poj3650---将一个字符串中的特定字符转换

    #include <stdio.h> #include <stdlib.h> #include<string.h> int main() { ]; int i; w ...

  6. wchar_t 、UTF-8、UTF-16的转换方法 - luketty的专栏 - 博客频道 - CSDN.NET

    wchar_t .UTF-8.UTF-16的转换方法 - luketty的专栏 - 博客频道 - CSDN.NET wchar_t .UTF-8.UTF-16的转换方法

  7. Struts2 四、Struts2 处理流程

    1. 一个请求在Struts2框架中的处理步骤: a) 客户端初始化一个指向Servlet容器的请求: b) 根据Web.xml配置,请求首先经过ActionContextCleanUp过滤器,其为可 ...

  8. 2016.09.01 html5兼容

    <!--[if lt IE 9]>  <script src="http://apps.bdimg.com/libs/html5shiv/3.7/html5shiv.min ...

  9. iOS7.0中UILabel高度调整注意事项(转)

    注释:原文链接丢失. 我的“记词助手”在升级到iOS7之后,一直出现UILabel错位的问题: 我的label是用- (CGSize)sizeWithFont:(UIFont *)font const ...

  10. ubuntu系统下设置静态IP

    改动 /etc/network/interfaces文件 加入下面内容 # The loopback network interface auto lo eth0 iface lo inet loop ...