Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
indy256 committed Oct 11, 2014
1 parent 92edcff commit 46d8296
Showing 1 changed file with 30 additions and 27 deletions.
57 changes: 30 additions & 27 deletions java/src/SuffixAutomaton.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ public class SuffixAutomaton {

public static class State {
int length;
int link;
int suffLink;
int[] next = new int[128];

{
Expand All @@ -19,7 +19,7 @@ public static State[] buildSuffixAutomaton(String s) {
int n = s.length();
State[] st = new State[Math.max(2, 2 * n - 1)];
st[0] = new State();
st[0].link = -1;
st[0].suffLink = -1;
st[0].endpos = -1;
int last = 0;
int size = 1;
Expand All @@ -29,32 +29,32 @@ public static State[] buildSuffixAutomaton(String s) {
st[cur].length = st[last].length + 1;
st[cur].endpos = st[last].length;
int p;
for (p = last; p != -1 && st[p].next[c] == -1; p = st[p].link) {
for (p = last; p != -1 && st[p].next[c] == -1; p = st[p].suffLink) {
st[p].next[c] = cur;
}
if (p == -1) {
st[cur].link = 0;
st[cur].suffLink = 0;
} else {
int q = st[p].next[c];
if (st[p].length + 1 == st[q].length)
st[cur].link = q;
st[cur].suffLink = q;
else {
int clone = size++;
st[clone] = new State();
st[clone].length = st[p].length + 1;
st[clone].next = st[q].next.clone();
st[clone].link = st[q].link;
for (; p != -1 && st[p].next[c] == q; p = st[p].link)
st[clone].suffLink = st[q].suffLink;
for (; p != -1 && st[p].next[c] == q; p = st[p].suffLink)
st[p].next[c] = clone;
st[q].link = clone;
st[cur].link = clone;
st[q].suffLink = clone;
st[cur].suffLink = clone;
st[clone].endpos = -1;
}
}
last = cur;
}
for (int i = 1; i < size; i++) {
st[st[i].link].ilink.add(i);
st[st[i].suffLink].ilink.add(i);
}
return Arrays.copyOf(st, size);
}
Expand All @@ -63,14 +63,20 @@ public static State[] buildSuffixAutomaton(String s) {
public static void main(String[] args) {
Random rnd = new Random(1);
for (int step = 0; step < 100_000; step++) {
int n1 = rnd.nextInt(10);
int n2 = rnd.nextInt(10);
String s1 = getRandomString(n1, rnd);
String s2 = getRandomString(n2, rnd);
String res1 = lcs(s1, s2);
int res2 = slowLcs(s1, s2);
int n1 = rnd.nextInt(20);
int n2 = rnd.nextInt(20) + 1;
String a = getRandomString(n1, rnd);
String b = getRandomString(n2, rnd);
String res1 = lcs(a, b);
int res2 = slowLcs(a, b);
if (res1.length() != res2)
throw new RuntimeException();
Integer[] occurrences1 = occurrences(a, b);
List<Integer> occurrences2 = new ArrayList<>();
for (int p = a.indexOf(b); p != -1; p = a.indexOf(b, p + 1))
occurrences2.add(p);
if (!Arrays.equals(occurrences1, occurrences2.toArray(new Integer[occurrences2.size()])))
throw new RuntimeException();
}
}

Expand All @@ -85,7 +91,7 @@ static String lcs(String a, String b) {
for (int i = 0, cur = 0; i < b.length(); ++i) {
char c = b.charAt(i);
if (st[cur].next[c] == -1) {
for (; cur != -1 && st[cur].next[c] == -1; cur = st[cur].link) {
for (; cur != -1 && st[cur].next[c] == -1; cur = st[cur].suffLink) {
}
if (cur == -1) {
cur = 0;
Expand All @@ -105,24 +111,21 @@ static String lcs(String a, String b) {
return b.substring(bestPos - bestLen + 1, bestPos + 1);
}

static int[] occurrences(String haystack, String needle) {
static Integer[] occurrences(String haystack, String needle) {
String common = lcs(haystack, needle);
if (!common.equals(needle))
return new int[0];
return new Integer[0];
List<Integer> list = new ArrayList<>();
dfs(buildSuffixAutomaton(haystack), bestState, needle.length(), list);
int[] res = new int[list.size()];
for (int i = 0; i < res.length; i++)
res[i] = list.get(i);
Arrays.sort(res);
return res;
occurrencesDfs(buildSuffixAutomaton(haystack), bestState, needle.length(), list);
Collections.sort(list);
return list.toArray(new Integer[list.size()]);
}

static void dfs(State[] st, int p, int len, List<Integer> list) {
static void occurrencesDfs(State[] st, int p, int len, List<Integer> list) {
if (st[p].endpos != -1 || p == 0)
list.add(st[p].endpos - len + 1);
for (int x : st[p].ilink)
dfs(st, x, len, list);
occurrencesDfs(st, x, len, list);
}

static int slowLcs(String a, String b) {
Expand Down

0 comments on commit 46d8296

Please sign in to comment.