This Java program is to Implement Suffix arrayIn computer science, a suffix array is a sorted array of all suffixes of a string. It is a simple, yet powerful data structure which is used, among others, in full text indices, data compression algorithms and within the field of bioinformatics.
Here is the source code of the Java program to implement suffix array. The Java program is successfully compiled and run on a Linux system. The program output is also shown below.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class SuffixArray
{
private String[] text;
private int length;
private int[] index;
private String[] suffix;
public SuffixArray(String text)
{
this.text = new String[text.length()];
for (int i = 0; i < text.length(); i++)
{
this.text[i] = text.substring(i, i+1);
}
this.length = text.length();
this.index = new int[length];
for (int i = 0; i < length; i++)
{
index[i] = i;
}
suffix = new String[length];
}
public void createSuffixArray()
{
for(int index = 0; index < length; index++)
{
String text = "";
for (int text_index = index; text_index < length; text_index++)
{
text+=this.text[text_index];
}
suffix[index] = text;
}
int back;
for (int iteration = 1; iteration < length; iteration++)
{
String key = suffix[iteration];
int keyindex = index[iteration];
for (back = iteration - 1; back >= 0; back--)
{
if (suffix[back].compareTo(key) > 0)
{
suffix[back + 1] = suffix[back];
index[back + 1] = index[back];
}
else
{
break;
}
}
suffix[ back + 1 ] = key;
index[back + 1 ] = keyindex;
}
System.out.println("SUFFIX \t INDEX");
for (int iterate = 0; iterate < length; iterate++)
{
System.out.println(suffix[iterate] + "\t" + index[iterate]);
}
}
public static void main(String...arg)throws IOException
{
String text = "";
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the Text String ");
text = reader.readLine();
SuffixArray suffixarray = new SuffixArray(text);
suffixarray.createSuffixArray();
}
}
$javac SuffixArray.java $java SuffixArray Enter the Text String banana SUFFIX INDEX a 5 ana 3 anana 1 banana 0 na 4 nana 2
Related posts:
Java Program to Construct an Expression Tree for an Postfix Expression
Tạo ứng dụng Java RESTful Client với thư viện OkHttp
Hướng dẫn tạo và sử dụng ThreadPool trong Java
Java Program to Implement Interpolation Search Algorithm
Merging Streams in Java
Intro to Inversion of Control and Dependency Injection with Spring
Java Program to Check if a Matrix is Invertible
Examine the internal DNS cache
Java Program to Find a Good Feedback Vertex Set
Java Program to Find Number of Spanning Trees in a Complete Bipartite Graph
Apache Commons Collections Bag
Apache Commons Collections SetUtils
Interface trong Java 8 – Default method và Static method
Java Program to Implement Variable length array
Spring Boot - Internationalization
Getting a File’s Mime Type in Java
Java Program to Solve the 0-1 Knapsack Problem
Using Optional with Jackson
Hướng dẫn Java Design Pattern – Command
Java Timer
Spring Security Registration – Resend Verification Email
Collect a Java Stream to an Immutable Collection
Send email with JavaMail
Java Program to Delete a Particular Node in a Tree Without Using Recursion
Guide to DelayQueue
Giới thiệu luồng vào ra (I/O) trong Java
Transactions with Spring and JPA
Java Program to Implement Gauss Seidel Method
Java Program to Find MST (Minimum Spanning Tree) using Prim’s Algorithm
Removing all duplicates from a List in Java
Hướng dẫn Java Design Pattern – Builder
Java Optional as Return Type