Finite automation machine:
Pseudo code:
Java code:
/**
* Created with IntelliJ IDEA.
* User: 1O1O
* Date: 2015-03-31
* Time: 21:29 PM
* :)~
* FINITE-AUTOMATON-MATCH:STRING-MATCH
*/
public class Main {
public static int[][] COMPUTE_TRANSITION_FUNCTION(String Pattern, char[] sigma){
int m = Pattern.length();
int n = sigma.length;
int[][] delta = new int[m+1][n];
for(int q=0; q<=m; q++){
for(int i=0; i<n; i++){
int k = ((m+1)<=(q+2))?(m+1):(q+2);
do{
k = k-1;
if(k == 0){
delta[q][i] = 0;
break;
}
String Pk = Pattern.substring(0, k);
String Pq = Pattern.substring(0, q) + sigma[i];
int Pq_len = Pq.length();
int j;
for(j=k-1; j>-1; j--){
if(Pk.charAt(j) == Pq.charAt(--Pq_len))
continue;
else
break;
}
if(j == -1){
delta[q][i] = k;
break;
}
}while(true);
}
}
//Format Output:
System.out.println("Stat a b c P");
for(int i=0; i<=m; i++){
System.out.print(String.format("%-6s", i));
for(int j=0; j<n; j++){
System.out.print(String.format("%-6s", delta[i][j]));
}
if(i != m)
System.out.print(Pattern.charAt(i));
System.out.println();
}
return delta;
}
public static void FINITE_AUTOMATON_MATCHER(String Text, int[][] delta, int m){
int n = Text.length();
int q = 0;
int index;
for(int i=0; i<n; i++){
switch(Text.charAt(i)){
case 'a': index = 0; break;
case 'b': index = 1; break;
case 'c': index = 2; break;
default: index = 0;
}
q = delta[q][index];
if(q == m){
System.out.println("Pattern occurs with shift: "+(i-m+2));
}
}
}
public static void main(String[] args) {
String Text = "bacbababaabcbabababacababbababacabbc";
String Pattern = "ababaca";
int m = Pattern.length();
char[] sigma = {'a','b','c'};
int[][] delta = COMPUTE_TRANSITION_FUNCTION(Pattern, sigma);
FINITE_AUTOMATON_MATCHER(Text, delta, m);
}
}
Output:
Stat a b c P
0 1 0 0 a
1 1 2 0 b
2 3 0 0 a
3 1 4 0 b
4 5 0 0 a
5 1 4 6 c
6 7 0 0 a
7 1 2 0
Pattern occurs with shift: 16
Pattern occurs with shift: 27
Reference:
From: 《INTRODUCTION TO ALGORITHMS》THIRD EDITION.