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 – Create a File
Send an email using the SMTP protocol
Java Program to Find Second Smallest of n Elements with Given Complexity Constraint
Quick Guide to @RestClientTest in Spring Boot
Object cloning trong java
Java Program to Check whether Graph is a Bipartite using BFS
Java Program to Implement Splay Tree
Introduction to Thread Pools in Java
New Features in Java 15
Java Program to Implement Sieve Of Sundaram
Adding a Newline Character to a String in Java
Java Program to Implement WeakHashMap API
Java Program to Implement ConcurrentHashMap API
Java Program to Apply DFS to Perform the Topological Sorting of a Directed Acyclic Graph
Static Content in Spring WebFlux
Introduction to Spring Cloud OpenFeign
ETags for REST with Spring
HttpAsyncClient Tutorial
Automatic Property Expansion with Spring Boot
Programmatic Transaction Management in Spring
Java Program to Find the Shortest Path from Source Vertex to All Other Vertices in Linear Time
Converting String to Stream of chars
Java Program to implement Bit Matrix
Wiring in Spring: @Autowired, @Resource and @Inject
Java Program to Implement Quick Hull Algorithm to Find Convex Hull
Java Program to Find Inverse of a Matrix
Hướng dẫn Java Design Pattern – Adapter
Spring MVC Async vs Spring WebFlux
Kiểu dữ liệu Ngày Giờ (Date Time) trong java
Spring Boot - Google OAuth2 Sign-In
Remove the First Element from a List
Java Program to Perform the Shaker Sort