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.
InputThe 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.
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.
(((#)((#)
1 2
()((#((#(#()
2 2 1
#
-1
(#)
-1
|s| denotes the length of the string s.
#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]);
}