H. Streets of Working Lanterns----思维题
2017-02-24 15:20
225 查看
H. Streets of Working Lanterns
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
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.
Input
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
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.
Examples
input
output
input
output
input
output
题目链接:http://codeforces.com/gym/101149/problem/H
这个题比赛的时候我没有做出来,想麻烦了,赛后按照柏皓的思路水了一发,过了,这个题就是让你把?变成“(”或“)”,看能不能完成括号匹配。
我们先计算一下字符串序列长度,奇数直接输出Impossible,然后我们计算需要多少个左括号,多少个右括号,?的规则是少多少个左括号就把前多少个?变成(,其他的就是),然后看看是否合法就可以。
代码:
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
int main(){
string s;
while(cin>>s){
int len=s.length();
if(len&1){
cout<<"Impossible"<<endl;
continue;
}
else{
int a=0,b=0;
for(int i=0;i<len;i++){
if(s[i]=='(')
a++;
}
int k=len/2-a;
int c=0,d=0;
bool flag=true;
for(int i=0;i<len;i++){
if(s[i]=='?'&&k){
s[i]='(';
k--;
}
else if(s[i]=='?'){
s[i]=')';
}
if(s[i]=='(')
c++;
if(s[i]==')')
d++;
if(c<d){
flag=false;
break;
}
}
if(!flag||(c!=d)){
cout<<"Impossible"<<endl;
}
else{
cout<<s<<endl;
}
}
}
return 0;
}
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
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.
Input
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
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.
Examples
input
(?(?))
output
()(())
input
()
output
()
input
?(
output
Impossible
题目链接:http://codeforces.com/gym/101149/problem/H
这个题比赛的时候我没有做出来,想麻烦了,赛后按照柏皓的思路水了一发,过了,这个题就是让你把?变成“(”或“)”,看能不能完成括号匹配。
我们先计算一下字符串序列长度,奇数直接输出Impossible,然后我们计算需要多少个左括号,多少个右括号,?的规则是少多少个左括号就把前多少个?变成(,其他的就是),然后看看是否合法就可以。
代码:
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
int main(){
string s;
while(cin>>s){
int len=s.length();
if(len&1){
cout<<"Impossible"<<endl;
continue;
}
else{
int a=0,b=0;
for(int i=0;i<len;i++){
if(s[i]=='(')
a++;
}
int k=len/2-a;
int c=0,d=0;
bool flag=true;
for(int i=0;i<len;i++){
if(s[i]=='?'&&k){
s[i]='(';
k--;
}
else if(s[i]=='?'){
s[i]=')';
}
if(s[i]=='(')
c++;
if(s[i]==')')
d++;
if(c<d){
flag=false;
break;
}
}
if(!flag||(c!=d)){
cout<<"Impossible"<<endl;
}
else{
cout<<s<<endl;
}
}
}
return 0;
}
相关文章推荐
- GYM 101149 H.Streets of Working Lanterns(贪心)
- 【思维】Stacks of Flapjacks
- svn1.7 appears to be part of a Subversion 1.7 or greater working copy. Please upgrade your Subversio
- 【codefores 612】 The Union of k-Segments 【思维+ 线段处理】
- Add n number of working days to date
- 【Codeforces】-699A-Launch of Collider(思维)
- 贪心/思维题 UVA 11292 The Dragon of Loowater
- the case of using Json in my working
- pwd--print name of current/working directory
- Working of Garbage Collector
- 2010年7月28日_周三_Working with AJAX capabilities of the Web ADF_overview
- POJ 1859 The Perfect Symmetry && POJ2526 Center of symmetry(思维题)
- UESTC - 1263:The Desire of Asuna【思维】
- CodeChef:A temple of Snakes(思维 & 二分)
- Xcode4 布置Git环境Your working copy is out of date. Try pulling from the remote to get the latest change
- [Immutable.js] Working with Subsets of an Immutable.js Map()
- The Working Way of Motor Run Capacitors
- Codeforces 769D k-Interesting Pairs Of Integers【思维+预处理+暴力枚举】
- Smart OJ 2484 Sum of xor 思维题 [二星]
- zoj&CCPC秦皇岛站E-思维-String of CCPC