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. 淘宝小练习#css

    * { margin: 0; padding: 0; } a { text-decoration: none; } .box { background: #f4f4f4; } /* 头部样式STAR ...

  2. abp(net core)+easyui+efcore实现仓储管理系统——ABP WebAPI与EasyUI结合增删改查之一(二十七)

    abp(net core)+easyui+efcore实现仓储管理系统目录 abp(net core)+easyui+efcore实现仓储管理系统——ABP总体介绍(一) abp(net core)+ ...

  3. flume安装和介绍

    一.安装1.flume-ng-1.5.0-cdh5.3.6.tar.gz 下载链接:https://pan.baidu.com/s/1AWPGP2qnY6_VpYr_iSnJ3w 密码:tiog2.对 ...

  4. opencv 6 图像轮廓与图像分割修复 3 图像的矩,分水岭,图像修补

    图像的矩 矩的计算:moments()函数 计算轮廓面积:contourArea()函数 #include "opencv2/highgui/highgui.hpp" #inclu ...

  5. 关于手机微信端ios的input不能选中问题解决方案

    最近在做一个微信端的商城,以前做web端的比较多,手机端做的相对来说要少点,老板说让我用俗称”靠谱的移动前端框架”—-AUI来搭建项目. 当时觉得用不用框架无所谓啦.结果后来写到一半把项目发布到手机上 ...

  6. 洛谷P2634 聪聪可可 (点分治)

    ###题目链接### 题目大意: 给你一棵树,假如树上两点间的距离是 3 的倍数 的点对有 s 对,则输出最简分数  s/n ,其中 n 表示所有整棵树的点对总数. 分析: 1.显然,可以采用点分治. ...

  7. WebGL简易教程(十三):帧缓存对象(离屏渲染)

    目录 1. 概述 2. 示例 2.1. 着色器部分 2.2. 初始化/准备工作 2.2.1. 着色器切换 2.2.2. 帧缓冲区 2.3. 绘制函数 2.3.1. 初始化顶点数组 2.3.2. 传递非 ...

  8. 为什么坚果云Markdown值得使用?

    值得使用说明是被认为有价值的,值得有两种意思的解释:1.有价值,有意义:2.价钱合适,合算:那么坚果云Markdown是否是人们值得使用的呢?下面就来看看坚果云Markdown到底是什么?为什么值得使 ...

  9. Airtest介绍与脚本入门

    前言 通过阅读本小节教程,你将了解以下内容: 一个Airtest脚本例子的详细解析 如何在Python脚本中调用Airtest接口 图片语句的参数介绍 Airtest介绍 Airtest是一款基于Py ...

  10. 【Java基础】字面量相加的类型转换

    Java字面量的相加类型转换 1.Java 编译期间(javac),凡是字面量和常量的运算,都会先运算出结果 2.运行期当字符串池中有 String"字面量"时,Java 会直接用 ...