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. XRPictureBox z

    XRPictureBox 大小加入是40x40  我绑定的图片好比是60X50 , 在不自己写代码的情况下,XRPictureBox 有没有什么属性可以调整,比如像SizeMode那种? // Set ...

  2. IRequiresSessionState和IReadOnlySessionState应用上的一些差异

    在调用ashx时,如果需要应用Session,则必须继承接口 IRequiresSessionState,IReadOnlySessionState,但根据字面,可以知道 IRequiresSessi ...

  3. Drupal如何更新注册表?

    Drupal的注册表是指registry和registry_file两个数据表.前一个表保存所有可用的类和接口以及它们所对应的文件,后一个表保存每个文件的hash码. 1. 将所有需要更新的文件都汇总 ...

  4. Thread .join 的用法一例

    在使用身份证读卡器时,要求 1. 身份证读到身份证 就 停止线程. 2. 关闭界面时会 自动停止调用读身份证的线程.这时候就需要用到 Thead.join 例子如下: Thread thread; p ...

  5. 让pre标签自动换行示例代码

    pre 元素可定义预格式化的文本.被包围在 pre 元素中的文本通常会保留空格和换行符.而文本也会呈现为等宽字体. <pre> 标签的一个常见应用就是用来表示计算机的源代码.对于技术博客经 ...

  6. div错位解决IE6、IE7、IE8样式不兼容问题

    IE6里DIV错位的问题       采用”FLOAT:LEFT“的DIV在IE8.IE7.都没问题,IE6下却向下移动,出现空白.这是因为,IE6采用的内核默认把DIV之间的距离增加了3~5个PX, ...

  7. C#缓存的一点想法及测试

    项目开发中,用到了缓存,其中的一个列表项,可能要多线程处理,就有了下面的想法,具体的问题在代码中有详细说明,见下文. static void Main(string[] args) { 测试缓存的想法 ...

  8. Mysql字符串截取函数SUBSTRING的用法说明

    感觉上MySQL的字符串函数截取字符,比用程序截取(如PHP或JAVA)来得强大,所以在这里做一个记录,希望对大家有用. 函数: 1.从左开始截取字符串 left(str, length) 说明:le ...

  9. Linux下IP的配置

    修改ip地址1.即时生效:# ifconfig eth0 192.168.1.102 netmask 255.255.255.02.启动生效:修改/etc/sysconfig/network-scri ...

  10. 想要完全导入swc中的所有类

    其实还有个这个办法.修改参数.-include-libraries library [...] 强制导入SWC文件中的所有类,不管是否曾使用过这个类 (((比如把swc文件放到src里,然后编译参数加 ...