C. Replace To Make Regular Bracket Sequence

题目连接:

http://www.codeforces.com/contest/612/problem/C

Description

You are given string s consists of opening and closing brackets of four kinds <>, {}, [], (). There are two types of brackets: opening and closing. You can replace any bracket by another of the same type. For example, you can replace < by the bracket {, but you can't replace it by ) or >.

The following definition of a regular bracket sequence is well-known, so you can be familiar with it.

Let's define a regular bracket sequence (RBS). Empty string is RBS. Let s1 and s2 be a RBS then the strings s2, {s1}s2, [s1]s2, (s1)s2 are also RBS.

For example the string "[[(){}]<>]" is RBS, but the strings "[)()" and "][()()" are not.

Determine the least number of replaces to make the string s RBS.

Input

The only line contains a non empty string s, consisting of only opening and closing brackets of four kinds. The length of s does not exceed 106.

Output

If it's impossible to get RBS from s print Impossible.

Otherwise print the least number of replaces needed to get RBS from s.

Sample Input

[<}){}

Sample Output

2

Hint

题意

给你一个只含有括号的字符串,你可以将一种类型的左括号改成另外一种类型,右括号改成另外一种右括号

问你最少修改多少次,才能使得这个字符串匹配,输出次数

题解:

用stack,每次将左括号压进stack里面,遇到右括号就判断一下就好了

非法就很简单,看看栈最后是否还有,看看右括号的时候,左括号的栈是否为空

代码

#include<bits/stdc++.h>
using namespace std; string s;
stack<char> S;
int main()
{
cin>>s;
int ans = 0;
for(int i=0;i<s.size();i++)
{ if(s[i]==']')
{
if(S.size()==0)return puts("Impossible");
if(S.top()=='[')
S.pop();
else
{
ans++;
S.pop();
}
}
else if(s[i]==')')
{
if(S.size()==0)return puts("Impossible");
if(S.top()=='(')
S.pop();
else
{
ans++;
S.pop();
}
} else if(s[i]=='>')
{
if(S.size()==0)return puts("Impossible");
if(S.top()=='<')
S.pop();
else
{
ans++;
S.pop();
}
}
else if(s[i]=='}')
{
if(S.size()==0)return puts("Impossible");
if(S.top()=='{')
S.pop();
else
{
ans++;
S.pop();
}
}
else S.push(s[i]);
}
if(S.size()!=0)return puts("Impossible");
cout<<ans<<endl;
}

Educational Codeforces Round 4 C. Replace To Make Regular Bracket Sequence 栈的更多相关文章

  1. Educational Codeforces Round 4 C. Replace To Make Regular Bracket Sequence

    题目链接:http://codeforces.com/contest/612/problem/C 解题思路: 题意就是要求判断这个序列是否为RBS,每个开都要有一个和它对应的关,如:<()> ...

  2. Codeforces Round #529 (Div. 3) E. Almost Regular Bracket Sequence (思维)

    Codeforces Round #529 (Div. 3) 题目传送门 题意: 给你由左右括号组成的字符串,问你有多少处括号翻转过来是合法的序列 思路: 这么考虑: 如果是左括号 1)整个序列左括号 ...

  3. Codeforces Round #529 (Div. 3) E. Almost Regular Bracket Sequence(思维)

    传送门 题意: 给你一个只包含 '(' 和 ')' 的长度为 n 字符序列s: 给出一个操作:将第 i 个位置的字符反转('(' ')' 互换): 问有多少位置反转后,可以使得字符串 s 变为&quo ...

  4. Codeforces Round #529 (Div. 3) E. Almost Regular Bracket Sequence (思维,模拟栈)

    题意:给你一串括号,每次仅可以修改一个位置,问有多少位置仅修改一次后所有括号合法. 题解:我们用栈来将这串括号进行匹配,每成功匹配一对就将它们消去,因为题目要求仅修改一处使得所有括号合法,所以栈中最后 ...

  5. CodeForces - 612C Replace To Make Regular Bracket Sequence 压栈

    C. Replace To Make Regular Bracket Sequence time limit per test 1 second memory limit per test 256 m ...

  6. Replace To Make Regular Bracket Sequence

    Replace To Make Regular Bracket Sequence You are given string s consists of opening and closing brac ...

  7. Codeforces Beta Round #5 C. Longest Regular Bracket Sequence 栈/dp

    C. Longest Regular Bracket Sequence Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.c ...

  8. D - Replace To Make Regular Bracket Sequence

    You are given string s consists of opening and closing brackets of four kinds <>, {}, [], (). ...

  9. CF 612C. Replace To Make Regular Bracket Sequence【括号匹配】

    [链接]:CF [题意]:给你一个只含有括号的字符串,你可以将一种类型的左括号改成另外一种类型,右括号改成另外一种右括号 问你最少修改多少次,才能使得这个字符串匹配,输出次数 [分析]: 本题用到了栈 ...

随机推荐

  1. Php 笔记2-----手机端 与 php服务器的通信

    对于 手机端 和 php服务器的通信,是不存在表单这一概念的  ,除非自己去实现, 所以通常情况下步骤是: 假定上传的是字符串. 1  手机端的流程是 把文件或者字符串,转化为 特定的流. 2 通过h ...

  2. 指定URL,计算文件大小

    将http://www.baidu.com替换成指定的URL,就可以获得文件的大小. 文件大小已经转换为以KB为单位. url对象用openconnection()打开连接:获得URLConnecti ...

  3. 【转】requirejs简单入门

    博主今天正式工作啦,工作中用到了js模块化技术,这里转来一个入门教程,很易懂,转给同样刚入门的你们~~ 原地址:http://www.ruanyifeng.com/blog/2012/11/requi ...

  4. searchBar 隐藏

    searchBar 隐藏 CGRect newBounds = self.tableView.bounds; newBounds.origin.y = newBounds.origin.y + _he ...

  5. 编辑器CocoStudio和CocosBuilder的对比

    来源:http://4137613.blog.51cto.com/4127613/1352805   CocosBuilder CocoStudio 控件种类 支持大部分cocos2d-x自带的常用控 ...

  6. 轻松学习Linux之自动执行任务

    在 Linux 中,任务可以被配置在指定的时间段.指定的日期.或系统平均载量低于指定的数量时自动运行,系统管理员可使用自动化的任务来执行定期备份.监控系统.运行定制脚本等,往往初学者都是win用户,习 ...

  7. 第二百三十五天 how can I 坚持

    其实昨天听遗憾的,尽头看了新闻,有好多人都出去赏雪了,可惜了,最遗憾的是没有叫上你一块去. 晚上喝了点酒,抽了两根烟,以前基本不喝酒,就别提抽烟了,陈小春的<算你狠>,该如何是好. 经常在 ...

  8. cocos2dx移植android平台-我的血泪史

    版权声明:本文由( 小塔 )原创,转载请保留文章出处! 本文链接:http://www.zaojiahua.com/android-platform.html 本人这几天一直都没有跟新自己的网站内容, ...

  9. jy

    222 DROP TABLE t_vhl_jy_car; CREATE TABLE t_vhl_jy_car( VEHICLE_JY_CODE ) PRIMARY KEY, VEHICLE_CODE ...

  10. linux极点五笔无法输入词组_ibus设置

    菜鸟学linux——用的是ubuntu 不知道是不是按个哪些快捷键,极点五笔突然无法输入词组.那个抓狂啊 没关系,设置一下就ok 第一步:右上角输入法,右键——>首选项——>常规——> ...