Rikka with Parenthesis II
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 859 Accepted Submission(s): 442
Correct parentheses sequences can be defined recursively as follows:
1.The empty string "" is a correct sequence.
2.If "X" and "Y" are correct sequences, then "XY" (the concatenation of X and Y) is a correct sequence.
3.If "X" is a correct sequence, then "(X)" is a correct sequence.
Each correct parentheses sequence can be derived using the above rules.
Examples of correct parentheses sequences include "", "()", "()()()", "(()())", and "(((())))".
Now Yuta has a parentheses sequence S
Rikka likes correct parentheses sequence. So she wants to know if she can change S to a correct parentheses sequence after this operation.
It is too difficult for Rikka. Can you help her?
For each testcase, the first line contains an integers n(1<=n<=100000), the length of S. And the second line contains a string of length S which only contains ‘(’ and ‘)’.
3 4 ())( 4 ()() 6 )))(((
Yes Yes NoHintFor the second sample input, Rikka can choose (1,3) or (2,4) to swap. But do nothing is not allowed.
2016 Multi-University Training Contest 8
思路:
这道题的意思就是,现在有一个判断一个串是否是合法的串的规则,给你一个只包含左括号或右括号的串,要求你随机交换其中两个括号的位置一次,然后求得到的串是否合法。串合法的规则是, 只有"", "()", "()()()", "(()())"和"(((())))"这四种形式的是合法的。就比如样例中的())(,把第一个和第四个换一下位置就变成了(())就合法了,第三个样例)))(((就没有办法合法。
这道题有两种做法,分别是普凡王和老司机的两种做法:
做法一之普凡王:
做法效仿之前的一道矩阵乘法的题,当遇到左括号是就进栈,遇到右括号时就出栈进行处理再把处理的结果进栈。但是这道题有一点不同的就是可以通过一次改变来修正括号。那么在手工推导之下,你会发现,把前方的)和后方的(进行交换,除去已经匹配好的括号之后最外层的括号一定是)。。(这种形式,所以这个修正括号的机会就要留给这两个。所以就按类似于进栈出栈的想法来做了。
做法二之老司机:
想象有任意合法串,我们用A,B,C,D,E来表示那么能挽救回来的串的最糟糕形势无非就是 A ) B ) C ( D ( E其中用字母表示的地方前后括号都是能匹配的。那么最先先统计左括号和右括号的个数,如果不想等直接判no用一个ans=0,遇到左括号的时候ans++,遇到右括号的时候ans--。那么当ans<-2时,就表示输出的串我们已经无力回天把它变回来了。还有需要注意的就是每个串必须交换一次位置,所以()这样的输入也是no。
代码:
//普凡王做法
#include <iostream>
#include <cstdio>
#include <stack>
using namespace std;
const int maxn=100000+10;
int main(int argc, const char * argv[]) {
int T;
scanf("%d",&T);
while(T--)
{
int n,counter1=0,counter2=0;
stack <char> st;
scanf("%d",&n);
if(n==0) {printf("Yes\n"); continue;}
char s[maxn],left[maxn];
for(int i=1;i<=n;i++)
{
cin>>s[i];
if(s[i]=='(') counter1++;
else counter2++;
}
if(counter1!=counter2) {printf("No\n"); continue;}
if(n==2&&s[1]=='('&&s[2]==')') {printf("No\n"); continue;}
for(int i=1;i<=n;i++)
{
if(s[i]=='(')
{
st.push(s[i]);
}
else
{
if(st.empty()==1) st.push(s[i]);
else st.pop();
}
}
if(st.empty()==1) {printf("Yes\n"); continue;}
int p=0;
while(st.empty()!=1)
{
left[++p]=st.top();
st.pop();
}
left[1]='(';
left[p]=')';
for(int i=1;i<=p;i++)
{
if(left[i]=='(')
{
st.push(left[i]);
}
else
{
if(st.empty()==1) st.push(left[i]);
else st.pop();
}
}
if(st.empty()==1) {printf("Yes\n"); continue;}
else {printf("No\n"); continue;}
}
return 0;
}
//老司机做法
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
using namespace std;
const int maxn=100000;
char save[maxn+5];
//int panduan[maxn+5];
int main(){
int t;
scanf("%d",&t);
while(t--){
int n;
scanf("%d",&n);
if(n==1){
scanf("%s",save);
printf("No\n");
continue;
}
scanf("%s",save);
int i,j,k;
int before=0,after=0;
for(i=0;i<n;i++){
if(save[i]=='('){
before++;
}else{
after++;
}
}
if(before!=after){
printf("No\n");
continue;
}
int ans=0;
bool flag=false;
for(i=0;i<n;i++){
if(save[i]=='('){
ans++;
}else{
ans--;
}
if(ans<-2){
flag=true;
break;
}
}
if(n==2&&save[0]=='('&&save[1]==')'){
printf("No\n");
continue;
}
if(flag){
printf("No\n");
continue;
}else{
printf("Yes\n");
}
}
return 0;
}