/********************************************************************* COURSE: CSE 5311 ASSIGNMENT: HW 7 NAME: TO TSUI ID: 636-42-3258 FILED AS: test.java IMPLEMENTED BY: TO TSUI & ZHEN LU COMPILING COMMAND: This java program is lmplemented to run under windows Visual Studio environment. Please contact me for details. **********************************************************************/ /* A applet class to demonstrate a string matching algorithm The algorithm used can be specified by using the "alg" attribute.When you click on a applet,a thread is forked which animates the string matching algorithm. */ import java.awt.*; import java.io.InputStream; import java.util.Hashtable; import java.net.*; public class test extends java.applet.Applet implements Runnable { private Thread kicker; /* The thread which is matching */ StringBuffer arbit =new StringBuffer("12111131212312122121332321212121221133121312121231"); StringBuffer search=new StringBuffer("1212"); Font font; String alphabeta="123"; String blanks=" "; int position; int arr[]; int h1 = -1; int h2 = -1; int old=0; String algName; int counter[]=new int[20]; MatchAlgorithm algorithm; void scramble() { int sizer; int guess,looper; sizer=50; for(looper=0;looper0) && !((target.toString()).regionMatches(loop1+1-1,(target.toString()),q,1))){ loop1=prefix[loop1-1]; } if( (target.toString()).regionMatches(loop1+1-1,(target.toString()),q,1)){ loop1=loop1+1; } prefix[q]=loop1; } /*----------Reversing the String--------------------------*/ for(loop1=target_len-1;loop1>=0;loop1--){ target_r.append(target.toString().charAt(loop1)); } /*--------Prefix of the reverse---------------------*/ loop1=0; for(q=1;q0) && !((target_r.toString()).regionMatches(loop1+1-1,(target_r.toString()),q,1))){ loop1=prefix_r[loop1-1]; } if( (target_r.toString()).regionMatches(loop1+1-1,(target_r.toString()),q,1)){ loop1=loop1+1; } prefix_r[q]=loop1; } /*----------Computing the suffix function----------------*/ for(j=0;j<=target_len+1;j++){ suffix[j]=target_len -prefix[target_len-1]; } for(i=1;i<=target_len;i++){ j=target_len-prefix_r[i-1]; if(suffix[j] > (i- prefix_r[i-1])){ suffix[j]=i-prefix_r[i-1]; } } /*-----------computing the bad character heuristic---------------*/ for(i=0;i0)&&(target.toString().regionMatches(j-1,source.toString(),loop2+j-1,1))){ j=j-1; } if(j==0){ counter[1]=0; counter[0]=loop2*6; pause(1,1); loop2+=suffix[0]; } else { counter[0]=loop2*6; pause(1,1); for(loop1=0;loop1= i){ loop2+=suffix[j]; } else {loop2+=i;} } } } } class BruteMatchAlgorithm extends MatchAlgorithm{ void match(int counter[],StringBuffer source,StringBuffer target,String alphabet) throws Exception{ counter[0]=0; int source_len=source.length(); int target_len=target.length(); int range=source_len-target_len; int tag=0; int index; for(int j=0; j<= range;j++){ if(stopRequested){ return; } counter[1]=0; for(int loop2=0;loop2 < target_len;loop2++){ index=tag+loop2; if(!(source.toString()).regionMatches(index,(target.toString()),loop2,1)){ counter[1]=1; loop2= target_len+5; break; } } counter[0]=(tag*6)%900; tag=tag+1; pause(1,1); counter[1]=0; } } } class KMPAlgorithm extends MatchAlgorithm{ void match(int counter[],StringBuffer source,StringBuffer target,String alphabet) throws Exception{ counter[0]=0; int source_len=source.length(); int target_len=target.length(); int range=source_len-target_len; int loop1,q; int i; int index; int prefix[] = new int[100]; prefix[1-1]=0; loop1=0; for(q=1;q0) && !((target.toString()).regionMatches(loop1+1-1,(target.toString()),q,1))){ loop1=prefix[loop1-1]; } if( (target.toString()).regionMatches(loop1+1-1,(target.toString()),q,1)){ loop1=loop1+1; } prefix[q]=loop1; } q=0; for(i=0; i0) && !(target.toString().regionMatches(q,source.toString(),i,1))){ q=prefix[q-1];} if(target.toString().regionMatches(q,source.toString(),i,1)){ q=q+1;} counter[0]=((i-q+1)*6); if (((i-q)< range)&& (q != target_len)){ pause(1,1);} if (q == target_len){ counter[1]=0; counter[0]=((i-target_len+1)*6); pause(1,1); q=prefix[q-1]; } } } }