题目描述
Malek has recently found a treasure map. While he was looking for a treasure he found a locked door. There was a string s 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 i ( 1<=i<=|s| 1<=i<=∣s∣ ) there are no more ‘)’ characters than ‘(’ characters among the first i i characters of s 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.
输入格式
Malek has recently found a treasure map. While he was looking for a treasure he found a locked door. There was a string s 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 i ( 1<=i<=|s| 1<=i<=∣s∣ ) there are no more ‘)’ characters than ‘(’ characters among the first i i characters of s 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.
输出格式
Malek has recently found a treasure map. While he was looking for a treasure he found a locked door. There was a string s 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 i ( 1<=i<=|s| 1<=i<=∣s∣ ) there are no more ‘)’ characters than ‘(’ characters among the first i i characters of s 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.
题意翻译
得你一个包含"(“和”#"的序列,’#‘需要转换为k个右括号 ( k > 0 ),那么这个表达式的括号能否在你转化后匹配,如果可以,输出每一个’#'转化的右括号数 k 的值;如果不能,就输出 −1
可能有很多组满足的情况只需输出一组即可 Special Judge 万岁
输入输出样例
输入 #1 复制
(((#)((#)
输出 #1 复制
1
2
输入 #2 复制
()((#((#(#()
输出 #2 复制
2
2
1
输入 #3 复制
输出 #3 复制
-1
输入 #4 复制
(#)
输出 #4 复制
-1
说明/提示
|s| ∣s∣ denotes the length of the string s s .
分析:
最后一个#之前的#的取值都是1,最后一个#则将所有还没有匹配的’(‘匹配。
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 1e5+10;
char s[N];
int ans[N];
int main()
{
scanf("%s",s);
int n=strlen(s);
int a=0,flag=1,k=0;//a:记录'('的数量,flag:是否能成立,k:#的计数
for(int i=0;i<n;i++)
{
if(s[i]=='(') a++;
else if(s[i]==')')
{
if(a>0) a--;//i之前必须至少有一个为匹配的'('
else
{
flag=0;
break;
}
}
else if(s[i]=='#')
{
if(a>0) //i之前必须至少有一个为匹配的'('
{
ans[++k]=1;
a--;
}
else
{
flag=0;
break;
}
}
}
if(a>0&&k==0) flag=0;//如果没有#且原字符串不匹配
if(flag)
{
ans[k]+=a;
a=0,k=0;
for(int i=0;i<n;i++)//检验此时字符串是否匹配
{
if(s[i]=='(') a++;
else if(s[i]==')')
{
if(a>0) a--;
else
{
flag=0;
break;
}
}
else if(s[i]=='#')
{
if(a<ans[++k])
{
flag=0;
break;
}
else
{
a=a-ans[k];
}
}
}
if(a!=0) flag=0;
}
if(flag==0) printf("-1");
else
{
for(int i=1;i<=k;i++)
printf("%d\n",ans[i]);
}
return 0;
}