/* * BinaryStringMatchUI.java * */ public class BinaryStringMatchUI extends javax.swing.JFrame { int m; // length of p - variable name picked because of use in text int n; // length of text - variable name picked because of use in text int [] f; // Failure function as defined in 9.1 int [] bfail; // Failure function as defined in 9.1, modified for binary input char one=' '; // one of two characters in binary pattern char two=' '; // second of two characters in binary pattern String patternString; // pattern to be found in bigString char [] p; // pattern to be found in bigString in char form String bigString; // big String to be searched for patternString char [] big; // bigString in char form /** Creates new form BinaryStringMatchUI */ public BinaryStringMatchUI() { initComponents(); } /** This method is called from within the constructor to * initialize the form. * WARNING: Do NOT modify this code. The content of this method is * always regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // //GEN-BEGIN:initComponents private void initComponents() { jScrollPane1 = new javax.swing.JScrollPane(); OutputFailureFunctions = new javax.swing.JTextArea(); TextStr = new javax.swing.JTextField(); PatternStr = new javax.swing.JTextField(); jLabel1 = new javax.swing.JLabel(); jLabel2 = new javax.swing.JLabel(); RunMatch = new javax.swing.JToggleButton(); jScrollPane3 = new javax.swing.JScrollPane(); OutputArea = new javax.swing.JTextArea(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); OutputFailureFunctions.setColumns(20); OutputFailureFunctions.setRows(5); jScrollPane1.setViewportView(OutputFailureFunctions); TextStr.setText("xxyxxyxxxxyxyxyxxyxxyxyxyxxxyxxyxxyxxxyxyxyxxxyxxyxyxyxy"); TextStr.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { TextStrActionPerformed(evt); } }); PatternStr.setText("xxyxxyxyxyxy"); jLabel1.setText("Text"); jLabel2.setText("Pattern"); RunMatch.setText("RunMatch"); RunMatch.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { RunMatchActionPerformed(evt); } }); OutputArea.setColumns(20); OutputArea.setRows(5); OutputArea.setText("Output"); jScrollPane3.setViewportView(OutputArea); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 481, Short.MAX_VALUE)) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(PatternStr) .addComponent(TextStr, javax.swing.GroupLayout.DEFAULT_SIZE, 390, Short.MAX_VALUE)) .addGap(18, 18, 18) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(jLabel1) .addGap(18, 18, 18) .addComponent(RunMatch)) .addComponent(jLabel2)))) .addGap(0, 0, 0)) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(TextStr, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel1) .addComponent(RunMatch)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(PatternStr, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel2)) .addGap(22, 22, 22) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(34, 34, 34) .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 198, Short.MAX_VALUE)) .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 232, javax.swing.GroupLayout.PREFERRED_SIZE)) .addContainerGap(60, Short.MAX_VALUE)) ); pack(); }// //GEN-END:initComponents private void TextStrActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_TextStrActionPerformed // TODO add your handling code here: }//GEN-LAST:event_TextStrActionPerformed // Determine if the pattern is found in the big string public void findIt() { OutputArea.append("findIt\n"); patternString = PatternStr.getText(); m = patternString.length(); OutputArea.append("Pattern is of length " + m +"\n"); p = new char[m+1]; patternString.getChars(0,m,p,0); bigString = TextStr.getText(); n = bigString.length(); big = new char[n+1]; bigString.getChars(0,n,big,0); bfail = new int[m+1]; f = new int[m+1]; one=two = '*'; long start = System.currentTimeMillis(); if(!checkBinary(p,m) || !checkBinary(big,n)) System.out.println("ERROR not binary"); else { createBfail(); printBfail(); int where = findMatchBruteForce(); if (where >=0) { OutputArea.append("Did find a match\n"); displayString(where,m); } else OutputArea.append("Did NOT find a match\n"); } long elapsedTimeMillis = System.currentTimeMillis()-start; OutputArea.append("\nTime taken " + elapsedTimeMillis+"\n"); } // Print out the big string with the pattern shown between double asterisks public void displayString(int beg, int howMany) { System.out.println(beg + " "+ howMany + " " + n + "\n"); OutputArea.append(bigString.substring(0,beg) + "**"); OutputArea.append(bigString.substring(beg,beg+howMany)+"**"); OutputArea.append(bigString.substring(beg+howMany,bigString.length())); } // Given a character in the pattern, return the other character char opposite(char a){ if (a==one) return two; return one; } // Create the binary fail function bfail public void createBfail() { int i,b; f[0] = 0; for(i=0; i< m-1; i++) { b = f[i]; bfail[i]=0; while(b > 0 && p[i+1] != p[b]) b = f[b-1]; // so b is decreased by at least 1 here; therefore the while loop terminates if(p[i+1] == p[b]) f[i+1] = b+1; else f[i+1] = 0; } } // Make sure the string contains only two characters // Store the two characters as variables one and two public boolean checkBinary( char [] s, int len){ if (one=='*') one =two =s[0]; for (int i=0; i < len; i++) { if (s[i]!= one){ if (two==one) two = s[i]; if (s[i]!=two) { OutputArea.append("checkBinary Value " + s[i] + " different from "+ one + " or " + two +"\n"); return false; } } } return true; } // Return the location of the match of pattern p in text string big // Return -1 if there is no match. public int findMatchBruteForce() { // You need to create an intelligent binary findMatch for (int i=0; i <= n - m; i++) { int j = 0; while (j < m && big[i+j]==p[j]) { j++; } if (j==m) return i; } return -1; } // Print the binary Fail functions public void printBfail(){ OutputFailureFunctions.setTabSize(4); OutputFailureFunctions.append("\ti\tp[i]\tBfail\t F \n"); for (int i=0; i < m; i++) OutputFailureFunctions.append("\t"+ i + "\t" + p[i] + "\t" +bfail[i]+ "\t" + f[i]+"\n"); } private void RunMatchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_RunMatchActionPerformed // TODO add your handling code here: //Add the code to do the String Match OutputArea.setText("String is " + TextStr.getText()+"\n"); OutputArea.append("Pattern is " + PatternStr.getText()+"\n"); patternString = PatternStr.getText(); bigString = TextStr.getText(); System.out.println("Find it " + patternString + "->" + bigString); OutputArea.append("Find it " + patternString + "->" + bigString + "\n"); findIt(); }//GEN-LAST:event_RunMatchActionPerformed /** * @param args the command line arguments */ public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new BinaryStringMatchUI().setVisible(true); } }); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JTextArea OutputArea; private javax.swing.JTextArea OutputFailureFunctions; private javax.swing.JTextField PatternStr; private javax.swing.JToggleButton RunMatch; private javax.swing.JTextField TextStr; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JScrollPane jScrollPane3; // End of variables declaration//GEN-END:variables }