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. 使用websocketpp进行websocket通信

    websocketpp介绍 websocketpp是一个只有头文件的支持websocket协议的C++开源库,支持websocket客户端和服务器功能,网络传输模块基于boost::asio 提供 s ...

  2. 二叉搜索树BST(C语言实现可用)

    1:概述 搜索树是一种可以进行插入,搜索,删除等操作的数据结构,可以用作字典或优先级队列.二叉搜索树是最简单的搜索树.其左子树的键值<=根节点的键值,右子树的键值>=根节点的键值. 如果共 ...

  3. 关于jQuery easyUI 添加合计统计行

    首先在onLoadSuccess中添加计算函数:计算方法按各自业务需要,我做了一个判断非数 然后再在gatagrid表格添加行,$('#div').datagrid('appendRow', {... ...

  4. 分组取topN

    假设有这样一个文件,文件内容如下 class1 class2 class1 class1 class2 class2 class1 class2 class1 class2 要求按照班级分组取出每个班 ...

  5. 领扣(LeetCode)二叉树的所有路径 个人题解

    给定一个二叉树,返回所有从根节点到叶子节点的路径. 说明: 叶子节点是指没有子节点的节点. 示例: 输入: 1 / \ 2 3 \ 5 输出: ["1->2->5", ...

  6. SpringBoot 配置文件与依赖库分离打包配置

    一.应用场景 一般情况下我们对springboot应用打包时使用springboot的maven插件spring-boot-maven-plugin的maven进行打包,打包完成得到一个fatjar, ...

  7. SIGCHLD waitpid, 小心子进程结束事件被偷了

    原本项目中依赖子进程执行的地方,都使用jni调用java层的ProcessManager,换了c++ACE框架后,发现这些任务都很慢,调试才发现所有子进程执行的任务都必须等待到reactor超时才返回 ...

  8. Springboot操作Elasticsearch

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

  9. 【Luogu P3375】字符串匹配KMP算法模板

    Luogu P3375 模式串:即题目中的S2所代表的意义 文本串:即题目中的S1所代表的意义 对于字符串匹配,有一种很显然的朴素算法:在S1中枚举起点一位一位匹配,失配之后起点往后移动一位,从头开始 ...

  10. telnet指令研究—以网络聊天程序为例

    一.telnet指令 Telnet取名自Telecommunications和Networks的联合缩写,是早期个人计算机上连接到服务器主机的一个网络指令,由于存在安全问题,现在已经很少被使用.在wi ...