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 Generate Date Between Given Range
Iterable to Stream in Java
Spring Boot - Quick Start
Hướng dẫn Java Design Pattern – Strategy
Java 8 Stream findFirst() vs. findAny()
Recommended Package Structure of a Spring Boot Project
Setting a Request Timeout for a Spring REST API
Java Program to Check whether Graph is a Bipartite using 2 Color Algorithm
Overflow and Underflow in Java
Java Program to Implement Find all Back Edges in a Graph
Interface trong Java 8 – Default method và Static method
Notify User of Login From New Device or Location
Java Program for Topological Sorting in Graphs
Java Program to Implement Counting Sort
Java Program to Create a Random Linear Extension for a DAG
Java Program to Implement Adjacency Matrix
Period and Duration in Java
Giới thiệu Swagger – Công cụ document cho RESTfull APIs
Removing all duplicates from a List in Java
Exception Handling in Java
Java Program to Implement ConcurrentSkipListMap API
Giới thiệu Java Service Provider Interface (SPI) – Tạo các ứng dụng Java dễ mở rộng
ETL with Spring Cloud Data Flow
Spring Boot - Enabling Swagger2
Ép kiểu trong Java (Type casting)
Injecting Prototype Beans into a Singleton Instance in Spring
Java Scanner hasNext() vs. hasNextLine()
New in Spring Security OAuth2 – Verify Claims
Hướng dẫn sử dụng String Format trong Java
Rest Web service: Filter và Interceptor với Jersey 2.x (P2)
Implementing a Runnable vs Extending a Thread
Find the Registered Spring Security Filters