HDOJ-ACM1023(JAVA)

题意:输入栈的大小,输出可能的出栈顺序的个数。

这道题,如果做了1022,那就只要在上面改改就行了,

第一想法是加上全排列-----结果是正确的,但是绝对会超时

验证性的实现了:(Time Limit Exceeded)

import java.util.*;

import java.io.*;

public class Main{

    public static void main(String[] arg){
        Scanner scan = new Scanner(new BufferedInputStream(System.in));
        while(scan.hasNextInt()){
            int n =scan.nextInt();
            count = 0;
            char[] cs = new char[n];
            char[] copy = new char[n];
            for(int i = 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];//最多有200辆火车,1表示出,0表示进
    static char[] stack = new char[100];
    static int count;

    static void check(char[] put , char[] pop){
        int n = put.length;
        int top = -1;
        int in=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++;
        }
    }

    //非字典序
    static void getAllPermutationsAndCheck(char[] copy,char[] cs,int start,int len){
            if(start == len ){
                check(copy, cs);
                return;
            }
            for(int i = start ; i != len ; i ++){
                swap(cs,i,start);
                getAllPermutationsAndCheck(copy,cs,start+1,len);
                swap(cs,i,start);
            }
        }

        static void swap(char[] cs , int i , int j){
            char t;
            t = cs[i];
            cs[i] = cs[j];
            cs[j] = t;
        }

}

做了2021,题中有个(n<1000000),和这道题一样(The result will be very large, so you may not process it by 32-bit integers.),可以想象得到,不能用递归,应该有巧妙方法来求。

不过,绞尽脑汁地想也想不出来的,除非你能从结果中看出规律~反正我没看出来 - -

结果符合卡特兰数,1, 1, 2, 5, 14, 42, 132, 429, 1430, 4862, 16796, 58786,......

卡特兰数实现:① 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,...)

我实现了第二个:(Wrong Answer)

import java.util.*;

import java.io.*;

public class Main{

    public static void main(String[] arg){
        Scanner scan = new Scanner(new BufferedInputStream(System.in));
        while(scan.hasNextInt()){
            int n =scan.nextInt();
            int count = catalan(n);
            System.out.println(count);
        }
        scan.close();
    }

    static int catalan(int n) {
        if(n==1){
            return 1;
        }
        return catalan(n-1)*(4*n-2)/(n+1);
    }

}

当然,数字越界了(The result will be very large, so you may not process it by 32-bit integers.)

因此,要用大数处理 - -

我用JAVA里面的BigInteger立刻就Accepted了

import java.util.*;

import java.io.*;
import java.math.BigInteger;

public class Main{

    public static void main(String[] arg){
        Scanner scan = new Scanner(new BufferedInputStream(System.in));
        while(scan.hasNextInt()){
            int n =scan.nextInt();
            BigInteger count = catalan(n);
            System.out.println(count.toString());
        }
        scan.close();
    }

    static BigInteger catalan(int n) {
        if(n==1){
            return new BigInteger("1");
        }
        return catalan(n-1).multiply(new BigInteger(String.valueOf(4*n-2))).divide(new BigInteger(String.valueOf(n+1)));
    }

}
时间: 2024-11-04 17:46:38

HDOJ-ACM1023(JAVA)的相关文章

HDOJ 1000 java

第一次做hdoj 试了好几次才通过 发现主类的名字必须是Main import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner= new Scanner(System.in); while(scanner.hasNext()) { int A=scanner.nextInt(); int B=scanner.nextInt(); System.out.p

ACM--大数阶乘--HDOJ 1042--N!--Java

HDOJ题目地址:传送门 N! Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 74952    Accepted Submission(s): 21832 Problem Description Given an integer N(0 ≤ N ≤ 10000), your task is to calculate N! Inp

HDOJ 1001 java

import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner= new Scanner(System.in); int sum,n; while(scanner.hasNext()) { n=scanner.nextInt(); sum=0; for(int i=1;i<=n;i++) { sum+=i; } System.out.println(sum

HDOJ 1002 java

import java.math.BigInteger; import java.util.ArrayList; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner= new Scanner(System.in); int n=scanner.nextInt(); for(int i=1;i<=n;i++) { BigInteger a=sca

hdoj 1007 java MLE通不过

参考网上的一个算法,但是一直都是MLE,实在是想不出来什么办法了. 参考博客:编程之美寻找最近点对 import java.util.Arrays; import java.util.Scanner; public class Main { public static int N=100000; public static int[] list=new int[N]; public static Point[] ptns=new Point[N]; public static float MAX

【HDOJ 1002】A + B Problem II

A + B Problem II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 238517    Accepted Submission(s): 45969 Problem Description I have a very simple problem for you. Given two integers A and B, you

hdoj 1316 How Many Fibs? 【Java大数】+【打表】

现将前1000个的斐波那契数打表,然后再找就好了. 代码: import java.util.Scanner; import java.math.*; public class Main{ public static void main(String[] args){ Scanner cin = new Scanner(System.in); BigInteger[] s = new BigInteger[1005]; s[1] = new BigInteger("1"); s[2]

HDOJ/HDU 2203 亲和串(简单的判断~Java的indexOf()方法秒)

Problem Description 人随着岁数的增长是越大越聪明还是越大越笨,这是一个值得全世界科学家思考的问题,同样的问题Eddy也一直在思考,因为他在很小的时候就知道亲和串如何判断了,但是发现,现在长大了却不知道怎么去判断亲和串了,于是他只好又再一次来请教聪明且乐于助人的你来解决这个问题. 亲和串的定义是这样的:给定两个字符串s1和s2,如果能通过s1循环移位,使s2包含在s1中,那么我们就说s2 是s1的亲和串. Input 本题有多组测试数据,每组数据的第一行包含输入字符串s1,第二

hdoj 1753 大明A+B 高精度/java

大明A+B Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 9273    Accepted Submission(s): 3318 Problem Description 话说,经过了漫长的一个多月,小明已经成长了许多,所以他改了一个名字叫“大明”.这时他已经不是那个只会做100以内加法的那个“小明”了,现在他甚至会任意长度的正小数的加

hdoj 1002 A + B Problem II 高精度 java

A + B Problem II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 241933    Accepted Submission(s): 46646 Problem Description I have a very simple problem for you. Given two integers A and B, you