**Codeforces Round #282 (Div. 2) C. Treasure ACM解题报告(构造难题)

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.

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 s contains 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.

题目大意就是给你一串字符串,#可以是若干个)不能是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;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值