Gym
来源:互联网 发布:pi数据库下载 编辑:程序博客网 时间:2024/06/06 03:42
Policeman Anatoliy monitors a lair of unorganized criminal group spreading prohibited Asian drawings. The lair has only one entrance, which is also an exit. When someone enters into the lair, Anatoliy writes an opening round bracket in his notepad, and when someone comes out, he writes a closing round bracket.
Long surveillance provokes an appetite, so Anatoliy has to eat donuts not to starve to death. Unfortunately, after the surveillance had ended, Anatoliy discovered a lot of greasy stains left by donuts in his notepad, and they prevent to understand which brackets are opening or closing. He doesn't want his boss to shout on him, so he must restore his records. He ensured that the lair of criminals was empty before he started the surveillance and after he ended it.
The input contains a single string of length no more than 5·105. This string consists of characters «(», «)» and «?». Character «(» means that someone entered into the lair, character «)» means that someone came out of it, and character «?» means that there is a greasy stain on this place, and it's impossible to determine which of the other two characters was there initially.
Output a recovered string consisting of characters «(» and «)» only, so that Anatoliy really could write it in his notepad. If there are many suitable strings, output any of them. If Anatoliy messed up something and his records contained mistakes, output «Impossible», without quotes.
(?(?))
()(())
()
()
?(
Impossible
题意:
问你能否匹配上。。
不知道当时咋想的越想越复杂。。
括号匹配。。第一个条件 左括号数等于右括号。
第二个条件每个点的左括号数不能大于右括号。
定义两个变量存储 ?数量tmp 和 左括号与右括号的差值 cnt
if(cnt>tmp||len%2)如果差值大于问号数怎么都弥补不了。。长度为奇数肯定不满足。
然后就是确定每个位置右括号数量都大于左括号数量。
#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;#define N 500005char s[N];int main(){ while(~scanf("%s",s)) { int res=0; int cnt=0; int len=strlen(s); for(int i=0; i<len; i++) { if(s[i]=='(') res++; else if(s[i]==')') res--; else cnt++; } int Flag=0; if(abs(res)>cnt||(res+cnt)%2)Flag=1; else { int tmp=(cnt-res)/2; res=0; for(int i=0; i<len; i++) { if(s[i]=='?') { if(tmp)s[i]='(',tmp--; else s[i]=')'; } if(s[i]=='(') { res++; } else if(s[i]==')') { res--; if(res<0) Flag=1; } } } if(Flag) printf("Impossible\n"); else printf("%s\n",s); }}
- Gym
- Gym
- Gym
- Gym
- Gym
- Gym
- Gym
- Gym
- Gym
- Gym
- Gym
- Gym
- Gym
- Gym
- Gym
- Gym
- Gym
- Gym
- cvte笔试题
- 基于稀疏表示的分类方法 Sparse Representation based Classification Method
- poj 1325 Machine Schedule
- 如何运用html5 canvas将图片保存至手机本地的图库中?
- [PTA MOOC] 自测-4 Have Fun with Numbers(20 分)
- Gym
- 在复杂的网络环境下构建 DevOps 测试的最佳实践
- 安装docker时出现报错,curl: (35) gnutls_handshake() failed: The TLS connection was non-properly terminated.
- 二步实现 远程连接 阿里云SqlServer 2012 数据库服务器
- 关于NVMe SSD以及其IO性能抖动和blktrace工具
- Java string类特点1
- cnki__未解之谜
- 算法学习之旅,中级篇(6)-–递归之斐波拉契数列
- Java中Class对象详解