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
Description
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 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.
Sample Input
[<}){}
Sample Output
2
Hint
题意
给你一个只含有括号的字符串,你可以将一种类型的左括号改成另外一种类型,右括号改成另外一种右括号
问你最少修改多少次,才能使得这个字符串匹配,输出次数
题解:
用stack,每次将左括号压进stack里面,遇到右括号就判断一下就好了
非法就很简单,看看栈最后是否还有,看看右括号的时候,左括号的栈是否为空
代码
#include<bits/stdc++.h>
using namespace std;
string s;
stack<char> S;
int main()
{
cin>>s;
int ans = 0;
for(int i=0;i<s.size();i++)
{
if(s[i]==']')
{
if(S.size()==0)return puts("Impossible");
if(S.top()=='[')
S.pop();
else
{
ans++;
S.pop();
}
}
else if(s[i]==')')
{
if(S.size()==0)return puts("Impossible");
if(S.top()=='(')
S.pop();
else
{
ans++;
S.pop();
}
}
else if(s[i]=='>')
{
if(S.size()==0)return puts("Impossible");
if(S.top()=='<')
S.pop();
else
{
ans++;
S.pop();
}
}
else if(s[i]=='}')
{
if(S.size()==0)return puts("Impossible");
if(S.top()=='{')
S.pop();
else
{
ans++;
S.pop();
}
}
else S.push(s[i]);
}
if(S.size()!=0)return puts("Impossible");
cout<<ans<<endl;
}
Educational Codeforces Round 4 C. Replace To Make Regular Bracket Sequence 栈的更多相关文章
- Educational Codeforces Round 4 C. Replace To Make Regular Bracket Sequence
题目链接:http://codeforces.com/contest/612/problem/C 解题思路: 题意就是要求判断这个序列是否为RBS,每个开都要有一个和它对应的关,如:<()> ...
- Codeforces Round #529 (Div. 3) E. Almost Regular Bracket Sequence (思维)
Codeforces Round #529 (Div. 3) 题目传送门 题意: 给你由左右括号组成的字符串,问你有多少处括号翻转过来是合法的序列 思路: 这么考虑: 如果是左括号 1)整个序列左括号 ...
- Codeforces Round #529 (Div. 3) E. Almost Regular Bracket Sequence(思维)
传送门 题意: 给你一个只包含 '(' 和 ')' 的长度为 n 字符序列s: 给出一个操作:将第 i 个位置的字符反转('(' ')' 互换): 问有多少位置反转后,可以使得字符串 s 变为&quo ...
- Codeforces Round #529 (Div. 3) E. Almost Regular Bracket Sequence (思维,模拟栈)
题意:给你一串括号,每次仅可以修改一个位置,问有多少位置仅修改一次后所有括号合法. 题解:我们用栈来将这串括号进行匹配,每成功匹配一对就将它们消去,因为题目要求仅修改一处使得所有括号合法,所以栈中最后 ...
- 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 ...
- Replace To Make Regular Bracket Sequence
Replace To Make Regular Bracket Sequence You are given string s consists of opening and closing brac ...
- 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 ...
- D - Replace To Make Regular Bracket Sequence
You are given string s consists of opening and closing brackets of four kinds <>, {}, [], (). ...
- CF 612C. Replace To Make Regular Bracket Sequence【括号匹配】
[链接]:CF [题意]:给你一个只含有括号的字符串,你可以将一种类型的左括号改成另外一种类型,右括号改成另外一种右括号 问你最少修改多少次,才能使得这个字符串匹配,输出次数 [分析]: 本题用到了栈 ...
随机推荐
- AI线性图标教程-转起
- vmware10中开启Intel VT-x
记得刚接触linux的时候,是在win7下使用vmware虚拟机来安装linux,这样就可以方便的一边使用win7娱乐,一边在linux下进行学习.后来发现这种方式使得win7很卡,虚拟机也很卡,让人 ...
- centos 7搭建vpn(pptpd)服务器 (只限centos 7)
第一步:首先检查ppp是否开启 若使用XEN构架的VPS,此步骤不用执行 终端输入命令:cat /dev/ppp 开启成功的标志:No such file or directory 或者 No su ...
- searchDisplayController 时引起的数组越界
当 [searchDisplayController.searchResultsTableView setSeparatorStyle:UITableViewCellSeparatorStyleNo ...
- html5 canvas 鼠标绘制
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- nova service-list
nova-scheduler start/running, process 4820root@ruiy-controller-a:/var/log/nova# nova service-list+-- ...
- 第二百二十四天 how can I 坚持
实物商品兑换,有点小难搞,其实也没什么难的,也就那些东西,不过好像这就是我设计实现的,干起来挺来劲的. 供暖了,挺暖和的,哈哈. 小米耳机(炫彩版)到了.感觉挺好的. 还在纠结到底要买哪种颜色的羽绒服 ...
- string 与char* char[]之间的转换 2015-04-09 11:30 29人阅读 评论(0) 收藏
1.首先必须了解,string可以被看成是以字符为元素的一种容器.字符构成序列(字符串).有时候在字符序列中进行遍历,标准的string类提供了STL容器接口.具有一些成员函数比如begin().en ...
- Leveldb Advanced
[Slice] The return value of the it->key() and it->value() is a simple structure that contains ...
- HDU 1846 Brave Game(简单巴什博弈)
Brave Game Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...