我的除法序列代码并不能解决所遇到的所有问题

时间:2018-10-21 06:01:56

标签: java arrays sequence dynamic-programming

  

此问题与正整数a1,a2,…,aN的序列有关。序列的子序列是通过删除某些元素而获得的任何东西。例如,3,7,11,3是6, 3 ,11,5, 7 ,4、3, 11 的子序列,5, 3 ,但3,3,7不是6,3,11,5,7,4,3,11,5,3的子序列。

     

一个完全除数的序列是序列a1,a2,…,aN,其中,当i      

给出一个整数序列,您的目标是找到该序列最长的完全除法子序列的长度。

     

考虑顺序2,3,7,8,14,39,145,76,320

     

它具有长度为3的完全除法序列,即2,8,320,但长度不等于4或更大。

     

考虑序列2,11,16,12,36,60,71,17,29,144,288,129,432,993。

     

它具有两个完全分开的长度为5的子序列-(2,12,36,144,288)或(2,12,36,144,432)。

为解决此问题,我编写了以下代码:

import java.util.Scanner;

class DivSeq {

  private int n, input[];

  void accept() {
    Scanner sc = new Scanner(System.in);
    n = sc.nextInt();
    input = new int[n];
    for(int i = 0; i<n; i++)
    input[i] = sc.nextInt();
    sc.close();         
}

int size(int a[]) {
    //this function returns the number of non zero entries in an array
    int ctr = 0;
    for(int i = 0; i<a.length; i++) {
        if(a[i]==0)
        break;
        else
        ctr++;
    }
    return ctr;
  }

  int sequence() {
    int subseq[], pvrseq[], seq[], j, a = 1, q, k = 1, f = 0;
    subseq = new int [n]; 
    pvrseq = new int [n];
    seq = new int [n]; 
    for(int i = 0; i<n-1; i++) {
        k = 1;
        for(int c = 0; c<seq.length; c++)
        seq[c] = 0;
        //seq has been initialized, now inserting 1st value
        seq[0] = input[i];
        //creating the sequence
        for(j = i+1; j<n; j++) {
            if(input[j]%input[i]==0) 
            seq[k++] = input[j];
        }
        //if size of sequence is 1, then there is no use of checking it
        if(size(seq)<2)
        continue;
        subseq[0] = seq[0];
        a = 1;
        while(a<size(seq)-1) {
            k = 2;
            for(int p = a; p<size(seq)-1; p++) {
                //initial value of subsequence
                if(subseq[1] == 0)
                subseq[1] = seq[p];
                //creating the subsequence
                for(q = p+1; q<size(seq); q++) {  

                    if(seq[q]%seq[p]==0) {
                        subseq[k++] = seq[q];
                        p = q-1;
                        f = 1;
                        break;
                    }

                }
                if(f==1 && q==size(seq)-1)
                break;
            }

            //checking the size of subsequence and previous sequence

            if(size(pvrseq)<size(subseq)) {

                 for(int y = 0; y<subseq.length; y++)
                 pvrseq[y] = subseq[y];

                 for(int y = 1; y<subseq.length; y++)
                 subseq[y] = 0;
            }
            a++;
        }           
    }

    return size(pvrseq);
  }

  public static void main(String [] args) {
    DivSeq obj = new DivSeq();
    obj.accept();
    System.out.println(obj.sequence());
  }

}

此代码解决了应解决的一些测试用例。

情况1:2,3,7,8,14,39,145,76,320         期望的输出= 3

案例2:2,11,16,12,36,60,71,17,29,144,288,129,432,993         期望的输出= 5

其余测试用例不可见。

但是,它不能解决所有问题,我也无法理解原因。它只能满足4/11个测试用例(包括案例1和案例2)。

2 个答案:

答案 0 :(得分:1)

@LuCio和@Aldert我找到了解决此问题的另一种方法。这是代码:

    import java.util.Scanner;

    class DivSeqUpdated {

      private int n, input[], weight[];

      void accept() {
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        input = new int[n];
        weight = new int[n];
        for(int i = 0; i<n; i++)
        input[i] = sc.nextInt();
        sc.close();
      }

      int max(int x, int y) {
        return x<y?y:x;
      }

      int sequence(int src, int a[], int n) {
        if(weight[src]==-1) {
            int i, tmp = 0;
            for(i = src+1; i<n; i++) {
                if(a[i]%a[src]==0)
                tmp = max(tmp, sequence(i,a,n));
            }
            weight[src] = tmp+1;
        }
        return weight[src];
      }

      public static void main(String [] args) {
        DivSeqUpdated obj = new DivSeqUpdated();
        obj.accept();
        for(int i = 0; i<obj.n; i++)
        obj.weight[i] = -1;
        int tmp = 0;
        for(int i = 0; i<obj.n; i++) 
        tmp = obj.max(tmp, obj.sequence(i,obj.input,obj.n));
        System.out.println(tmp);
      }    

  }

给出11/11测试的结果,可以解决该问题。我希望这也可以对其他用户有所帮助。

答案 1 :(得分:0)

我将采用递归方法来确定序列的最长子序列。

  protected static int[] subsequence(int[] seq) {
    int[] longestSub = new int[0];
    for (int i = 0; i < seq.length; i++) {
      int[] subAtI = subseq(subarray(seq, i));
      if (longestSub.length < subAtI.length) {
        longestSub = subAtI;
      }
    }
    return longestSub;
  }

  private static int[] subseq(int[] seq) {
    if (seq.length == 1) {
      return seq;
    }

    int[] longestSub = new int[0];
    int current = seq[0];
    for (int i = 1; i < seq.length; i++) {
      int number = seq[i];
      if (number > 0 && current > 0 && number % current == 0) {
        int[] subAtI = subseq(subarray(seq, i));
        if (longestSub.length < subAtI.length) {
          longestSub = subAtI;
        }
      }
    }

    return concat(current, longestSub);
  }

  private static int[] concat(int current, int[] sub) {
    int[] result = new int[sub.length + 1];
    result[0] = current;
    System.arraycopy(sub, 0, result, 1, sub.length);
    return result;
  }

  private static int[] subarray(int[] seq, int i) {
    int length = seq.length - i;
    int[] result = new int[length];
    System.arraycopy(seq, i, result, 0, length);
    return result;
  }

对于一个序列,迭代每个整数(seq[i])并确定该整数(subsequence(subarray(seq, i)))的子序列。最长的子序列就是结果。

将其应用于问题的一些示例:

  public static void main(String[] args) throws IOException {
    printLongestSub(new int[] { 2, 11, 16, 12, 36, 60, 71, 17, 29, 144, 288, 129, 432, 993 }); // [2, 12, 36, 144, 288]
    printLongestSub(new int[] { 2, 3, 7, 8, 14, 39, 145, 76, 320 }); // [2, 8, 320]
    printLongestSub(new int[] { 2 }); // [2]
    printLongestSub(new int[] { 2, 5 }); // [2]
    printLongestSub(new int[] { 5, 6, 12 }); // [6, 12]
    printLongestSub(new int[] { 5, 6, 12, 11, 33, 99 }); // [11, 33, 99]
  }

  private static void printLongestSub(int[] seq) {
    System.out.println(Arrays.toString(subsequence(seq)));
  }