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 [题意]:给你一个只含有括号的字符串,你可以将一种类型的左括号改成另外一种类型,右括号改成另外一种右括号 问你最少修改多少次,才能使得这个字符串匹配,输出次数 [分析]: 本题用到了栈 ...
随机推荐
- 将cocos2dx项目从Visual Studio 迁移到 xcode
因为Visual Studio和XCode的巨大差异性,一开始选择任何一个IDE,都会有一个迁移的过程,XCode的迁移到Visual Studio相对非常简单,不用再介绍.将项目从Visual St ...
- SORT UNIQUE|AGGREGATE|GROUP BY|ORDER BY|JOIN
相信做oracle开发和管理的朋友对sort肯定不会陌生,大家通常都遇到这样那样的排序性能问题,所以我写这一系列关于sort的文章告诉大家在oracle里面sort是怎么一回事以及如果调整sort获得 ...
- PHP.ini 配置文件解析
[PHP] ;;;;;;;;;;;;;;;;;;;; About php.ini ;;;;;;;;;;;;;;;;;;;;; PHP's initialization file, generall ...
- 一些JS周边工具
Visual Studio JS 辅助插件 JScript Editor Extensions 功能: 1. 代码块折叠 2. 方法参数智能提示 3. 代码块Outlining ...
- ansible中文手册-基础模块使用
此篇文章主要是翻译ansible官网文档而来,在里面讲述了如何使用ansible的基础模块,总体感觉比较晦涩,但是后面会写出自己相关实践的文档,从而更加通俗易懂,官网的东西拿来当手册偶尔翻翻也是很不错 ...
- 记一个社交APP的开发过程——基础架构选型(转自一位大哥)
记一个社交APP的开发过程——基础架构选型 目录[-] 基本产品形态 技术选型 最近两周在忙于开发一个社交App,因为之前做过一点儿社交方面的东西,就被拉去做API后端了,一个人头一次完整的去搭这么一 ...
- TopFreeTheme精选免费模板【20130701.特别版】
今天我们整理了16款WordPress和Joomla的最新主题.它们都是来自Themeforest,RocketTheme,YooTheme以及TemPlaza的高质量主题,赶快收藏起来吧. Este ...
- Linux网卡启动报错(this device is not active)
重启网络服务 service network restart 报如下错误: shutting down interface eth0: error:device "eth0" ...
- [转]Python文件操作
前言 这里的“文件”不单单指磁盘上的普通文件,也指代任何抽象层面上的文件.例如:通过URL打开一个Web页面“文件”,Unix系统下进程间通讯也是通过抽象的进程“文件”进行的.由于使用了统一的接口,从 ...
- HBase高性能复杂条件查询引擎---二级多列索引
http://www.infoq.com/cn/articles/hbase-second-index-engine 原理 “二级多列索引”是针对目标记录的某个或某些列建立的“键-值”数据,以列的值为 ...