zoukankan      html  css  js  c++  java
  • 16 多校8 Rikka with Parenthesis II

    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 SS, and he wants Rikka to choose two different position i,ji,j and swap Si,SjSi,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?

    InputThe 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 ‘)’.OutputFor each testcase, print "Yes" or "No" in a line.Sample Input

    3
    4
    ())(
    4
    ()()
    6
    )))(((

    Sample Output

    Yes
    Yes
    No
    
    
            
     

    Hint

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

    水题,注意两点1一定要交换所以"()"不行;2记录 l和r 遇到第一次r>l的情况和最后面的'('交换位置,之后就不能交换位置了

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<queue>
     4 #include<vector>
     5 #include<cstring>
     6 #include<string>
     7 #include<algorithm>
     8 #include<map>
     9 #include<cmath>
    10 #include<math.h>
    11 using namespace std;
    12 
    13 
    14 
    15 int main()
    16 {
    17     int T,n;
    18     int l,r,sum;
    19     scanf("%d",&T);
    20     char a[100005];
    21     bool flag1,flag2;
    22     while(T--)
    23     {
    24         scanf("%d",&n);
    25         if(n==0)
    26         {
    27             printf("Yes
    ");
    28             continue;
    29         }
    30         cin>>a;
    31         l=0;r=0;sum=0;
    32         if(n==2&&a[0]=='('&&a[1]==')')
    33             printf("No
    ");
    34         else if(n%2!=0)
    35             printf("No
    ");
    36         else
    37         {
    38             flag1=false;flag2=true;
    39             for(int i=0;i<n;i++)
    40             {
    41                 if(a[i]=='(')
    42                     l++;
    43                 else if(a[i]==')')
    44                     r++;
    45                 if(r>l&&!flag1)
    46                 {
    47                     for(int j=n-1;j>i;j--)
    48                     {
    49                         if(a[j]=='(')
    50                         {
    51                             a[i]='(';
    52                             a[j]=')';
    53                             l++;
    54                             r--;
    55                             flag1=true;
    56                             break;
    57                         }
    58                     }
    59                 }
    60                 else if(r>l&&flag1==true)
    61                 {
    62                     flag2=false;
    63                     break;
    64                 }
    65             }
    66             if(flag2&&l==r)
    67                 printf("Yes
    ");
    68             else
    69                 printf("No
    ");
    70         }
    71         
    72     }
    73     return 0;
    74 }
  • 相关阅读:
    (C)const关键字
    (C)volatile关键字
    蛋疼的四旋翼
    多线程之:死锁
    多线程之:ThreadLocal
    多线程之:线程同步代码块
    多线程之:线程安全
    多线程之:竞态条件&临界区
    多线程之:java线程创建
    多线程之:多线程的优缺点
  • 原文地址:https://www.cnblogs.com/Annetree/p/7201127.html
Copyright © 2011-2022 走看看