Codeforces 494 A. Treasure && Codeforces Round #282 (Div. 1)

题目链接:

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

解题思路:

Codeforces官方题解:

Consider a string consisting of '(' and ')' characters. Let's build the following sequence from this string:

  1. a0 = 0

  2. for each 1 ≤ i ≤ |s| ai = ai - 1 + 1 if si = '(' and ai = ai - 1 - 1 otherwise. (The string is considered as 1-based index).

It can be proven that a string is beautiful if the following conditions are satisfied:

  1. for each 0 ≤ i ≤ |s| ai ≥ 0.

  2. a|s| = 0

Using the above fact we can prove that if in a beautiful string we remove a ')' character and put it further toward the end of the string the resulting string is beautiful as well. These facts leads us to the following fact: if we can move a ')' character further toward the end of string it is better if we'd do it. This yields the following greedy solution:

We'll first put exactly one ')' character at each '#' character. Then we'll build the sequence we described above. if the first condition isn't satisfied then there is no way that leads to a beautiful string. So the answer is -1. Otherwise we must put exactly a|s| more ')' characters in the place of last '#' character. Then if this string is beautiful we'll print it otherwise the answer is -1.

AC代码:

#include<iostream>
#include<cstdio>
#include<string>
#include<vector>
using namespace std;
int main()
{
    string str;
    while(cin>>str)
    {
        vector<int>vec;
        vec.clear();
        bool flag = true;
        int lp=0,rp=0,index,l=str.size();
        for(int i=0;i<str.size();i++)
            if(str[i]=='#')
                index = i;
        for(int i=0;i<index;i++)
        {
            if(str[i]=='#')
            {
                vec.push_back(1);
                str[i]=')';
            }
        }
        lp=rp=0;
        for(int i=0;i<str.size();i++)
        {
            if(str[i]=='(')
                lp++;
            else
                if(str[i]==')')
                    rp++;
        }
        string mak=")",fina="";
        for(int i=0;i<lp-rp-1;i++)
            mak+=')';
        vec.push_back(lp-rp);
        for(int i=0;i<str.size();i++)
        {
            if(str[i]=='#')
                fina+=mak;
            else
                fina+=str[i];
        }
        lp=rp=0;
        //    cout<<fina<<endl;
        for(int i=0;i<fina.size();i++)
        {
            if(fina[i]==')')
                rp++;
            else
                lp++;
            if(rp>lp)
                flag=false;
        }
        if(flag)
        {
            for(int i=0;i<vec.size();i++)
                printf("%d\n",vec[i]);
        }
        else
            printf("-1\n");
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值