Replace To Make Regular Bracket Sequence

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 <s1>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.

Examples

Input
[<}){}
Output
2
Input
{()}[]
Output
0
Input
]]
Output
Impossible

题意:输入一行括号序列,左边括号可以和右括号抵消。右括号可以转换为其他类型右括号进行成对抵消。输出需要转换的个数。如果无法抵消输出"Impossible"
 #include <iostream>
#include <stack>
using namespace std; int main(){
string input;
while(cin >> input){
int count = ;
stack<char> s;
s.push(input[]); //[
for(int i = ;i < input.length();i++){//<}){}
if(s.size() > && (s.top() == '[' || s.top() == '<' || s.top() == '(' || s.top() == '{')){
if(input[i] == ']' || input[i] == '>' || input[i] == ')' || input[i] == '}'){
if(!((s.top() == '[' && input[i] == ']') ||
(s.top() == '<' && input[i] == '>') ||
(s.top() == '(' && input[i] == ')') ||
(s.top() == '{' && input[i] == '}'))){
count++;
}
s.pop();
}else{
s.push(input[i]);
}
}else{
s.push(input[i]);
}
}
if(s.size() != ){
cout << "Impossible" << endl;
}else{
cout << count << endl;
}
}
}

Replace To Make Regular Bracket Sequence的更多相关文章

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

    C. Replace To Make Regular Bracket Sequence 题目连接: http://www.codeforces.com/contest/612/problem/C De ...

  2. 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 ...

  3. D - Replace To Make Regular Bracket Sequence

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

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

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

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

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

  6. 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 ...

  7. CF1095E Almost Regular Bracket Sequence

    题目地址:CF1095E Almost Regular Bracket Sequence 真的是尬,Div.3都没AK,难受QWQ 就死在这道水题上(水题都切不了,我太菜了) 看了题解,发现题解有错, ...

  8. (CodeForces - 5C)Longest Regular Bracket Sequence(dp+栈)(最长连续括号模板)

    (CodeForces - 5C)Longest Regular Bracket Sequence time limit per test:2 seconds memory limit per tes ...

  9. 贪心+stack Codeforces Beta Round #5 C. Longest Regular Bracket Sequence

    题目传送门 /* 题意:求最长括号匹配的长度和它的个数 贪心+stack:用栈存放最近的左括号的位置,若是有右括号匹配,则记录它们的长度,更新最大值,可以在O (n)解决 详细解释:http://bl ...

随机推荐

  1. Oracle表空间不足;查询表空间使用率(unable to extend lob segment SYS_LOB0000076749C00006$$ by 8192 in tablespace USERS)

    查询表空间对应地址 *),) total_space from dba_data_files order by tablespace_name; //方案一:修改表空间大小(32000可改为想要的数值 ...

  2. Android 开发 Activity里获取View的宽度和高度 转载

    原文地址:https://blog.csdn.net/chenbaige/article/details/77991594 前言: 可能很多情况下,我们都会有在activity中获取view 的尺寸大 ...

  3. Android 开发 Fresco框架点击小图显示全屏大图实现 ZoomableDraweeView

    目标需求 实现一张小图片,被点击后变成一个在整个屏幕上显示的大图片.类似于微信朋友圈的图片. 实现流程 1.Fresco基本初始化 2.下载并且导入ZoomableDraweeView 它是实现大图的 ...

  4. Linux修改hostname与免密码登录

    修改hostname [root@centos7 ~]$ hostnamectl set-hostname hadoop001 # 使用这个命令会立即生效且重启也生效 [root@centos7 ~] ...

  5. Struts2高位漏洞升级到struts2.3.32

    Struts2高位漏洞升级到struts2.3.32 3月7日带来了一个高危漏洞Struts2漏洞——CVE编号CVE-2017-5638.其原因是由于Apache Struts2的Jakarta M ...

  6. Docker 开启 remoter api

    debian 环境: vi /lib/systemd/system/dcoker.service 修改: ExecStart=/usr/bin/dockerd -H fd://为 ExecStart= ...

  7. stm32 HAL库笔记(零)

    最近在设计四旋翼飞行器,用stm32f407,有三种开发方式可以选择:一.寄存器开发.二:库函数开发.三:HAL库开发,考虑了一下,选择了HAL库,原因如下: 1. 寄存器开发相对较慢,寄存器很多,配 ...

  8. 如何才能成为一个合格的web前端工程师

    转载原文地址:https://juejin.im/post/5cc1da82f265da036023b628 开篇前端开发是一个非常特殊的行业,它的历史实际上不是很长,但是知识之繁杂,技术迭代速度之快 ...

  9. v-for key

    key 当 Vue.js 用 v-for 正在更新已渲染过的元素列表时,它默认用“就地复用”策略.如果数据项的顺序被改变,Vue 将不会移动 DOM 元素来匹配数据项的顺序, 而是简单复用此处每个元素 ...

  10. 使用python备份文件

    想写个定时备份文件的功能,这个功能需要实现:1.搜索指定的目录里是否存在当天的文件2.如果存在压缩并加密文件3.通过ftp上传到备份服务器4.在备份服务器上定时将文件拷贝到移动硬盘并定时清理文件 本来 ...