Treasure CodeForces - 494A(栈模拟)

Malek has recently found a treasure map. While he was looking for a treasure he found a locked door. There was a string s written on the door consisting of characters '(', ')' and '#'. Below there was a manual on how to open the door. After spending a long time Malek managed to decode the manual and found out that the goal is to replace each '#' with one or more ')' characters so that the final string becomes beautiful.

Below there was also written that a string is called beautiful if for each i (1 ≤ i ≤ |s|) there are no more ')' characters than '(' characters among the first icharacters of s and also the total number of '(' characters is equal to the total number of ')' characters.

Help Malek open the door by telling him for each '#' character how many ')' characters he must replace it with.

Input

The first line of the input contains a string s (1 ≤ |s| ≤ 105). Each character of this string is one of the characters '(', ')' or '#'. It is guaranteed that scontains at least one '#' character.

Output

If there is no way of replacing '#' characters which leads to a beautiful string print  - 1. Otherwise for each character '#' print a separate line containing a positive integer, the number of ')' characters this character must be replaced with.

If there are several possible answers, you may output any of them.

Example
Input
(((#)((#)
Output
1
2
Input
()((#((#(#()
Output
2
2
1
Input
#
Output
-1
Input
(#)
Output
-1
Note

|s| denotes the length of the string s.

题意:给你一串字符,只包含'('     ')'    '#',并且保证一定会有#,#可以当任意个数的 ')',  问让#变成   ')' ,能否使括号配对,若能输出每个#代表多少个 右括号,不能则输出-1
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
using namespace std;
typedef long long LL;
const int N=1e5+1;
char s[N];
int a[N],b[N],ans[N];//a数组存左括号的下标,b存右括号的下标,ans存答案
int main()
{
    int ra=0,rb=0;//ra代表左括号个数,ra代表#个数
    scanf("%s",s);
    int len=strlen(s);
    //处理右括号抵消掉左括号
    for(int i=0;i<len;i++)
    {
        //每个#和左括号的位置存起来
        if(s[i]=='#') b[++rb]=i;
        else if(s[i]=='(') a[++ra]=i;
        else
        {
            if(!ra)//遇到右括号如果没有左括号可以匹配了,直接输出-1
            {
                printf("-1\n");
                return 0;
            }
            ra--;//可以的话抵消一个左括号
        }
    }
    int k=0,sum=0;
    while(rb)//从右往左计算每个#要代表多少个右括号
    {
        sum=0;
        if(!ra||a[ra]>b[rb])//没有左括号或者左括号的下标大于#的下标
        {
            printf("-1\n");
            return 0;
        }
        ra--;//左括号减一
        sum++;
        while(ra&&a[ra]>=b[rb-1])//前一个#之前的所有左括号都加到这个#上
        {
            ra--;
            sum++;
        }
        ans[k++]=sum;
        rb--;//#个数减一
    }
    for(int i=k-1;i>=0;i--)
        printf("%d\n",ans[i]);
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值