java—用动态规划帮助求解算法

tzxcd3kk  于 2021-07-09  发布在  Java
关注(0)|答案(0)|浏览(212)

我试图用动态规划来解决一个最长锚定梳问题,但不知道如何实现算法。除了算法之外的代码已经写在下面了。
有人能告诉我怎么解算法部分吗?
这是问题的定义。
在数字列表中,此数组中的任何子序列,其形式为a[i1]、a[i1+1]、a[i2]、a[i2+1]、,a[ik],数组a的2k个元素的[ik+1],对于某个整数k≥ 1,对于某些指数1≤ i1<i1+1<i2<i2+1<····<ik<ik+1≤ n使得a[i1]<a[i1+1]>a[i2]<a[i2+1]>a[i3]<····>a[ik]<a[ik+1]称为梳。
我们称一个给定的梳子为[i1],[i1+1],[i2],[i2+1],a[ik],如果a[i1]=a[ik],则锚定a[ik+1]。
3个例子:
例如,如果a[1,3,10,15,21],n=5,则因为该序列在增加,所以最长梳具有长度k=1,并且这种最长梳不是唯一的,例如,1,3;3, 10; 15、21–是3个最长且固定的梳子的示例,每个梳子的长度为1,即每个梳子有一个齿。因此,此示例中的输出为1。输入数组a=[5,3,2,1],n=4的另一个例子,其中序列是递减的,这意味着那里没有梳子,因此到最长锚定梳子问题的输出是0。
另一个输入a[1,3,2,11,12,10,11,2,23],n=9。这里,子序列a[1]、a[2]、a[3]、a[4]、a[6]、a[7]、a[8]、a[9]为1、3、2、11、10、11、2、23,是长度为k=4的梳子(有4个齿),但它没有被锚定,因为它的第一个和最后一个齿的第一个元素不相等:a[1]=1和a[8]=2。然而,子序列a[3]、a[4]、a[6]、a[7]、a[8]、a[9]即2、11、10、11、2、23是长度为k=3(有3个齿)的锚定梳齿,因为其第一个和最后一个齿的第一个元素相等:a[3]=2和a[8]=2。这也是本例中最长的锚定梳,因此最长锚定梳问题的输出为3。
3.例如,假设n=10,输入序列为:1 3 2 4 3 5 4 6 1 3,即a[1]=1,a[2]=3,a[3]=2,a[4]=4,a[5]=3,a[6]=5,a[7]=4,a[8]=6,a[9]=1,a[10]=3。然后,例如,a[2]=3,a[4]=4,a[5]=3,a[6]=5是长度为2的锚定梳,但是最长的锚定梳是整个阵列并且具有长度5。

import javax.xml.crypto.Data;
 import java.io.File;
 import java.io.InputStreamReader;
 import java.io.BufferedReader;
 import java.io.FileInputStream;
 import java.lang.reflect.Array;
 import java.util.ArrayList;
 import java.util.Scanner;

public class Main{

