CodeForces 494A

A. Treasure
 
 

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.

Examples
input
(((#)((#)
output
1
2
input
()((#((#(#()
output
2
2
1
input
#
output
-1
input
(#)
output
-1

大致题意:给你一个字符串,‘#’代表大于0个‘)’,让你输出可以使()匹配的每个‘#’替换的‘)’的数目。
看到()匹配我第一想法就是栈,想到栈基本这道题就解决了一半了,剩余就是细节处理大致就是每个#前面的‘(’要大于‘)’,最后一个#后面‘(’不能大于‘)’。
具体代码如下:
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<stack>
#include<string.h>
using namespace std;
stack<char> s;
char v[100005];
int num = 0;
int ans[100005];
int main()
{
    cin >> v;
    int len = strlen(v);
    if (v[0] == ')' || v[0] == '#' || v[len - 1] == '(')
    {
        cout << "-1" << endl;
        return 0;
    }
    int n1 = 0,n2 = 0;//n1表示每个#号前面的(数目,n2表示最后一个#后面)的数目
    for (int i = 0; i < len; i++)
    {
        if (v[i] == '(')
        {
            s.push(v[i]);
            n1++;
        }        
        if (v[i] == ')')
        {
            s.pop();
            n1--;
        }
        if (v[i] == '#')
        {
            int j;
            for (j = i + 1; j < len; j++)
            {
                if (v[j] == '#')
                {
                    ans[num++] = 1;
                    n1--;
                    break;
                }
            }
            if (j == len)
            {
                for (j = i + 1; j < len; j++)
                {
                    if (v[j] == '(')
                        n1++;
                    else
                        n1--;
                }
                ans[num++] = n1;
                break;
            }
        }
        if (n1 < 0)
        {
            cout << "-1" << endl;
            return 0;
        }
    }
    for (int i = len - 1; i >= 0; i--)
    {
        if (v[i] != '#')
        {
            if (v[i] == ')')
                n2++;
            else
                n2--;
        }
        else
            break;
    }
    if (n1 <= 0 || n2 < 0)
    {
        cout << "-1" << endl;
    }
    else
    {
        for (int i = 0; i < num; i++)
        {
            cout << ans[i] << endl;
        }
    }
    return 0;
}

发这个文章的主要原因是这是弱第一次用STL。。。。。。





原文地址:https://www.cnblogs.com/Anony-WhiteLearner/p/6354031.html