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. poj中一些对我来说不错的东西(每天不同的加入,要保持)

    1.关于深度搜索与暴力结合的棋盘翻转问题 poj1753::2965:: 2.贪心算法:2109,2586: 3.韩信点兵问题:poj1006

  2. 自制DTU

        最近打算自己做一款工业级DTU产品,预计到今年年底前完成,博客会随时更新产品进度,有兴趣的朋友可以联系我.

  3. 进程占用百分百CPU不卡(从未试过,当别的程序运行的时候,当前程序还会运行吗?)

    在写程序中.为了让程序效率高.有时会点用很高的CPU.这里用户体验不好可以设置线程的优先级来搞定. BOOL SetThreadPriority( HANDLE hThread, // handle ...

  4. ecshop的小总结

    1 为防止非法调用自己的页面,在被包含页面加上: if (!defined('IN_ECS'))  {die('Hacking attempt'); } 2 在主动包含页面要定义define('IN_ ...

  5. Hibernate、乐观锁和悲观锁

    悲观锁(Pessimistic Lock), 顾名思义,就是很悲观,每次去拿数据的时候都认为别人会修改,所以每次在拿数据的时候都会上锁,这样别人想拿这个数据就会block直到它拿到锁.传统的关系型数据 ...

  6. iOS获取本地ip(基本通用)

    今天有个朋友问我怎样訪问手机ip,上网找了几个,用了近200多行代码,最后发现头文件用的居然还是Linux中的,OC没有这个头文件.感觉socket本身应该能够后去自己的ip就试了一下,果然7.8行代 ...

  7. 关于Resharper的使用经验

    发现Resharper这东西真的有点累赘,重构也是,一开始用会有很多莫名其妙的提示.现在的项目用了Resharper,js是很方便,有定位功能,但连TypeScript的js都有了.

  8. AngularJs 实例

    1.AngularJs 表单验证: 示例 .controller('signupController', ['$scope', function($scope) { $scope.submitted ...

  9. SqlServer之触发器

    1.触发器之理论: 触发器(Trigger)是一种特殊类型的存储过程,是在用户对某一种表的数据进行UPDATE.INSERT 和 DELETE 操作时被触发执行的一段程序.触发器有助于强制引用完整性, ...

  10. 指针直接赋值为整型AND利用宏定义求结构体成员偏移量

    首先我们要更正一个很熟悉的概念,那就是指针不仅仅是“地址”,指针还有一个很重要的特性,那就是“类型”. 指针初始化时,“=”的右操作数; 除外,该语句表示指针为空): 所以 ; 这样的代码是不允许的. ...