**Codeforces Round #282 (Div. 2) C. Treasure ACM解题报告(构造难题)
2015-02-07 17:07
435 查看
Malek has recently found a treasure map. While he was looking for a treasure he found a locked door. There was a string s written on the door
consisting of characters '(', ')' and '#'.
Below there was a manual on how to open the door. After spending a long time Malek managed to decode the manual and found out that the goal is to replace each '#' with one or
more ')' characters so that the final string becomes beautiful.
Below there was also written that a string is called beautiful if for each i (1 ≤ i ≤ |s|)
there are no more ')' characters than '(' characters among the first i characters
of s and also the total number of '(' characters is equal to the total number of ')'
characters.
Help Malek open the door by telling him for each '#' character how many ')'
characters he must replace it with.
Input
The first line of the input contains a string s (1 ≤ |s| ≤ 105).
Each character of this string is one of the characters '(', ')' or '#'.
It is guaranteed that s contains at least one '#' character.
Output
If there is no way of replacing '#' characters which leads to a beautiful string print - 1.
Otherwise for each character '#' print a separate line containing a positive integer, the number of ')'
characters this character must be replaced with.
If there are several possible answers, you may output any of them.
题目大意就是给你一串字符串,#可以是若干个)不能是0,然后每个#变成多少个)可以使所有括号都匹配。
这题是个构造题,我觉得好难的说,想了好久都没能找到个合适的思路,因为这个括号可以有大的套着很多小的,很复杂。
看了题解之后发现构造前面的#都变成1个),最后一个#来匹配剩下没匹配的(。
中间如果有)的数量多于(的话要跳出,输出-1.
consisting of characters '(', ')' and '#'.
Below there was a manual on how to open the door. After spending a long time Malek managed to decode the manual and found out that the goal is to replace each '#' with one or
more ')' characters so that the final string becomes beautiful.
Below there was also written that a string is called beautiful if for each i (1 ≤ i ≤ |s|)
there are no more ')' characters than '(' characters among the first i characters
of s and also the total number of '(' characters is equal to the total number of ')'
characters.
Help Malek open the door by telling him for each '#' character how many ')'
characters he must replace it with.
Input
The first line of the input contains a string s (1 ≤ |s| ≤ 105).
Each character of this string is one of the characters '(', ')' or '#'.
It is guaranteed that s contains at least one '#' character.
Output
If there is no way of replacing '#' characters which leads to a beautiful string print - 1.
Otherwise for each character '#' print a separate line containing a positive integer, the number of ')'
characters this character must be replaced with.
If there are several possible answers, you may output any of them.
题目大意就是给你一串字符串,#可以是若干个)不能是0,然后每个#变成多少个)可以使所有括号都匹配。
这题是个构造题,我觉得好难的说,想了好久都没能找到个合适的思路,因为这个括号可以有大的套着很多小的,很复杂。
看了题解之后发现构造前面的#都变成1个),最后一个#来匹配剩下没匹配的(。
中间如果有)的数量多于(的话要跳出,输出-1.
#include<iostream> #include<cstdio> #include<cctype> #include<cstdlib> #include<cmath> #include<algorithm> #include<cstring> #include<string> #include<vector> #include<queue> #include<map> #include<set> #include<sstream> #include<stack> using namespace std; #define MAX 105 typedef long long LL; const double pi=3.141592653589793; const int INF=1e9; const double inf=1e20; int ans[100005]; int main() { string s; cin>>s; int n=s.size(); int tot=0,cnt=0,last,left=0,right=0,fail=0; for(int i=0;i<n;i++) { if(s[i]=='#') { tot++; last=i;//最后一个出现的# } } for(int i=n-1;i>last;i--) { if(s[i]==')') right++;//右侧的情况 else if(s[i]=='(') right--; if(right<0) { fail=1; break; } } if(!fail) { for(int i=0;i<=last;i++) { if(s[i]=='(') left++;//左侧的情况 else if(s[i]==')') left--; else { if(cnt!=tot-1) { ans[cnt++]=1;//前tot-1个#每个都给1个)即可,这必然是可以得 left--; } else { left=left-right; if(left<1) { fail=1; break; } else ans[cnt]=left; } } if(left<0) { fail=1; break; } } } if(fail) printf("-1\n"); else { for(int i=0;i<tot;i++) printf("%d\n",ans[i]); } return 0; }
相关文章推荐
- Codeforces Round #384 (Div. 2) C. Vladik and fractions(构造题)
- Codeforces Round #289 (Div. 2, ACM ICPC Rules) D. Restoring Numbers 构造 数学
- Codeforces Round #265 (Div. 2) C. No to Palindromes! 构造不含回文子串的串
- Codeforces Round #298 (Div. 2) E. Berland Local Positioning System 构造
- Codeforces Round #257 (Div. 2)E(数论+构造)
- Codeforces Round #384 (Div. 2) C. Vladik and fractions(构造题)
- Codeforces Round #275 (Div. 2) C - Diverse Permutation (构造)
- Codeforces Round #265 (Div. 2) C. No to Palindromes! 构造不含回文子串的串
- Codeforces Round #447 (Div. 2) C 构造
- Codeforces Round #282 (Div. 1) A. Treasure (贪心)
- 【Codeforces Round 269 (Div 2)B】【简单构造】MUH and Sticks 数值升序输出三种可能编号序
- Codeforces Round #381 (Div. 2)C(构造,思维)
- Codeforces Round #252(Div. 2) 441C. Valera and Tubes 构造
- Codeforces Round #306 (Div. 2) ABCDE(构造)
- 【Codeforces Round 262 (Div 2)D】【构造】Little Victor and Set 集合最多取k数使得异或值尽可能小
- Codeforces Round #209 (Div. 2) B && Codeforces 359 B(构造)
- Codeforces Round #293 (Div. 2) -- A. Vitaly and Strings (字符串构造)
- **Codeforces Round #250 (Div. 2) C. The Child and Toy ACM解题报告(贪心难题)
- Codeforces Round #381 (Div. 2)C. Alyona and mex(构造题,好题)
- Codeforces Round #400 (Div. 1 + Div. 2, combined) C. Molly's Chemicals 区间和、构造、前缀的后缀