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

至少换成一个,那么就先都换成一个,记结果为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=,INF=0x7FFFFFFF;
typedef long long lon;
const double EPS=1e-; int work(string &str)
{
int res=;
stack<char> stk;
for(int i=;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=;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();
//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+,str.size());
//cout<<sub<<endl;
for(int i=;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=;i<resnum;++i)
{
if(i!=resnum-)cout<<<<endl;
else cout<<+res<<endl;
}
else cout<<-<<endl;
return ;
}

codeforces 494a//Treasure// Codeforces Round #282(Div. 1)的更多相关文章

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

    A. Treasure Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/494/problem/A ...

  2. Codeforces Round #282 Div.1 B Obsessive String --DP

    题意: 给两个串S,T,问能找出多少的S的(a1,b1)(a2,b2)..(ak,bk),使Sa1---Sb1,...Sak---Sbk都包含子串T,其中k>=1,且(a1,b1)...(ak, ...

  3. 数学 Codeforces Round #282 (Div. 2) B. Modular Equations

    题目传送门 题意:a % x == b,求符合条件的x有几个 数学:等式转换为:a == nx + b,那么设k = nx = a - b,易得k的约数(>b)的都符合条件,比如a=25 b=1 ...

  4. Codeforces Round #282 (Div. 1)B. Obsessive String KMP+DP

    B. Obsessive String   Hamed has recently found a string t and suddenly became quite fond of it. He s ...

  5. Codeforces Round #282 (Div. 2) A

    解题思路:用数组将每一个显示数字可能表示的数字种数存储起来即可 反思:第一次做的时候没有做出来是因为题意理解错误,第二次WA是因为情况没有考虑完全,1对应有7个数字,5对应有4个数字       A. ...

  6. Codeforces Round #577 (Div. 2) D. Treasure Hunting

    Codeforces Round #577 (Div. 2)  D. Treasure Hunting 这个一场div2 前面三题特别简单,这个D题的dp还是比较难的,不过题目告诉你了只能往上走,所以 ...

  7. Codeforces Round #366 (Div. 2) ABC

    Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 #I hate that I love that I hate ...

  8. Codeforces Round #354 (Div. 2) ABCD

    Codeforces Round #354 (Div. 2) Problems     # Name     A Nicholas and Permutation standard input/out ...

  9. Codeforces Round #368 (Div. 2)

    直达–>Codeforces Round #368 (Div. 2) A Brain’s Photos 给你一个NxM的矩阵,一个字母代表一种颜色,如果有”C”,”M”,”Y”三种中任意一种就输 ...

随机推荐

  1. python 单例模式,一个类只能生成唯一的一个实例,重写__new__方法详解

    单例:一个类只能生成唯一的一个实例 每个类只要被实例化了,他的私有属性 '_instance'就会被赋值,这样理解对吗 对 #方法1,实现__new__方法 #并在将一个类的实例绑定到类变量_inst ...

  2. java,url长链接生成短链接,短链接生成器,自定义字符串,对字符串md5混合KEY加密,根据短链接获得key值,不重复的随机数,不重复的随机字符串

    java,url长链接生成短链接,短链接生成器,自定义字符串,对字符串md5混合KEY加密,根据短链接获得key值,不重复的随机数,不重复的随机字符串 package com.zdz.test; im ...

  3. [转] Python的import初探

    转载自:http://www.lingcc.com/2011/12/15/11902/#sec-1 日常使用python编程时,为了用某个代码模块,通常需要在代码中先import相应的module.那 ...

  4. 关于sqlite使用场景

    对于sqlite,实际中从来没有用过,也几乎没有考虑过其使用场景,更不要说专门去研究它了,今天看最新的数据库流行度排行榜的时候,发现sqlite的长期趋势好像一直在第十位左右徘徊,特地搜索了下其使用场 ...

  5. Observer模式实践

    Observer 模式在实践中的应用场景: 为 Point 类设计一个数据绑定机制,当其坐标 x 或 y 被更改时,可以通知外界其更改的过程.将更改过程打印在控制台上.考虑使用松耦合设计. 代码: # ...

  6. 09: TemplateView , ListView ,DetailView三种常用类视图用法

    1.1 视图混合介绍 1.Mixin和View的职能区分 1. Mixin提供数据,View提供模板和渲染,所以一般get_context_data在Mixin中,get(),post(),head( ...

  7. 05: 配置yum源

    1.1 将镜像复制到本地创建yum源 1.将准备好的系统镜像放到指定的目录,本次目录指定在:/dawnfs/sourcecode 2.创建挂载目录:mkdir /mnt/yum 3.挂载镜像: mou ...

  8. Ubuntu Linux系统环境变量配置文件

    Ubuntu Linux系统环境变量配置文件: /etc/profile : 在登录时,操作系统定制用户环境时使用的第一个文件 ,此文件为系统的每个用户设置环境信息,当用户第一次登录时,该文件被执行. ...

  9. 20145220韩旭飞《网络对抗》实验九:web安全基础实践

    基础问答 (1)SQL注入攻击原理,如何防御 原理:把SQL命令插入到Web表单递交或输入域名或页面请求的查询字符串. 防御: 利用输入规则限制进行防御,不允许特殊字符输入 (2)XSS攻击的原理,如 ...

  10. 括号序和dfs序

    记得清北讲过括号序和dfs序,忘记了 dfs序 dfs序就是dfs的顺序,这个好记 就是在dfs遍历树的时候,将每个结点开始时记录一次,结束时记录一次 而且一个子树可以表示为连续的一段, 只有子树操作 ...