Suffix array solution (java)

Using the suffix array to perform brute-force solution of the suffix array in string matching:
In order to facilitate the recording of the subscript and suffix of the suffix of the original string, we use classes to bind it, and override the compareTo () and toString () methods.

import java.util.Arrays;
import java.util.Scanner;
public class Hzsz {
//后缀数组求解
//利用对象对字符串的原始下标和字串进行绑定
  static  class  suff implements Comparable<suff>{
  String str;//字串
  int index;//原始下标
  suff(String str,int index)//构造器
  {
   this.str=str;
   this.index=index;
  }
  @Override
  public int compareTo(suff arg0) //覆盖compareto方法
  {
  return this.str.compareTo(arg0.str);
  }
   public String toString()
  {
   return "suff{"+"str="+this.str+", index="+this.index+'\n';
  }
 }
  public static suff[] getsa(String a)
  {
   int length1=a.length();
   suff [] b=new suff[length1];
   for(int i=0;i<length1;i++)
   {
   String sta=a.substring(i);
   b[i]=new suff(sta,i);
   }
   Arrays.sort(b);
   return b;
     }
 public static void main(String[] args) {
  Scanner a=new Scanner(System.in);
  String b=a.nextLine();
  suff []c=getsa(b);
  System.out.println(Arrays.toString(c));
 }
 }
 

The results of the use case run are as follows:
Insert picture description here

Published 14 original articles · praised 0 · visits 243

Guess you like

Origin blog.csdn.net/lianggege88/article/details/105220172