0

I have been following a tutorial I found. It is however in C++ and I'm using Java so there might have been a few things lost in translation. I've tried both googling and searching here and while there seem to be plenty of asked questions I still remain stuck. Though it feels like I'm very close.

According to the tutorial, there should be a match for the pattern 'nan' but there simply is no match when I'm running it. What am I missing? Sorry for code that unformated itself when pasted.

package u1;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Arrays;
import java.util.Scanner;

public class SuffixSort {
    public Element[] processPattern(String pattern) {
    Element[] patternArray = new Element[pattern.length()];
    for (int i = 0; i < pattern.length(); i++) {
        patternArray[i] = new Element(i, pattern.substring(i, pattern.length()));
    }

    Arrays.sort(patternArray);

    return patternArray;
}

public void binarySearch(String text, String pattern, Element[] array) {
    int left = 0, right = text.length() - 1;
    int mid = 0, result;

    while (left <= right) {
        mid = left + (right - left) / 2;
        result = pattern.compareTo(array[mid].getSuffix());

        if (result == 0) {
            System.out.println("Match: " + array[mid].getIndex());
            return;
        } else if (result < 0) {
            right = mid - 1;
        } else {
            left = mid + 1;
        }
    }
}

public static void main(String[] args) {
    try {
        String text = "banana";
        String pattern = "nan";
        SuffixSort ss = new SuffixSort();
        Scanner in = new Scanner(new FileReader("src/resources/100k.txt"));

        /*
         * while (in.hasNextLine()) { text += in.nextLine(); }
         */

        Element[] suffixArray = ss.processPattern(text);

        double runtime = System.nanoTime();
        ss.binarySearch(text, pattern, suffixArray);
        runtime = (System.nanoTime() - runtime) / 1000000;

        in.close();

        System.out.println(runtime);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
}
}

Other class

package u1;

public class Element implements Comparable<Element>{
private int index;
private String suffix;

public Element(int index, String suffix){
    this.index = index;
    this.suffix = suffix;
}

@Override
public int compareTo(Element o) {
    return this.getSuffix().compareTo(o.getSuffix());
}

public int getIndex() {
    return index;
}

public String getSuffix() {
    return suffix;
}

public void setSuffix(String suffix) {
    this.suffix = suffix;
}
}
Nick Z
  • 1
  • Have you tried stepping through with a debugger? Looking at both code sections I see no immediate differences. Also, the `1`s and `l`s look very similar on that site (at least for me). Maybe its something there. – Obicere Jan 14 '17 at 01:56
  • Actually, it probably is because you compare suffixes differently than they define: `return strcmp(a.suff, b.suff) < 0? 1 : 0;`. Here, they basically use an inverse comparison. – Obicere Jan 14 '17 at 01:58
  • I don't understand that line. How do I translate that into Java? – Nick Z Jan 15 '17 at 03:12
  • It should be the exact same! `Comparable#compareTo` returns an `int`. So if you apply the same logic, you should get the same result. – Obicere Jan 15 '17 at 03:15
  • I replaced the comparison method in the Element class: return this.getSuffix().compareTo(o.getSuffix()) < 0? 1 : 0; It still doesn't find 'nan' in 'banana,' it does find the suffixes themselves though, like 'ana.' – Nick Z Jan 15 '17 at 18:38

0 Answers0