• Codeforces Round #282 (Div. 1) A. Treasure 水题


    A. Treasure

    Time Limit: 20 Sec

    Memory Limit: 256 MB

    题目连接

    http://codeforces.com/contest/494/problem/A

    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.

    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.

    Sample Input

    (((#)((#)

    Sample Output

    1
    2

    HINT

    题意

    给你一个序列,其中#可以被替换成1个或多个)符号

    你需要使得这个序列beautiful

    beautiful的定义是,对于每一个i,使得(的个数大于或等于),并且最后(的个数和)的个数相同

    题解:

    简单分析一下我就可以贪心的去搞一搞

    前面的#我们都只替换一个,然后最后的#替换剩下的

    然后我们再扫一遍进行check就好了

    代码

    #include<iostream>
    #include<stdio.h>
    #include<vector>
    using namespace std;
    
    string s;
    vector<int> ans;
    int main()
    {
        cin>>s;
        int sum = 0;
        int flag = 0;
        for(int i=0;i<s.size();i++)
        {
            if(s[i]=='(')sum--;
            else if(s[i]==')')sum++;
            else
            {ans.push_back(i);flag++;}
            if(sum+flag>0)return puts("-1");
        }
        int k = ans.size();
        if(k+sum>0)return puts("-1");
        else if(k==0&&sum!=0)return puts("-1");
        else
        {
            int Ans = 0;
            for(int i=0;i<s.size();i++)
            {
                if(s[i]=='(')Ans--;
                else if(s[i]==')')Ans++;
                else if(i==ans[ans.size()-1])Ans+=0-(sum+k-1);
                else Ans++;
                if(Ans>0)return puts("-1");
            }
            for(int i=0;i<k;i++)
            {
                if(i==k-1)
                    printf("%d
    ",0-(sum+k-1));
                else
                    printf("1
    ");
            }
        }
    }
  • 相关阅读:
    运输计划[二分答案 LCA 树上差分]
    树的重心与树的直径
    约瑟夫问题
    [The 2019 Asia Yinchuan First Round Online Programming] D Take Your Seat
    CF858F Wizard's Tour
    当那一天来临
    NOI2000 青蛙过河[递推]
    BZOJ4305 数列的GCD
    中国剩余定理和扩展中国剩余定理
    重写select
  • 原文地址:https://www.cnblogs.com/qscqesze/p/4965912.html
Copyright © 2020-2023  润新知