
Hi, here's my question.. I am trying to implement pattern matching using brute force algorithm where I am trying to apply four way technique to search a given pattern in a text.in this four way technique I will be splitting text which is of length "n" into two halves i.e.from 0 to n/2 and n/2 to n and i will search for a pattern in both the directions i.e. from left as well as from right on this divided parts..there is also a special case wherein i will be searching at the junction where splitting takes place..now what i want to do this is to run this program as four different and independent threads using multithreading concept in java. As you know processors such as core2quad has four processors so i want these threads to utilise the four processors completely...this is my code for brute force..plz help me out in solving this. i also need the amount of time taken by this program to execute.
import java.lang.*;
import java.io.*;
import java.util.*;
public class bruteforcefourway
{
public static void main(String args[ ])
{
String text1,pattern1;
Scanner s=new Scanner(System.in);
System.out.println("\n Enter the text");
text1=s.nextLine();
System.out.println("\n Enter the pattern");
pattern1=s.nextLine();
brute(text1,pattern1);
}
public static void brute(String text,String pattern)
{
int count=0,i,j;
int n=text.length(); // length of the text
int m=pattern.length(); //length of the pattern
for(i= 0;i<=n/2;i++)
{
j=0;
while((j<m)&&(pattern.charAt(j)==text.charAt(i + j)))
{
j++;
if (j==m)
{
count++;
System.out.printf("\n %d Match found between %d and %d",count,i-1,i+m);
}
}
}
for(i= n/2+1;i<=n;i++)
{
j=0;
while((j<m)&&(pattern.charAt(j)==text.charAt(i + j)))
{
j++;
if (j==m)
{
count++;
System.out.printf("\n %d Match found between %d and %d",count,i-1,i+m);
}
}
}
for(i= n/2-m+1;i<=n/2+m-1;i++)// special case searching at the junction where splitting took place
{
j=0;
while((j<m)&&(pattern.charAt(j)==text.charAt(i + j)))
{
j++;
if (j==m)
{
count++;
System.out.printf("\n %d Match found between %d and %d",count,i-1,i+m);
}
}
}
System.out.printf("\n\n");
System.out.printf("\n Total Matches found are : %d",count);
}
}
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.