hdoj-acm1023 (JAVA)

Source: Internet
Author: User

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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.