// TODO: use linked list in place of arraylist. It may get faster.
  private void findAnchorWords() {

    // intersect unique words in the ground truth and ocr output
    anchorsRef = new ArrayList<IndexEntry>(MAX_NUMBER_OF_CANDIDATE_ANCHORS);
    anchorsCand = new ArrayList<IndexEntry>(MAX_NUMBER_OF_CANDIDATE_ANCHORS);
    ArrayList<IndexEntry> newAnchorsRef =
        new ArrayList<IndexEntry>(MAX_NUMBER_OF_CANDIDATE_ANCHORS);
    ArrayList<IndexEntry> newAnchorsCand =
        new ArrayList<IndexEntry>(MAX_NUMBER_OF_CANDIDATE_ANCHORS);

    int i = 0;
    int startRef, endRef, startCand, endCand;
    boolean start = true;

    do {
      if (start) { // initial condition: input sequences are considered to be two big segments.
        startRef = 0;
        endRef = refIndex.getNumOfTokens();
        startCand = 0;
        endCand = ocrIndex.getNumOfTokens();
        i--; // not to skip the first subsegment in the list
        start = false;
      } else {

        // if the first iteration could not find any anchor words, then simply quit.
        if (anchorsRef.isEmpty()) {
          break;
        }

        // RECURSIVE STAGE
        // INDEX SUB-SEGMENTS FOR MORE ANCHOR WORDS
        if (i == 0) { // first segment
          startRef = 0;
          endRef = anchorsRef.get(i).getPosInt();
          startCand = 0;
          endCand = anchorsCand.get(i).getPosInt();
        } // last segment
        else if (i == anchorsRef.size()) {
          startRef = anchorsRef.get(i - 1).getPosInt() + 1;
          endRef = refIndex.getNumOfTokens();
          startCand = anchorsCand.get(i - 1).getPosInt() + 1;
          endCand = ocrIndex.getNumOfTokens();
        } else { // inbetween segments
          startRef = anchorsRef.get(i - 1).getPosInt() + 1;
          endRef = anchorsRef.get(i).getPosInt();
          startCand = anchorsCand.get(i - 1).getPosInt() + 1;
          endCand = anchorsCand.get(i).getPosInt();
        }
      }

      // if one of the segments is still large, go on recursive operations.
      if (((long) (endRef - startRef)) > MAX_SEGMENT_LENGTH
          || ((long) (endCand - startCand)) > MAX_SEGMENT_LENGTH) {
        newAnchorsRef.clear();
        newAnchorsCand.clear();
        findCommonUniqueWords(startRef, endRef, startCand, endCand, newAnchorsRef, newAnchorsCand);

        // if there are no common unique words, then do not try to chop it down
        if (newAnchorsRef.isEmpty()) {
          if (i < anchorsRef.size()) {
            i++; // skip over the segment
            continue;
          } else {
            break; // if it is the last segment, quit anchor word search
          }
        }
      } else { // if the segment is small enough to use edit distance based alignment, then skip
               // over
        if (i < anchorsRef.size()) {
          i++;
          continue;
        } else {
          break; //  if it is the last segment, quit anchor word search
        }
      }

      int LCSindices[] = LCS.findLCS(newAnchorsRef, newAnchorsCand);
      int lcsLength = LCSindices.length / 2;

      // use achors if and only if they are in the longest common subsequence
      ArrayList<IndexEntry> newAnchorsRefRefined = new ArrayList<IndexEntry>();
      ArrayList<IndexEntry> newAnchorsCandRefined = new ArrayList<IndexEntry>();

      for (int kk = 0; kk < lcsLength; kk++) {
        IndexEntry refEntry = newAnchorsRef.get(LCSindices[kk]);
        IndexEntry candEntry = newAnchorsCand.get(LCSindices[(kk + lcsLength)]);

        newAnchorsRefRefined.add(refEntry);
        newAnchorsCandRefined.add(candEntry);
      }
      newAnchorsRef = newAnchorsRefRefined;
      newAnchorsCand = newAnchorsCandRefined;

      if (lcsLength > 0) {
        if (i == -1) { // for the first iteration
          anchorsRef.addAll(newAnchorsRef);
          anchorsCand.addAll(newAnchorsCand);
        } else {
          anchorsRef.addAll(i, newAnchorsRef);
          anchorsCand.addAll(i, newAnchorsCand);
          i--;
        }
      }
      i++;
      // break; // test - stop recursion at the first stage
    } while (i <= anchorsRef.size());
  }
  public static void main(String[] args) {
    if (args.length != 2) {
      System.err.println("Please pass the names of the matrix and string files as parameters.");
      System.exit(1);
    }

    long start, end;

    System.out.println("*** PROBLEM #1: Largest Square Submatrix of 1's");
    try {
      int[][] matrix = readMatrix(args[0]);

      start = System.currentTimeMillis();
      LSS.findLSS(matrix);
      end = System.currentTimeMillis();
      System.out.println("(It took " + (end - start) + " ms.)");
    } catch (FileNotFoundException ex) {
      System.err.println("The file: '" + args[0] + "' was not found.");
    }

    System.out.println();

    System.out.println("*** PROBLEM #2: Longest Common Subsequence of two strings");
    try {
      String[] strings = readLCSWords(args[1]);

      System.out.println("The lonesg common subsequence between the two words in the file is:");

      start = System.currentTimeMillis();
      String lcs = LCS.calcLCS(strings[0], strings[1]);
      end = System.currentTimeMillis();

      System.out.println(lcs);
      System.out.println("The LCS is of size " + lcs.length());
      System.out.println("(It took " + (end - start) + " ms.)");
    } catch (FileNotFoundException ex) {
      System.err.println("The file: '" + args[1] + "' was not found.");
    } catch (IllegalArgumentException ex) {
      System.err.println(ex.getMessage());
    }

    System.out.println();

    System.out.println("*** PROBLEM #3: Optimal Coin Change");
    final List<Integer> coins = new ArrayList<>();
    coins.add(1);
    coins.add(5);
    coins.add(10);
    coins.add(18);
    coins.add(25);
    OptimalChangeCalc calc = new OptimalChangeCalc(coins);

    start = System.currentTimeMillis();
    calc.calcOptimalChange(100);
    end = System.currentTimeMillis();

    System.out.println("Coins utilized: 1, 5, 10, 18, 25");
    System.out.println(
        "The following is a table of cents along with the list "
            + " of minimum coins needed to make that change using the coins above.");
    calc.printOptimalChange();
    System.out.println("(It took " + (end - start) + " ms.)");
  }
Example #3
0
 public static void main(String[] args) {
   String a = "ABCBDAB";
   String b = "BDCABA";
   int lcs = LCS.lcs_length(a, b);
   System.out.println(lcs);
 }
Example #4
0
 public static void main(String[] args) {
   char[] x = {' ', 'A', 'B', 'C', 'B', 'D', 'A', 'B'};
   char[] y = {' ', 'B', 'D', 'C', 'A', 'B', 'A'};
   LCS lcs = new LCS();
   lcs.printLCS(lcs.lcsLength(x, y), x, x.length - 1, y.length - 1);
 }