码迷,mamicode.com
首页 > 其他好文 > 详细

codeforces 494a//Treasure// Codeforces Round #282(Div. 1)

时间:2018-09-17 13:25:52      阅读:130      评论:0      收藏:0      [点我收藏+]

标签:乱码   ret   link   syn   work   std   algorithm   mat   push   

题意:一个‘(‘  ,  ‘)‘  ,  ‘#‘组成的串,可将‘#‘换成至少一个‘)‘。问一个换法能使串匹配。

至少换成一个,那么就先都换成一个,记结果为str。最后一个‘)‘的后面没有未匹配的‘(‘时可行。否则输出-1。因为后面不可能再添加‘)‘了。如果str会因为‘)‘匹配不到‘(‘则无解。否则有解,将str中‘(‘的数量-‘)‘的数量加到最后一个串中。

乱码:

//#pragma comment(linker,"/STACK:1024000000,1024000000") 
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
#include <stack>
#include <list>
using namespace std;
const int SZ=1000010,INF=0x7FFFFFFF;
typedef long long lon;
const double EPS=1e-9;

int work(string &str)
{
    int res=0;
    stack<char> stk;
    for(int i=0;i<str.size();++i)
    {
        if(str[i]==()
        {
            stk.push(str[i]);
        }
        else
        {
            if(stk.empty())
            {
                return INF;
            }
            else
            {
                stk.pop();
            }
        }
    }
    return stk.size();
}

bool work2(string &str)
{
    stack<char> stk;
    for(int i=0;i<str.size();++i)
    {
        if(str[i]==()
        {
            stk.push(();
        }
        else
        {
            if(!stk.empty())
            {
                stk.pop();
            }
        }
    }
    return stk.empty();
}

int main()
{
    std::ios::sync_with_stdio(0);
    //freopen("d:\\1.txt","r",stdin); 
    string str;
    cin>>str;
    int resnum=count(str.begin(),str.end(),#);
    int last=str.find_last_of(#);
    string sub=str.substr(last+1,str.size());
    //cout<<sub<<endl;
    for(int i=0;i<str.size();++i)
    {
        if(str[i]==#)str[i]=);
    }
    int res=work(str);
//    int cnt1=count(str.begin()+last+1,str.end(),‘(‘);
//    int cnt2=count(str.begin()+last+1,str.end(),‘)‘);
//    if(cnt1>cnt2)res=INF;
    //cout<<resnum<<endl;
    if(!work2(sub))res=INF;
    if(res!=INF)
    for(int i=0;i<resnum;++i)
    {
        if(i!=resnum-1)cout<<1<<endl;
        else cout<<1+res<<endl;
    }
    else cout<<-1<<endl;
    return 0;
}

 

codeforces 494a//Treasure// Codeforces Round #282(Div. 1)

标签:乱码   ret   link   syn   work   std   algorithm   mat   push   

原文地址:https://www.cnblogs.com/gaudar/p/9661398.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!