描述 Description
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.
输入格式 InputFormat
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.
输出格式 OutputFormat
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.
样例输入 SampleInput
(((((#(#(#(#()
样例输出 SampleOutput
1
1
1
5
代码 Code
因为可以把右括号全加到最后一个 #上去,所以只要保证每个 #都能有至少一个) 且符合条件即可。
#include <stdio.h>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
const int maxn=100005;
int i,j,t,n,m,l,r,k,z,y,x;
char a[maxn];
int main()
{
scanf("%s",a);
n=strlen(a);
for (m=x=i=0;i<n;i++)
{
if (a[i]=='(') x++;
else x--;
if (a[i]=='#') m++;
if (x<0)
{
printf("-1\n");
return 0;
}
}
z=x;
for (x=0,i=n-1;i>=0 && a[i]!='#';i--)
{
if (a[i]=='(') x--;else x++;
if (x<0)
{
printf("-1\n");
return 0;
}
}
for (i=1;i<m;i++) printf("1\n");
printf("%d\n",z+1);
}