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 i characters 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.
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 s contains 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.
题目大意就是给你一串字符串,#可以是若干个)不能是0,然后每个#变成多少个)可以使所有括号都匹配。
这题是个构造题,我觉得好难的说,想了好久都没能找到个合适的思路,因为这个括号可以有大的套着很多小的,很复杂。
看了题解之后发现构造前面的#都变成1个),最后一个#来匹配剩下没匹配的(。
中间如果有)的数量多于(的话要跳出,输出-1.
#include<iostream>
#include<cstdio>
#include<cctype>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<string>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<sstream>
#include<stack>
using namespace std;
#define MAX 105
typedef long long LL;
const double pi=3.141592653589793;
const int INF=1e9;
const double inf=1e20;
int ans[100005];
int main()
{
string s;
cin>>s;
int n=s.size();
int tot=0,cnt=0,last,left=0,right=0,fail=0;
for(int i=0;i<n;i++)
{
if(s[i]=='#')
{
tot++;
last=i;//最后一个出现的#
}
}
for(int i=n-1;i>last;i--)
{
if(s[i]==')') right++;//右侧的情况
else if(s[i]=='(') right--;
if(right<0)
{
fail=1;
break;
}
}
if(!fail)
{
for(int i=0;i<=last;i++)
{
if(s[i]=='(') left++;//左侧的情况
else if(s[i]==')') left--;
else
{
if(cnt!=tot-1)
{
ans[cnt++]=1;//前tot-1个#每个都给1个)即可,这必然是可以得
left--;
}
else
{
left=left-right;
if(left<1)
{
fail=1;
break;
}
else ans[cnt]=left;
}
}
if(left<0)
{
fail=1;
break;
}
}
}
if(fail) printf("-1\n");
else
{
for(int i=0;i<tot;i++) printf("%d\n",ans[i]);
}
return 0;
}