HDOJ 5831 Rikka with Parenthesis II (堆栈或者水题)

来源:互联网 发布:淘宝手机卖家版 编辑:程序博客网 时间:2024/06/05 13:25

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


Problem Description
As we know, Rikka is poor at math. Yuta is worrying about this situation, so he gives Rikka some math tasks to practice. There is one of them:

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, and he wants Rikka to choose two different position i,j and swap Si,Sj.

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?
 

Input
The first line contains a number t(1<=t<=1000), the number of the testcases. And there are no more then 10 testcases with n>100

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 ‘)’.
 

Output
For each testcase, print "Yes" or "No" in a line.
 

Sample Input
34())(4()()6)))(((
 

Sample Output
YesYesNo
Hint
For the second sample input, Rikka can choose (1,3) or (2,4) to swap. But do nothing is not allowed.
 

Author
学军中学
 

Source

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;  }  


0 0
原创粉丝点击