Topic Links:
Rikka with parenthesis II
Time limit:2000/1000 MS (java/others)
Memory limit:65536/65536 K (java/others)
Problem Descriptionas We know, Rikka is poor at math. Yuta is worrying on this situation, so he gives Rikka some math tasks to practice. There is one of the 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 and then "(x)" is a correct sequence.
Each correct parentheses sequence can is derived using the above rules.
Examples of correct parentheses sequences include "", "()", "() () ()", "(() () ())", and "(((()))".
Now Yuta have a parentheses sequenceS, and he wants Rikka to choose, different positioni,J and swapsi,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 is no more then ten testcases with n>100
For each testcase, the first line contains a 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 Input34 ()) (4 () () (6))) ((
Sample outputyesyesno Test Instructions: Ask the exchange of two positions to make these brackets match; Idea: Look at the number of brackets that cannot be matched, more than two pairs on the No; there is a WA point is () direct WA Cry me;
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include < cmath> #include <bits/stdc++.h> #include <stack> #include <map> using namespace std; #define for (i,j,n) for (int i=j;i<=n;i++) #define MST (SS,B) memset (ss,b,sizeof (ss)); typedef long Long LL; Template<class t> void Read (t&num) {char CH; bool F=false; For (Ch=getchar (); ch< ' 0 ' | | Ch> ' 9 '; f= ch== '-', Ch=getchar ()); for (num=0; ch>= ' 0 ' &&ch<= ' 9 '; num=num*10+ch-' 0 ', Ch=getchar ()); F && (num=-num);} int stk[70], tp;template<class t> inline void print (T p) {if (!p) {puts ("0"); return;} while (p) stk[++ TP] = p%10, p/=10; while (TP) Putchar (stk[tp--] + ' 0 '); Putchar (' \ n ');} Const LL Mod=1e9+7;const double Pi=acos ( -1.0); const ll Inf=1e18;const int N=1e5+10;const int Maxn=5e3+4;const double Eps=1 E-12;int N;char s[n];int Check () {int num1=0,num2=0; for (I,1,n) {if (s[i]== ' (') num1++; else num2++; } if (num1!=num2) return 0; if (n==2) {if (s[1]== ' (' &&s[2]== ') ') return 0; return 1; } num1=0;num2=0; for (I,1,n) {if (s[i]== ' (') num1++; else {if (num1==0) num2++; else num1--; }} if (num2>2) return 0; return 1;} int main () {int T; Read (T); while (t--) {read (n); scanf ("%s", s+1); if (check ()) printf ("yes\n"); else printf ("no\n"); } return 0;}
AC Code:
hdu-5831 Rikka with parenthesis II (greedy)