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 Implement Red Black Tree
Guide to the Java ArrayList
Getting Started with Custom Deserialization in Jackson
Converting a Stack Trace to a String in Java
Thực thi nhiều tác vụ cùng lúc như thế nào trong Java?
How to Use if/else Logic in Java 8 Streams
Java Program to Implement Ternary Search Algorithm
Spring Boot - Batch Service
Spring WebClient vs. RestTemplate
Java Program to Implement Repeated Squaring Algorithm
Receive email by java client
Java 8 and Infinite Streams
Display Auto-Configuration Report in Spring Boot
Mapping a Dynamic JSON Object with Jackson
Auditing with JPA, Hibernate, and Spring Data JPA
Tạo ứng dụng Java RESTful Client không sử dụng 3rd party libraries
Java Program to Perform Naive String Matching
Guide to the Volatile Keyword in Java
Java Program to Check for balanced parenthesis by using Stacks
MyBatis with Spring
Java Program to Solve TSP Using Minimum Spanning Trees
Java Program to Implement Pairing Heap
Java Program to Implement Graham Scan Algorithm to Find the Convex Hull
Anonymous Classes in Java
Java Streams vs Vavr Streams
Getting Started with Forms in Spring MVC
Spring MVC Content Negotiation
Java Program to Search for an Element in a Binary Search Tree
Java Program to Check if a Given Graph Contain Hamiltonian Cycle or Not
Testing in Spring Boot
Các chương trình minh họa sử dụng Cấu trúc điều khiển trong Java
Hướng dẫn sử dụng Java Annotation