Test instructions: The size of the input stack, output the number of possible stack order.
This problem, if done 1022, so long as the above change on the line,
The first idea is to add the full alignment-----The result is correct, but it will definitely expire.
Verifiable implementation: (TimeLimit exceeded)
ImportJava.util.*;ImportJava.io.*; Public classmain{ Public Static voidMain (string[] arg) {Scanner Scan=NewScanner (NewBufferedinputstream (system.in)); while(Scan.hasnextint ()) {intn =Scan.nextint (); Count= 0; Char[] cs =New Char[n]; Char[] copy =New Char[n]; for(inti = 0; I! = cs.length; i + +) {Cs[i]= (Char) I; Copy[i]= (Char) I; } getallpermutationsandcheck (Copy,cs,0, N); System.out.println (count); } scan.close (); } Static int[] Outflag =New int[200];//there are up to 200 trains, 1 indicates that 0 means Static Char[] Stack =New Char[100]; Static intcount; Static voidCheckChar[] Put,Char[] Pop) { intn =put.length; inttop =-1; intIn=0,out=0,flag=0; while(out!=n&&in!=n+1){ if(Top!=-1&&stack[top] = =Pop[out]) {Top--; out++; Outflag[flag+ +] = 1; Continue; } if(in==N) { Break; } Top++; Stack[top]= put[in++]; Outflag[flag+ +] = 0; } if(flag==2*N) {Count++; } } //Non-dictionary order Static voidGetallpermutationsandcheck (Char[] Copy,Char[] CS,intStartintLen) { if(Start = =Len) {Check (copy, CS); return; } for(inti = start; I! = Len; i + +) {swap (Cs,i,start); Getallpermutationsandcheck (Copy,cs,start+1, Len); Swap (Cs,i,start); } } Static voidSwapChar[] CS,intIintj) { CharT; T=Cs[i]; Cs[i]=Cs[j]; CS[J]=T; }}
Done 2021, there is a (n<1000000), the same as the problem (the result will be very large, so can not process it by 32-bit integers.), imaginable, cannot be used , there should be a clever way to beg.
But you can't think of it unless you can see the rules from the results--I don't see it anyway.
Results in accordance with Cattleya number, 1, 1, 2, 5, 429, 1430, 4862, 16796, 58786,......
Cattleya number implementation: ① h (n) = h (0) *h (n-1) +h (1) *h (n-2) + ... + h (n-1) H (0) (n>=2)
② H (n) =h (n-1) * (4*n-2)/(n+1)
③ H (n) =c (2n,n)/(n+1) (n=0,1,2,...)
④ H (n) =c (2n,n)-C (2n,n-1)(n=0,1,2,...)
I realized the second one: (wrong Answer)
ImportJava.util.*;ImportJava.io.*; Public classmain{ Public Static voidMain (string[] arg) {Scanner Scan=NewScanner (NewBufferedinputstream (system.in)); while(Scan.hasnextint ()) {intn =Scan.nextint (); intCount =Catalan (n); System.out.println (count); } scan.close (); } Static intCatalanintN) {if(n==1){ return1; } returnCatalan (n-1) * (4*n-2)/(n+1); }}
Of course, the numbers are out of bounds (the result would be very large, so if not process it by 32-bit integers.)
Therefore, to use a large number of processing--
I used the BigInteger in Java to Accepted immediately.
ImportJava.util.*;ImportJava.io.*;ImportJava.math.BigInteger; Public classmain{ Public Static voidMain (string[] arg) {Scanner Scan=NewScanner (NewBufferedinputstream (system.in)); while(Scan.hasnextint ()) {intn =Scan.nextint (); BigInteger Count=Catalan (n); System.out.println (Count.tostring ()); } scan.close (); } StaticBigInteger Catalan (intN) {if(n==1){ return NewBigInteger ("1"); } returnCatalan (n-1). Multiply (NewBigInteger (string.valueof (4*n-2))). Divide (NewBigInteger (String.valueof (n+1))); } }
hdoj-acm1023 (JAVA)