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:
How to Iterate Over a Stream With Indices
Java Program to Implement First Fit Decreasing for 1-D Objects and M Bins
A Guide to HashSet in Java
Constructor Dependency Injection in Spring
Java Program to Implement Patricia Trie
Java Program to Sort an Array of 10 Elements Using Heap Sort Algorithm
Java Program to Implement Find all Forward Edges in a Graph
Java Program to Perform Insertion in a BST
Reading an HTTP Response Body as a String in Java
Java Program to Solve a Matching Problem for a Given Specific Case
Introduction to Spring Cloud OpenFeign
Introduction to Project Reactor Bus
Java Program for Topological Sorting in Graphs
Deploy a Spring Boot App to Azure
Java – Convert File to InputStream
Using Java Assertions
Java program to Implement Tree Set
Java Program to Implement vector
Hướng dẫn sử dụng biểu thức chính quy (Regular Expression) trong Java
A Guide to @RepeatedTest in Junit 5
Find the Registered Spring Security Filters
Java Program to Find Strongly Connected Components in Graphs
Running Spring Boot Applications With Minikube
Remove the First Element from a List
Apache Commons Collections SetUtils
Spring Boot - Rest Controller Unit Test
Spring 5 and Servlet 4 – The PushBuilder
Call Methods at Runtime Using Java Reflection
Java Program to Implement Ternary Tree
Add Multiple Items to an Java ArrayList
Remove All Occurrences of a Specific Value from a List
Functional Interface trong Java 8