Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Naive String Matching Algorithm
- * @param cPattern
- * @param cText
- * @param bMatchFirst indicates to match the first occurence
- * @return
- */
- public long[] Naive(char []cPattern,char []cText,boolean bMatchFirst)
- {
- int iComparisons = 0, iMatchCount = 0, iNoOfInstructions = 0;
- long lResult[] = new long[4];
- long lEndTime = 0, lStartTime = System.nanoTime();
- if(cText.length > cPattern.length)
- {
- for (int i = 0; i <= cText.length-cPattern.length; i++) {
- int j = 0, iTemp = i;
- iComparisons++;
- while (j < cPattern.length && cPattern[j] == cText[iTemp]){
- iComparisons++;
- j++;
- iTemp++;
- if(j == cPattern.length){
- iNoOfInstructions++;
- iMatchCount++;
- if(bMatchFirst){
- iNoOfInstructions++;
- lEndTime = System.nanoTime();
- return CreateResultArray(iMatchCount,iComparisons,iNoOfInstructions,lStartTime,lEndTime);
- }
- }
- iNoOfInstructions++;
- }
- iNoOfInstructions++;
- }
- lEndTime = System.nanoTime();
- return CreateResultArray(iMatchCount,iComparisons,iNoOfInstructions,lStartTime,lEndTime);
- }
- else
- {
- return CreateResultArray(iMatchCount,iComparisons,iNoOfInstructions,0,0);
- }
- }
- private long[] CreateResultArray(int iMatchCount,int iComparisons,int iNoOfInstructions,long lStartTime,long lEndTime)
- {
- long lResult[]=new long[4];
- lResult[0]=iMatchCount;
- lResult[1]=iComparisons;
- lResult[2]=iNoOfInstructions;
- lResult[3]=lEndTime-lStartTime;
- return lResult;
- }
Add Comment
Please, Sign In to add comment