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.

Example

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

题目大意:给出一个括号序列,要求替换若干括号使原括号变成正则括号序列(即合法的匹配序列)。替换只能将左括号替换成不同类型的左括号,右括号替换成不同类型的右括号。问最少的替换个数,如果不存在这样的替换则输出 Impossible

题解:先打表,把情况列出来,然后用数组模拟栈操作

AC代码为:

#include<stdio.h>
#include<string.h>
#include<stdbool.h>
#define max 1000005 int top=0;
char str[max]; int judge(char s1,char s2)
{
if(s1=='[' && s2==']')
return 1;
else if(s1=='{' && s2=='}')
return 1;
else if(s1=='(' && s2==')')
return 1;
else if(s1=='<' && s2=='>')
return 1;
else if((s1=='<'||s1=='['||s1=='{'||s1=='(')&&(s2=='>'||s2==']'||s2=='}'||s2==')'))
return 2;
else
return 0;
} int main()
{
char ch;
int num=0;
scanf("%c",&ch);
str[top++]=ch;
while(scanf("%c",&ch)!=EOF && ch!='\n')
{
str[top++]=ch;
if(judge(str[top-2],str[top-1])==1)
{
top-=2;
}
else if(judge(str[top-2],str[top-1])==2)
{
top-=2;
num++;
}
}
if(top)
printf("Impossible\n");
else
printf("%d\n",num);
return 0; }

Codeforce612C的更多相关文章

随机推荐

  1. spark集群搭建(三台虚拟机)——spark集群搭建(5)

    !!!该系列使用三台虚拟机搭建一个完整的spark集群,集群环境如下: virtualBox5.2.Ubuntu14.04.securecrt7.3.6_x64英文版(连接虚拟机) jdk1.7.0. ...

  2. python的模块future用法实例解析

    计算机的知识太多了,很多东西就是一个使用过程中详细积累的过程.最近遇到了一个很久关于future的问题,踩了坑,这里就做个笔记,免得后续再犯类似错误.   future的作用:把下一个新版本的特性导入 ...

  3. JavaWeb核心知识点

    一:HTTP协议     一.概述 1. 概念:超文本传输协议 2. 作用:规范了客户端(浏览器)和服务器的数据交互格式 3. 特点 1. 简单快速:客户端向服务器请求服务时,仅通过键值对来传输请求方 ...

  4. 更新centos7的kernel

    现在安装的centos7 的内核是3.10的, 机器已经联网,可以直接利用包管理工具更新,需要注意的是现在3.0以上的内核引入了签名机制,需要导入签名的key,参考步骤如下: 1.导入keyrpm - ...

  5. 万恶之源-python基本数据类型

    万恶之源-基本数据类型(dict) 本节主要内容: 字典的简单介绍 字典增删改查和其他操作 3. 字典的嵌套 ⼀一. 字典的简单介绍 字典(dict)是python中唯⼀一的⼀一个映射类型.他是以{ ...

  6. PostGIS 递归方法

    在Oracle数据库中,有可以实现递归的函数 select * from table_name start with [condition1] connect by [condition2] 最近发现 ...

  7. Springboot操作Elasticsearch

    常见的日志系统是基于logstach+elasticsearch+kibna框架搭建的,但是有时候kibana的查询无法满足我们的要求,因此有时需要代码去操作es,本文后续都以es代替elastics ...

  8. 2019-11-27:kali 2019-4中文乱码解决方法

    1.更换阿里源 vim /etc/apt/soul,编辑源之后,apt-get updata && apt-get upgrade && apt-get clean , ...

  9. 2019-9-9:渗透测试,基础学习,pydictor使用,sql盲注,docker使用,笔记

    pydictor,强大的密码生成工具,可以合并密码字典,词频统计,去重,枚举数字字典生成字典python3 pydictor.py -base d --len 4 4 生成纯数字4位密码python3 ...

  10. c#关于数据和方法在不同类中的引用-xdd

    关于数据和方法在不同类中的引用 using System; using System.Collections.Generic; using System.Linq; using System.Text ...