D - 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

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

Input

[<}){}

Output

2

Input

{()}[]

Output

0

Input

]]

Output

Impossible

意思是有左右两类括号,同类可以变形,求把所有括号消掉需要变形多少次,不可以消掉就输出impossible,加个例子{[}]这个是要两次;用stack栈

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include <iomanip>
#include<cmath>
#include<float.h>
#include<string.h>
#include<algorithm>
#define sf scanf
#define pf printf
#define mm(x,b) memset((x),(b),sizeof(x))
#include<vector>
#include<queue>
#include<stack>
#include<map>
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=a;i>=n;i--)
typedef long long ll;
typedef long double ld;
typedef double db;
const ll mod=1e9+100;
const db e=exp(1);
using namespace std;
const double pi=acos(-1.0);
char a[1000005];
int judge(int n)
{
    stack<char>v;
    int num1=0,num2=0,ans=0;
    rep(i,0,n)
    {
        if(a[i]==‘[‘||a[i]==‘(‘||a[i]==‘{‘||a[i]==‘<‘)
        v.push(a[i]);
        else
        {
            if(v.empty()) return -1;
            switch(a[i])
            {
                case ‘>‘:if(v.top()!=‘<‘) ans++;break;
                case ‘]‘:if(v.top()!=‘[‘) ans++; break;
                case ‘)‘:if(v.top()!=‘(‘) ans++; break;
                case ‘}‘:if(v.top()!=‘{‘) ans++; break;
            }
            v.pop();
        }
    }
    if(!v.empty()) return -1;
    return ans;
}
int main()
{
    sf("%s",a);
    int len=strlen(a);
    if(len&1) { pf("Impossible"); return 0; }
    if(judge(len)==-1) pf("Impossible");
    else pf("%d",judge(len));
    return 0;
}

原文地址:https://www.cnblogs.com/wzl19981116/p/9406573.html

时间: 2024-10-22 06:10:26

D - Replace To Make Regular Bracket Sequence的相关文章

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

[链接]:CF [题意]:给你一个只含有括号的字符串,你可以将一种类型的左括号改成另外一种类型,右括号改成另外一种右括号 问你最少修改多少次,才能使得这个字符串匹配,输出次数 [分析]: 本题用到了栈.如果遇上左括号,就加进栈里.如果遇上右括号,就判断栈里的左括号是否和它匹配,不匹配就加一.不论匹不匹配,判断后都要让左括号出栈. 如果最后栈不为空,或者栈在循环结束前就为空,那么不论怎么改变,左右括号都不可能刚好匹配. [代码]: #include<cstdio> #include<cst

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.com/problemset/problem/5/C Description This is yet another problem dealing with regular bracket sequences. We should remind you that a bracket sequence

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

题目传送门 1 /* 2 题意:求最长括号匹配的长度和它的个数 3 贪心+stack:用栈存放最近的左括号的位置,若是有右括号匹配,则记录它们的长度,更新最大值,可以在O (n)解决 4 详细解释:http://blog.csdn.net/taoxin52/article/details/26012167 5 */ 6 #include <cstdio> 7 #include <algorithm> 8 #include <cstring> 9 #include <

Codeforces 1132A. Regular Bracket Sequence

原题链接:Codeforces 1132A. Regular Bracket Sequence 题目大意:你有\({cnt}_1,{cnt}_2,{cnt}_3,{cnt}_4\)个"((","()",")(","))",问能否将这些字符串组成一个合法的括号序列. 题解:这一道题,很明显的\({cnt}_2\)是不需要管的,对于第三种情况,它并不改变左右括号的数量差,只有第一.四情况改变,那么,很明显\({cnt}_1={cn

CodeForces 5C Longest Regular Bracket Sequence

题意:给定一串括号,求最长的规则('(())'或‘(()())’)的字串及最长字串的个数: 思路:使用栈保存左括号,与最近的右括号匹配,使用递推推出每个位置最长字串长度: #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> #include<stack> using namespace std; stack<int> t; int n,m,len

Codeforces Beta Round #5 C. Longest Regular Bracket Sequence

经过了一个多月的时间,今天终于可以回到正轨了,继续开始刷CF. 题目大意: 给出一个只有括号的字符串,求最长"匹配"子串的长度和数量. 解题思路: 设置数组记录匹配括号段的开头. 下面是代码: #include <set> #include <map> #include <queue> #include <math.h> #include <vector> #include <string> #include &l

E. Almost Regular Bracket Sequence

题目链接:http://codeforces.com/contest/1095/problem/E 解题心得: 刚开始拿到这个题的时候还真的没什么思路,后来仔细想想还是比较简单的.首先题目要求翻转一个括号就要达到符合括号的匹配规则,那么在匹配完符合条件的括号之后有多出的两个括号向左或者向右. 其次从左边开始)不能比(多出两个以上,不然无法通过翻转一个得到符合规范的括号列,当刚好多出一个的时候这个括号必须翻转. #include <bits/stdc++.h> using namespace s

Codeforces Round #529 (Div. 3) E. Almost Regular Bracket Sequence (思维)

Codeforces Round #529 (Div. 3) 题目传送门 题意: 给你由左右括号组成的字符串,问你有多少处括号翻转过来是合法的序列 思路: 这么考虑: 如果是左括号 1)整个序列左括号个数比右括号多 2 2)在这个位置之前,所有位置的前缀左括号个数都不少于前缀右括号个数 3)在这个位置和这个位置之后,在修改后所有位置的前缀左括号个数减去前缀右括号个数大于2 (这里这么想,把左变成右,左-1,右+1) 右括号也是这样 代码: #include<bits/stdc++.h> usi

cf3D Least Cost Bracket Sequence

This is yet another problem on regular bracket sequences. A bracket sequence is called regular, if by inserting "+" and "1" into it we get a correct mathematical expression. For example, sequences "(())()", "()" and