1.trie树
import java. util. ArrayList;
import java. util. Queue;
public class TrieST < Value> {
private static int R = 256 ;
private static class Node {
private Object val;
private Node[ ] next = new Node [ R] ;
}
private Node root;
public Value get ( String key) {
Node x = get ( root, key, 0 ) ;
if ( x == null) return null;
return ( Value) x. val;
}
private Node get ( Node x, String key, int d) {
if ( x == null) return null;
if ( d == key. length ( ) ) return x;
char c = key. charAt ( d) ;
return get ( x. next[ c] , key, d + 1 ) ;
}
public void put ( String key, Value value) {
root = put ( root, key, value, 0 ) ;
}
private Node put ( Node x, String key, Value val, int d) {
if ( x == null) x = new Node ( ) ;
if ( d == key. length ( ) ) {
x. val = val;
return x;
}
char c = key. charAt ( d) ;
x. next[ c] = put ( x. next[ c] , key, val, d + 1 ) ;
return x;
}
public String longestPrefixOf ( String s) {
int length = search ( root, s, 0 , 0 ) ;
return s. substring ( 0 , length) ;
}
private int search ( Node x, String s, int d, int length) {
if ( x == null) return length;
if ( x. val != null) length = d;
if ( d == s. length ( ) ) return length;
char c = s. charAt ( d) ;
return search ( x. next[ c] , s, d + 1 , length) ;
}
public void delete ( String key) {
root = delete ( root, key, 0 ) ;
}
public Node delete ( Node x, String key, int d) {
if ( x == null) return null;
if ( d == key. length ( ) ) {
x. val = null;
} else {
char c = key. charAt ( d) ;
x. next[ c] = delete ( x. next[ c] , key, d + 1 ) ;
}
if ( x. val != null) return x;
for ( char c = 0 ; c < R; c++ ) {
if ( x. next[ c] != null) return x;
}
return null;
}
public int size ( ) {
return size ( root) ;
}
private int size ( Node x) {
if ( x == null) return 0 ;
int cnt = 0 ;
if ( x. val != null) cnt++ ;
for ( char c = 0 ; c < R; c++ ) {
cnt += size ( x. next[ c] ) ;
}
return cnt;
}
public Iterable< String> keys ( String s) {
return keysWithPrefix ( "" ) ;
}
public Iterable< String> keysWithPrefix ( String s) {
ArrayList< String> arrayList = new ArrayList < > ( ) ;
collect ( get ( root, s, 0 ) , s, arrayList) ;
return arrayList;
}
private void collect ( Node x, String pre, ArrayList< String> q) {
if ( x == null) return ;
if ( x. val != null) q. add ( pre) ;
for ( char c = 0 ; c < R; c++ )
collect ( x. next[ c] , pre + c, q) ;
}
public static void main ( String[ ] args) {
TrieST< Integer> st = new TrieST < > ( ) ;
String s = "fdadfa" ;
String pre = "fda" ;
st. put ( pre, 1 ) ;
System. out. println ( st. get ( pre) ) ;
System. out. println ( st. get ( s) ) ;
System. out. println ( st. longestPrefixOf ( s) ) ;
st. delete ( pre) ;
System. out. println ( st. get ( pre) ) ;
}
}
2.KMP
public class KMP {
private String pat;
private int [ ] [ ] dfa;
public KMP ( String pat) {
this . pat = pat;
int M = pat. length ( ) ;
int R = 256 ;
dfa = new int [ R] [ M] ;
dfa[ pat. charAt ( 0 ) ] [ 0 ] = 1 ;
for ( int X = 0 , j = 1 ; j < M; j++ ) {
for ( int c = 0 ; c < R; c++ )
dfa[ c] [ j] = dfa[ c] [ X] ;
dfa[ pat. charAt ( j) ] [ j] = j + 1 ;
X = dfa[ pat. charAt ( j) ] [ X] ;
}
}
public int search ( String txt) {
int i, j, N = txt. length ( ) , M = pat. length ( ) ;
for ( i = 0 , j = 0 ; i < N && j < M; i++ )
j = dfa[ txt. charAt ( i) ] [ j] ;
if ( j == M) return i - M;
else return - 1 ;
}
public static void main ( String[ ] args) {
String pat = args[ 0 ] ;
String txt = args[ 1 ] ;
KMP kmp = new KMP ( pat) ;
System. out. println ( "text: " + txt) ;
int offset = kmp. search ( txt) ;
System. out. println ( "pattern: " + pat) ;
System. out. println ( offset) ;
}
}
3.TST树
import java. util. ArrayList;
public class TST < Value> {
private class Node {
char c;
Node left, mid, right;
Value value;
}
private Node root;
public Value get ( String key) {
Node x = get ( root, key, 0 ) ;
if ( x == null) return null;
return ( Value) x. value;
}
private Node get ( Node x, String key, int d) {
if ( x == null) return null;
char c = key. charAt ( d) ;
if ( c < x. c) return get ( x. left, key, d) ;
else if ( c > x. c) return get ( x. right, key, d) ;
else if ( d < key. length ( ) - 1 ) {
return get ( x. mid, key, d + 1 ) ;
}
return x;
}
public void put ( String key, Value value) {
root = put ( root, key, value, 0 ) ;
}
private Node put ( Node x, String key, Value value, int d) {
char c = key. charAt ( d) ;
if ( x == null) {
x = new Node ( ) ;
x. c = c;
}
if ( c < x. c) x. left = put ( x. left, key, value, d) ;
else if ( c > x. c) x. right = put ( x. right, key, value, d) ;
else if ( d < key. length ( ) - 1 ) {
x. mid = put ( x. mid, key, value, d + 1 ) ;
} else x. value = value;
return x;
}
public String longestPrefixOf ( String s) {
int length = search ( root, s, 0 , 0 ) ;
return s. substring ( 0 , length) ;
}
public int search ( Node x, String s, int length, int d) {
if ( x == null) return length;
if ( x. value != null) length = d;
if ( d == s. length ( ) ) return length;
char c = s. charAt ( d) ;
if ( c > x. c) return search ( x. right, s, length, d + 1 ) ;
else if ( c < x. c) return search ( x. left, s, length, d + 1 ) ;
else return search ( x. mid, s, length, d + 1 ) ;
}
public ArrayList< String> keys ( ) {
ArrayList< String> s = new ArrayList < > ( ) ;
s = keysWithPrefix ( "" ) ;
return s;
}
public ArrayList< String> keysWithPrefix ( String s) {
ArrayList< String> arrayList = new ArrayList < > ( ) ;
collect ( get ( root, s, 0 ) , s, arrayList) ;
return arrayList;
}
public void collect ( Node x, String s, ArrayList< String> arrayList) {
if ( x == null) return ;
s += x. c;
if ( x. value != null) arrayList. add ( s) ;
collect ( x. left, s, arrayList) ;
collect ( x. mid, s, arrayList) ;
collect ( x. right, s, arrayList) ;
}
public int size ( ) {
return size ( root) ;
}
private int size ( Node x) {
int cnt = 0 ;
if ( x == null) return 0 ;
if ( x. value != null) cnt++ ;
cnt += size ( x. left) ;
cnt += size ( x. mid) ;
cnt += size ( x. right) ;
return cnt;
}
}