CodeForces - 612C Replace To Make Regular Bracket Sequence 压栈
1 second
256 megabytes
standard input
standard output
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 ands2
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.
The only line contains a non empty string
s, consisting of only opening and closing brackets of four kinds. The length ofs does not exceed106.
If it's impossible to get RBS from
s print
Impossible.
Otherwise print the least number of replaces needed to get RBS from
s.
[<}){}
2
{()}[]
0
]]
Impossible
该题意思大概就是要形成正确的括号形式,左括号之间可以相互变换,右括号之间也可以相互变换,但左右括号之间不能相互变换。求出最小变换次数。 可以用STL中的stack解决,但没学过就用数组进行模拟压栈。
#include<stdio.h>
#include<string.h>
int main()
{
char a[1000005],b[1000000];
int n,j=0,sum=0,sum2=0;
scanf("%s",a);
n=strlen(a);
for(int i=0;i<n;i++)
{
b[j]=a[i];
if(sum2<0)
{
printf("Impossible\n");
return 0;
}
switch(a[i])//利用ASCII码进行判断,如果匹配则弹出
{
case '{':j++;sum2++;break;
case '[':j++;sum2++;break;
case '(':j++;sum2++;break;
case '<':j++;sum2++;break;
case '}':if(b[j-1]+2==a[i]){j--;}else{j--;sum++;}sum2--;break;
case ']':if(b[j-1]+2==a[i]){j--;}else{j--;sum++;}sum2--;break;
case ')':if(b[j-1]+1==a[i]){j--;}else{j--;sum++;}sum2--;break;
case '>':if(b[j-1]+2==a[i]){j--;}else{j--;sum++;}sum2--;break;
}
}
if(sum2!=0)
{
printf("Impossible\n");
return 0;
}
printf("%d\n",sum); return 0;
}
CodeForces - 612C Replace To Make Regular Bracket Sequence 压栈的更多相关文章
- CF 612C. Replace To Make Regular Bracket Sequence【括号匹配】
[链接]:CF [题意]:给你一个只含有括号的字符串,你可以将一种类型的左括号改成另外一种类型,右括号改成另外一种右括号 问你最少修改多少次,才能使得这个字符串匹配,输出次数 [分析]: 本题用到了栈 ...
- Educational Codeforces Round 4 C. Replace To Make Regular Bracket Sequence 栈
C. Replace To Make Regular Bracket Sequence 题目连接: http://www.codeforces.com/contest/612/problem/C De ...
- Replace To Make Regular Bracket Sequence
Replace To Make Regular Bracket Sequence You are given string s consists of opening and closing brac ...
- D - Replace To Make Regular Bracket Sequence
You are given string s consists of opening and closing brackets of four kinds <>, {}, [], (). ...
- Educational Codeforces Round 4 C. Replace To Make Regular Bracket Sequence
题目链接:http://codeforces.com/contest/612/problem/C 解题思路: 题意就是要求判断这个序列是否为RBS,每个开都要有一个和它对应的关,如:<()> ...
- Codeforces Beta Round #5 C. Longest Regular Bracket Sequence 栈/dp
C. Longest Regular Bracket Sequence Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.c ...
- (CodeForces - 5C)Longest Regular Bracket Sequence(dp+栈)(最长连续括号模板)
(CodeForces - 5C)Longest Regular Bracket Sequence time limit per test:2 seconds memory limit per tes ...
- 贪心+stack Codeforces Beta Round #5 C. Longest Regular Bracket Sequence
题目传送门 /* 题意:求最长括号匹配的长度和它的个数 贪心+stack:用栈存放最近的左括号的位置,若是有右括号匹配,则记录它们的长度,更新最大值,可以在O (n)解决 详细解释:http://bl ...
- Almost Regular Bracket Sequence CodeForces - 1095E (线段树,单点更新,区间查询维护括号序列)
Almost Regular Bracket Sequence CodeForces - 1095E You are given a bracket sequence ss consisting of ...
随机推荐
- CSS3 实现的一个简单的"动态主菜单" 示例[转]
其实这个示例蛮无聊的 很简单 也没什么实际的用处. 主要是展示了 CSS3 如何实现动画效果. 写这个主要是想看一看 完成这样的效果 我到底要写多少代码. 同时和我熟悉的java做个比较. 比较结果不 ...
- CSS只改变背景透明度,不改变子元素透明度
一般情况下,我们可以使用css的opcity属性改变某个元素的透明度,但是其元素下的子元素的透明度也会被改变,即使对子元素重新定义也没有用,例如: <div style="opacit ...
- Is It A Tree? 挂着并查集的帽子招摇撞骗
Description A tree is a well-known data structure that is either empty (null, void, nothing) or is a ...
- 【Python学习笔记】Jupyter Lab目录插件安装
Jupyter Lab目录插件安装 当然首先你得有python和已经安装了jupyter lab. 1 安装jupyter_contrib_nbextensions 首先先安装jupyter_cont ...
- nand flash 的oob 及坏块管理
0.NAND的操作管理方式 NAND FLASH的管理方式:以三星FLASH为例,一片Nand flash为一个设备(device),1 (Device) = xxxx (Blocks),1 ...
- 我看到的最棒的Twisted入门教程!
http://www.douban.com/note/232204441/ http://www.cnblogs.com/sevenyuan/archive/2010/11/18/1880681.ht ...
- linux系统时钟和硬件时钟不一致
在做DB2 集群复制的时候要求两台主机时间相互一致. 但是在一台主机上系统时间和硬件时间相差12个小时左右:手动同步后,重启后又相差12个小时左右. 为什么会是这样的,先介绍下系统时钟和硬件时钟的区别 ...
- local class incompatible: stream classdesc serialVersionUID = -2897844985684768944, local class serialVersionUID = 7350468743759137184
local class incompatible: stream classdesc serialVersionUID = 1, local class serialVersionUID = 2427 ...
- InnoDB锁问题
InnoDB锁问题 InnoDB与MyISAM的最大不同有两点:一是支持事务(TRANSACTION):二是采用了行级锁.行级锁与表级锁本来就有许多不同之处,另外,事务的引入也带来了一些新问题.下面我 ...
- java基础42 File类的构造函数及其方法
本文知识点(目录): 1.IO流(Input Output) 2.IO流技术的运用场景 3.File类的构造函数(方法) 4.File类的一些常用方法 5.实例(解析File类 ...