Replace To Make Regular Bracket Sequence
Replace To Make Regular Bracket Sequence
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.
Examples
[<}){}
2
{()}[]
0
]]
Impossible 题意:输入一行括号序列,左边括号可以和右括号抵消。右括号可以转换为其他类型右括号进行成对抵消。输出需要转换的个数。如果无法抵消输出"Impossible"
#include <iostream>
#include <stack>
using namespace std; int main(){
string input;
while(cin >> input){
int count = ;
stack<char> s;
s.push(input[]); //[
for(int i = ;i < input.length();i++){//<}){}
if(s.size() > && (s.top() == '[' || s.top() == '<' || s.top() == '(' || s.top() == '{')){
if(input[i] == ']' || input[i] == '>' || input[i] == ')' || input[i] == '}'){
if(!((s.top() == '[' && input[i] == ']') ||
(s.top() == '<' && input[i] == '>') ||
(s.top() == '(' && input[i] == ')') ||
(s.top() == '{' && input[i] == '}'))){
count++;
}
s.pop();
}else{
s.push(input[i]);
}
}else{
s.push(input[i]);
}
}
if(s.size() != ){
cout << "Impossible" << endl;
}else{
cout << count << endl;
}
}
}
Replace To Make Regular Bracket Sequence的更多相关文章
- 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 ...
- CodeForces - 612C Replace To Make Regular Bracket Sequence 压栈
C. Replace To Make Regular Bracket Sequence time limit per test 1 second memory limit per test 256 m ...
- 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,每个开都要有一个和它对应的关,如:<()> ...
- CF 612C. Replace To Make Regular Bracket Sequence【括号匹配】
[链接]:CF [题意]:给你一个只含有括号的字符串,你可以将一种类型的左括号改成另外一种类型,右括号改成另外一种右括号 问你最少修改多少次,才能使得这个字符串匹配,输出次数 [分析]: 本题用到了栈 ...
- 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 ...
- CF1095E Almost Regular Bracket Sequence
题目地址:CF1095E Almost Regular Bracket Sequence 真的是尬,Div.3都没AK,难受QWQ 就死在这道水题上(水题都切不了,我太菜了) 看了题解,发现题解有错, ...
- (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 ...
随机推荐
- wordpress中安装插件需要ftp服务
曾经天真的以为要装个ftp,然后发现这只是wordpress为了安全而留下的坑,只要修改wordpress配置即可 wordpress安装目录 ==> wp-config.php,文件添加以下代 ...
- jQuery 双击事件(dblclick)
在jQuery的事件绑定中,执行双击事件(dblclick)时能触发两次单击事件(click).即一个标签元素(如div等),如果元素同时绑定了单击事件(click)和双击事件(dblclick),那 ...
- related_name
定义表Apple: class Apple( models.Model): origin_level = models.ForeignKey(AppleLevel) new_level = model ...
- SHA-256算法和区块链原理初探
组内技术分享的内容,目前网上相关资料很多,但读起来都不太合自己的习惯,于是自己整理并编写一篇简洁并便于(自己)理解和分享的文章. 因为之前对密码学没有专门研究,自己的体会或理解会特别标注为" ...
- [Flutter] Windows/MacOS Flutter 环境走一遍
Windows Install 1.系统需要:> win7 > 400M磁盘空间 Windows PowerShell(Windows 搜索框中找) Git for Windows 2.x ...
- [Linux] umask 从三类人群的权限中拿走权限数字
作用 umask 用来设置用户创建文件.目录的默认权限,通过从权限中拿走相应的位,格式 `umask nnn`. 理解 rwx rwx rwx 权限对应三类人群,所属人,所属组,其 ...
- springboot项目创建
1.在eclipse中创建springboot项目,右键找到New,然后找到Spring Starter Project, 如果menu中找不到Spring Starter Project就选择oth ...
- linux 之网络命令
write 给用户发消息,用户必须在线,以ctrl+d保存结束 语法: write 用户 wall发广播信息(write all) 语法:wall 信息 mail 查看发送电子邮件 发送语法 : m ...
- 逻辑读为何消耗CPU?
在数据库系统中,经常会看到这个说法:“逻辑读很消耗CPU”,然后开始把这句话当作一个定理来使用.但是为什么“同样是读,为什么逻辑读会使用那么多CPU?” 查了一些资料,配合自己的理解,有下面几点体会: ...
- 吴裕雄 python 机器学习——线性回归模型
import numpy as np from sklearn import datasets,linear_model from sklearn.model_selection import tra ...