Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java源代码的时间优化(CodeChef CubeCakes)_Java_Algorithm_Runtime - Fatal编程技术网

Java源代码的时间优化(CodeChef CubeCakes)

Java源代码的时间优化(CodeChef CubeCakes),java,algorithm,runtime,Java,Algorithm,Runtime,我正在做CodeChef“立方体蛋糕”练习题:()。CodeChef的响应是运行时太长。关于时间优化有什么建议吗 我的代码在这里: import java.util.Scanner; public class CubeCakes { public static void main(String[] args) { Scanner scan = new Scanner(System.in); Comparison[] comparisons = new C

我正在做
CodeChef
“立方体蛋糕”练习题:()。
CodeChef
的响应是运行时太长。关于时间优化有什么建议吗

我的代码在这里:

import java.util.Scanner;

public class CubeCakes {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        Comparison[] comparisons = new Comparison[scan.nextInt()];
        for (int i = 0; i < comparisons.length; i++)
            comparisons[i] = new Comparison(scan.nextInt(), scan.nextDouble(), scan.next(), scan.next());
        scan.close();       
        for (Comparison e : comparisons) System.out.println(e.compare());
    }
}

class Comparison {
    int N;
    double minPercentEqual;
    char[][][] cube1, cube2;
    double[][][] sim;

    public Comparison(int sideLength, double minPercentEqual, String cube1, String cube2) {
        this.N = sideLength;
        this.minPercentEqual = minPercentEqual / 100;   
        this.cube1 = fillCube(cube1);
        this.cube2 = fillCube(cube2);
        this.sim = new double[N][N][N];
    } public char[][][] fillCube(String cubeString) {
        char[][][] cube = new char[N][N][N];
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) {
                for (int k = 0; k < N; k++) cube[i][j][k] = cubeString.charAt(i*N*N + j*N + k);
            }
        } return cube;
    }

    public String compare() {
        String retVal = "-1";
        for (int length = 1; length <= N; length++) {
            int similarSubcubes = 0;
            int maxpow = 1;
            if (length > 1) maxpow = (int) Math.pow(2, (int) (Math.log(length - 1) / Math.log(2)));
            for (int i = 0; i+length <= N; i++) {
                for (int j = 0; j+length <= N; j++) {
                    for (int k = 0; k+length <= N; k++) if (isSimilarSubcube(i, j, k, length, maxpow)) similarSubcubes++;
                }
            } if (similarSubcubes > 0) retVal = length + " " + similarSubcubes;
        } return retVal;
    }

    public boolean isSimilarSubcube(int x, int y, int z, int length, int maxpow) {
        //if (x + length > N || y + length > N || z + length > N) return false;
        double similarity = 0;
        int a = x; int b = y; int c = z;
        while (maxpow > 0) {
            for (int i = a; i < x+length; i += maxpow) {
                for (int j = b; j < y+length; j += maxpow) {
                    for (int k = c; k < z+length; k += maxpow) {
                        if (length == 1 && cube1[i][j][k] == cube2[i][j][k]) sim[i][j][k] = 1;
                        similarity += sim[i][j][k];
                    }
                }
            } a += maxpow; b += maxpow; c += maxpow;
            maxpow /= 2;
        } if (Math.pow(2, length) % 1 == 0) sim[x][y][z] = similarity;
        return (similarity / Math.pow(length, 3)) >= minPercentEqual;
    }
}
import java.util.Scanner;
公共级长方体{
公共静态void main(字符串[]args){
扫描仪扫描=新扫描仪(System.in);
比较[]比较=新比较[scan.nextInt()];
for(int i=0;iN)返回false;
双重相似性=0;
int a=x;int b=y;int c=z;
而(最大功率>0){
对于(int i=a;i=MinPercenteEqual;
}
}

考虑将此发布到暴力解决方案将不起作用。提示:如果你知道每个i,j,k在子多维数据集中有多少个公共字符
[0..i][0..j][0..k]
,你怎么能在
[i1..i2][j1..j2][k1..k2]
中找到公共字符的数量呢?@sebii,我是以2为基数做的,请参见方法IsMilarSubcube()@lreeder通常,当一个人在这些比赛中超过了时间限制时,这从根本上说是一种错误的方法,不能通过一些小的改进来解决,因此比@La comadreja更合适。@La comadreja你应该在问题本身中总结问题陈述,而不仅仅是链接到它。