    public static ArrayList<String> ReadData(String pathname) {
      ArrayList<String> strlist = new ArrayList<String>();
      try {

        File filename = new File(pathname);
        InputStreamReader reader = new InputStreamReader(
                new FileInputStream(filename));
        BufferedReader br = new BufferedReader(reader);
        int j = 0;
        String line = "";
        while ((line = br.readLine()) != null) {
            strlist.add(line);
        }
        return strlist;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return strlist;
}

public static ArrayList<ArrayList<Integer> > DataWash(ArrayList<String> Datalist) {
    ArrayList<ArrayList<Integer> > AIS = new ArrayList<ArrayList<Integer> >();
    ArrayList<Integer> IS = new ArrayList<Integer>();
    for (int i = 0; i < Datalist.size(); i++) {
        String Tstr = Datalist.get(i);
        if (Tstr.equals("A") == false) {
            IS.add(Integer.parseInt(Tstr));
        }
        if (Tstr.equals("A")) {
            ArrayList<Integer> elemAIS = new ArrayList<Integer>(IS);
            AIS.add(elemAIS);
            IS.clear();
        }
    }
    return AIS;
}

  //%%%%%%%%%%%%%%%%%%%%%%% Procedure LongestComb() that contains code to write. 
    //These codes are to complete:

public static int LongestComb(int[] A, int n){
    /* Input is array of input sequence (a_1 <= a_2 <= ... <= a_n) as A[0,1,...,n-1], that is,
    a_1 = A[0], a_2 = A[1], ..., a_n = A[n-1].
    n = number of integers in sequence A
    This procedure returns the value of the longest anchored comb (>= 1) or 0 if there is no anchored comb. It should not return the respective anchored comb.
    */

    /* 
                   Given an array T[1,...,n] 
                   then M = max_{k: some condition C(k) holds} [ T[k] ],
                   M denotes the largest value T[k] over all indices k such that condition C(k) holds.
    .....
    .....
    */

    /* Here should be the statement and description of the running time
    analysis: describe how the running time depends on
    n (length of the input sequence), and give short argument.

    .....
    ..... 
    */

    /* Here should be the code to solve the problem:

    .....
    .....
    return ...;
    */

} // end of procedure LongestComb()

public static int Computation(ArrayList<Integer> Instance, int opt){
    // opt=1 here means option 1 as in -opt1, and opt=2 means option 2 as in -opt2
    int NGounp = 0;
    int size = 0;
    int Correct = 0;
    size = Instance.size();

    int [] A = new int[size-opt];
    // NGounp = Integer.parseInt((String)Instance.get(0));
    NGounp = Instance.get(0); // NOTE: NGounp = 0 always, as it is NOT used for any purpose
                                       // It is just the first "0" in the first line before instance starts.
    for (int i = opt; i< size;i++ ){
        A[i-opt] = Instance.get(i);
    }
    int Size =A.length;
    if (NGounp >Size ){
        return (-1);
    }
    else {
        //Size
        int R = LongestComb(A,Size);
        return(R);
    }
}

public static String Test;

public static void main(String[] args) {
    if (args.length == 0) {
        String msg = "Rerun with flag: " +
        "\n\t -opt1 to get input from dataOne.txt " + 
        "\n\t -opt2 to check results in dataTwo.txt";
        System.out.println(msg);
        return;
    }
    Test = args[0];
    int opt = 2;
    String pathname = "dataTwo.txt";
    if (Test.equals("-opt1")) {
        opt = 1;
        pathname = "dataOne.txt";
    }

    ArrayList<String> Datalist = new ArrayList<String>();
    Datalist = ReadData(pathname);
    ArrayList<ArrayList<Integer> > AIS = DataWash(Datalist);

    int Nins = AIS.size();
    int NGounp = 0;
    int size = 0;
    if (Test.equals("-opt1")) {
        for (int t = 0; t < Nins; t++) {
            int Correct = 0;
            int Result = 0;
            ArrayList<Integer> Instance = AIS.get(t);
            Result = Computation(Instance, opt);
            System.out.println(Result);
        }
    }
    else {
        int wrong_no = 0;
        int Correct = 0;
        int Result = 0;
        ArrayList<Integer> Wrong = new ArrayList<Integer>();
        for (int t = 0; t < Nins; t++) {
            ArrayList<Integer> Instance = AIS.get(t);
            Result = Computation(Instance, opt);
            System.out.println(Result);
            Correct = Instance.get(1);
            if (Correct != Result) {
                Wrong.add(t+1);
                wrong_no=wrong_no+1;
            }
        }
        if (Wrong.size() > 0) {System.out.println("Index of wrong instance(s):");}
        for (int j = 0; j < Wrong.size(); j++) {
            System.out.print(Wrong.get(j));
            System.out.print(",");

            /*ArrayList Instance = (ArrayList)Wrong.get(j);
            for (int k = 0; k < Instance.size(); k++){
                System.out.println(Instance.get(k));
            }*/
        }
        System.out.println("");
        System.out.println("Percentage of correct answers:");
        System.out.println(((double)(Nins-wrong_no) / (double)Nins)*100);

    }

 }

}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题