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.

Examples

Input

[<}){}

Output

2

Input

{()}[]

Output

0

Input

]]

Output

Impossible

意思是有左右两类括号,同类可以变形,求把所有括号消掉需要变形多少次,不可以消掉就输出impossible,加个例子{[}]这个是要两次;用stack栈

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include <iomanip>
#include<cmath>
#include<float.h>
#include<string.h>
#include<algorithm>
#define sf scanf
#define pf printf
#define mm(x,b) memset((x),(b),sizeof(x))
#include<vector>
#include<queue>
#include<stack>
#include<map>
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=a;i>=n;i--)
typedef long long ll;
typedef long double ld;
typedef double db;
const ll mod=1e9+100;
const db e=exp(1);
using namespace std;
const double pi=acos(-1.0);
char a[1000005];
int judge(int n)
{
stack<char>v;
int num1=0,num2=0,ans=0;
rep(i,0,n)
{
if(a[i]=='['||a[i]=='('||a[i]=='{'||a[i]=='<')
v.push(a[i]);
else
{
if(v.empty()) return -1;
switch(a[i])
{
case '>':if(v.top()!='<') ans++;break;
case ']':if(v.top()!='[') ans++; break;
case ')':if(v.top()!='(') ans++; break;
case '}':if(v.top()!='{') ans++; break;
}
v.pop();
}
}
if(!v.empty()) return -1;
return ans;
}
int main()
{
sf("%s",a);
int len=strlen(a);
if(len&1) { pf("Impossible"); return 0; }
if(judge(len)==-1) pf("Impossible");
else pf("%d",judge(len));
return 0;
}

D - 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. Replace To Make Regular Bracket Sequence

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

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

  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. 反向代理WebSocket连接自动断掉的问题

    Nginx可能设置了超时时间,导致WebSocket一会儿就断了 解决方法: 1.增加Nginx配置 proxy_read_timeout 500s; 注:三种超时时间,参见 https://www. ...

  2. [Canvas]Bombman v1.00

    爆破小人Canvas版,请点此下载,并用浏览器打开试玩. 图例: 源码: <!DOCTYPE html> <html lang="utf-8"> <m ...

  3. Web 安全 之 OpenSSL

    什么是OpenSSL协议? SSL(Secure SocketLayer,安全套接层)协议是使用最为普遍网站加密技术,用以保障在Internet上数据传输之安全,利用数据加密(Encryption)技 ...

  4. 使用AOF持久化文件实现还原Redis数据库并得到RDB持久化文件

    目录 1 编写本文的初衷 2 具体实施 2.1 Redis持久化概念简介 2.2 获取指定Redis的AOF持久化文件 2.3 把Redis的持久化AOF文件转换为RDB文件 1 编写本文的初衷 因为 ...

  5. Kubernetes的ConfigMap解析

    ConfigMap功能在Kubernetes1.2版本的时候就有了,许多应用程序会从配置文件.命令行参数或环境变量中读取配置信息.这些配置信息需要与docker image解耦,你总不能每修改一个配置 ...

  6. [MySQL]对于事务并发处理带来的问题,脏读、不可重复读、幻读的理解

    一.缘由 众所周知MySQL从5.5.8开始,Innodb就是默认的存储引擎,Innodb最大的特点是:支持事务.支持行级锁. 既然支持事务,那么就会有处理并发事务带来的问题:更新丢失.脏读.不可重复 ...

  7. django DateTimeField 时间格式化

    ['%Y-%m-%d %H:%M:%S', # '2006-10-25 14:30:59' '%Y-%m-%d %H:%M', # '2006-10-25 14:30' '%Y-%m-%d', # ' ...

  8. 修改ip导致服务不可用

    修改ip导致服务不可用 1.修改hostsvi /etc/hosts 修改ip地址 2.lsnrctl start 后会发现The listener supports no services,解决方案 ...

  9. JavaScript反向shell

    Node.js反向Shell 摘自:http://www.itfang.net/?p=109 如下的Javascript就是一个Node.js的反向连接shell. 这个payload将会生成一个/b ...

  10. 【iCore4 双核心板_FPGA】例程二:GPIO输入实验——识别按键输入

    实验现象: 按键每按下一次,三色LED切换一次状态. 核心源代码: module key_ctrl( input clk_25m, input rst_n, input key, output fpg ...