Codeforce612C
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
[<}){}
2
{()}[]
0
]]
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的更多相关文章
随机推荐
- 对于 TCP 三次握手的理解
假设名叫 A 和 B 的两个人要进行通信,那么他们两人之间,首先要确保通信顺畅. 而确保通信顺畅,就要从 3 个维度,确定 8 个能力 3 个维度分别是: 1.人知道(A 知道.B 知道) 2.人(A ...
- 【algo&ds】3.栈和队列
1.堆栈 堆栈(Stack):具有一定操作约束的线性表(只在一端(栈顶,Top)做插入.删除) 先进后出特性 1.1堆栈的抽象数据类型描述 类型名称: 堆栈(Stack) 数据对象集:一个有0个或多个 ...
- PowerMock学习(四)之Mock static的使用
我们编写代码的时候,总会写一些工具类,为了方便调用喜欢使用static关键字来修饰对应方法. 那么现在举例说明,还是准备两个接口,第一个是查询学生总数,第二个是新增学生两个接口,具体示例代码如下: p ...
- 使用Charles设置https代理到http以及证书安装(服务端篇)
1.下载ssl证书到[登录],并且设置证书[始终信任] 2.SSL Proxying设置,Location为*,可以抓全部接口的https请求 参考:https://www.jianshu.com/p ...
- 深入理解跳表在Redis中的应用
本文首发于:深入理解跳表在Redis中的应用微信公众号:后端技术指南针持续输出干货 欢迎关注 前面写了一篇关于跳表基本原理和特性的文章,本次继续介绍跳表的概率平衡和工程实现, 跳表在Redis.Lev ...
- 使用node.js开发一个生成逐帧动画小工具
在实际工作中我们已经下下来不下于一万个npm包了,像我们熟悉的 vue-cli,react-native-cli 等,只需要输入简单的命令 vue init webpack project,即可快速帮 ...
- 我的书籍《深入解析Java编译器:源码剖析与实例详解》就要出版了
一个十足的技术迷,2013年毕业,做过ERP.游戏.计算广告,在大公司呆过,但终究不满足仅对技术的应用,在2018年末离开了公司,全职写了一本书<深入解析Java编译器:源码剖析与实例详解> ...
- Hash Map 在java中的解释及示例
目录 HashMap在java中的应用及示例 HashMap的内部结构 HashMap的性能 同步HashMap HashMap的构造函数 HashMap的时间复杂度 HashMap的方法 1. vo ...
- RAM、ROM和fFLASH相关概念整理
一:ROM ROM:Read Only Memory.只读存储器 是一种半导体内存,又叫做非挥发性内存.其特性是一旦数据被存储就无法再将之改变或删除.存储的数据不会因为电源关闭而消失. 二: ...
- es6 map的用法
let arr =[ {title:'aaaa',read:100,hot:true}, {title:'bbbb',read:50,hot:false}, {title:'ccc',read:100 ...