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的更多相关文章
随机推荐
- 【algo&ds】2.线性表
1.线性表 线性表(英语:Linear List)是由n(n≥0)个数据元素(结点)a[0],a[1],a[2]-,a[n-1]组成的有限序列. 其中: 数据元素的个数n定义为表的长度 = " ...
- Docker学习-Kubernetes - 集群部署
Docker学习 Docker学习-VMware Workstation 本地多台虚拟机互通,主机网络互通搭建 Docker学习-Docker搭建Consul集群 Docker学习-简单的私有Dock ...
- Java基础语法07-面向对象-多态
抽象类 抽象方法 : 没有方法体的方法. 抽象类:被abstract所修饰的类. 抽象类的语法格式: [权限修饰符] abstract class 类名{ }[权限修饰符] abstract clas ...
- 在react中配置less
在创建项目之后执行 $ yarn eject 抽离配置文件 会多出config和script文件夹 接下来安装less yarn add less less-loader 或者 npm install ...
- sparkSQL 简介
一.Spark SQL的特点 1.支持多种数据源:Hive.RDD.Parquet.JSON.JDBC等.2.多种性能优化技术:in-memory columnar storage.byte-code ...
- secureCRT连接虚拟机
1.secureCRT英文版下载 链接:https://pan.baidu.com/s/1LFWD-k2r4ZB7DHQA66QogQ 密码:khmo 破解方式参考 2.虚拟机静态IP设置 参考 3. ...
- 自动安装 linux 系统
实现自动安装 centos 6 和 centos 7 实现自动安装 Linux 系统需要在虚拟机上安装三个服务:apache .tftp.dhcp 三个服务放在一台虚拟机上即可 一.DHCP 服务器的 ...
- 从静态代理,jdk动态代理到cglib动态代理-一文搞懂代理模式
从代理模式到动态代理 代理模式是一种理论上非常简单,但是各种地方的实现往往却非常复杂.本文将从代理模式的基本概念出发,探讨代理模式在java领域的应用与实现.读完本文你将get到以下几点: 为什么需要 ...
- Vue学习之Vue模拟后台数据
前后端项目的开发,需要前端和后端同时进行开发,有时后端开发人员还未完成API接口可以提供给前端调用,因此就需要模拟前端请求后端接口,后端返回数据.一般开发中都是返回json数据格式来完成前后端的交互. ...
- 学习记录:《C++设计模式——李建忠主讲》4.“单一职责”模式
单一职责模式:在软件组件的设计中,如果责任划分的不清晰,使用继承得到的结果往往是随着需求的变化,子类急剧膨胀,同时充斥着重复代码,这时候的关键是划清责任. 典型模式:装饰模式(Decorator).桥 ...