C. Replace To Make Regular Bracket Sequence
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

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.

Input

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.

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.

Examples
Input
[<}){}
Output
2
Input
{()}[]
Output
0
Input
]]
Output
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 压栈的更多相关文章

  1. CF 612C. Replace To Make Regular Bracket Sequence【括号匹配】

    [链接]:CF [题意]:给你一个只含有括号的字符串,你可以将一种类型的左括号改成另外一种类型,右括号改成另外一种右括号 问你最少修改多少次,才能使得这个字符串匹配,输出次数 [分析]: 本题用到了栈 ...

  2. 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 ...

  3. Replace To Make Regular Bracket Sequence

    Replace To Make Regular Bracket Sequence You are given string s consists of opening and closing brac ...

  4. D - Replace To Make Regular Bracket Sequence

    You are given string s consists of opening and closing brackets of four kinds <>, {}, [], (). ...

  5. Educational Codeforces Round 4 C. Replace To Make Regular Bracket Sequence

    题目链接:http://codeforces.com/contest/612/problem/C 解题思路: 题意就是要求判断这个序列是否为RBS,每个开都要有一个和它对应的关,如:<()> ...

  6. 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 ...

  7. (CodeForces - 5C)Longest Regular Bracket Sequence(dp+栈)(最长连续括号模板)

    (CodeForces - 5C)Longest Regular Bracket Sequence time limit per test:2 seconds memory limit per tes ...

  8. 贪心+stack Codeforces Beta Round #5 C. Longest Regular Bracket Sequence

    题目传送门 /* 题意:求最长括号匹配的长度和它的个数 贪心+stack:用栈存放最近的左括号的位置,若是有右括号匹配,则记录它们的长度,更新最大值,可以在O (n)解决 详细解释:http://bl ...

  9. Almost Regular Bracket Sequence CodeForces - 1095E (线段树,单点更新,区间查询维护括号序列)

    Almost Regular Bracket Sequence CodeForces - 1095E You are given a bracket sequence ss consisting of ...

随机推荐

  1. [转载]Javascript 同步异步加载详解

    http://handyxuefeng.blog.163.com/blog/static/4545217220131125022640/ 本文总结一下浏览器在 javascript 的加载方式. 关键 ...

  2. 基本控件文档-UIView属性

    CHENYILONG Blog 基本控件文档-UIView属性 Fullscreen   UIView属性技术博客http://www.cnblogs.com/ChenYilong/ 新浪微博http ...

  3. jquery ajax complete 方法

    jquery ajax var ajaxTimeoutTest = $.ajax({ url:'',  //请求的URL timeout : 1000, //超时时间设置,单位毫秒 type : 'g ...

  4. 技术分享:如何在PowerShell脚本中嵌入EXE文件

    技术分享:如何在PowerShell脚本中嵌入EXE文件 我在尝试解决一个问题,即在客户端攻击中只使用纯 PowerShell 脚本作为攻击负荷.使用 PowerShell 运行恶意代码具有很多优点, ...

  5. 【Tomcat】Tomcat容器 web.xml详解

      Tomcat的安装目录下的conf目录下的web.xml文件有许多配置,例如: <init-param> <param-name>debug</param-name& ...

  6. 触发器Demo

    --mysql 触发器简单实例 --创建表1 )) ; --创建表2 )); --创建触发器,表一增加数据时,表二自动增加数据 create trigger t_afterinsert_on_tab1 ...

  7. nginx配置浅析

    一.nginx的介绍 nginx是由俄罗斯人开发的一款高性能的http和反向代理服务器,也可以用来作为邮件代理.相比较于其他的服务器,具有占用内存少,稳定性高等优势 二.nginx的配置 nginx的 ...

  8. imperva命令行查看流量值大小

    watch -d -n 1 /proc/hades/status echo clear > /proc/hades/status     //清除这些记录

  9. MVVM模式的命令绑定

    命令绑定要达到的效果 命令绑定要关注的核心就是两个方面的问题,命令能否执行和命令怎么执行.也就是说当View中的一个Button绑定了ViewModel中一个命令后,什么时候这个Button是可用的, ...

  10. js中字符串的常用方法

    一.普通方法 1.字符方法 动态方法:1.str.charAt(index);  返回子字符串,index为字符串下标,index取值范围[0,str.length-1] 动态方法:2.str.cha ...