Guest User

Untitled

a guest
Nov 19th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.83 KB | None | 0 0
  1. /**
  2. * Naive String Matching Algorithm
  3. * @param cPattern
  4. * @param cText
  5. * @param bMatchFirst indicates to match the first occurence
  6. * @return
  7. */
  8. public long[] Naive(char []cPattern,char []cText,boolean bMatchFirst)
  9. {
  10. int iComparisons = 0, iMatchCount = 0, iNoOfInstructions = 0;
  11. long lResult[] = new long[4];
  12. long lEndTime = 0, lStartTime = System.nanoTime();
  13. if(cText.length > cPattern.length)
  14. {
  15. for (int i = 0; i <= cText.length-cPattern.length; i++) {
  16. int j = 0, iTemp = i;
  17. iComparisons++;
  18. while (j < cPattern.length && cPattern[j] == cText[iTemp]){
  19. iComparisons++;
  20. j++;
  21. iTemp++;
  22. if(j == cPattern.length){
  23. iNoOfInstructions++;
  24. iMatchCount++;
  25. if(bMatchFirst){
  26. iNoOfInstructions++;
  27. lEndTime = System.nanoTime();
  28. return CreateResultArray(iMatchCount,iComparisons,iNoOfInstructions,lStartTime,lEndTime);
  29. }
  30. }
  31. iNoOfInstructions++;
  32. }
  33. iNoOfInstructions++;
  34. }
  35. lEndTime = System.nanoTime();
  36. return CreateResultArray(iMatchCount,iComparisons,iNoOfInstructions,lStartTime,lEndTime);
  37. }
  38. else
  39. {
  40. return CreateResultArray(iMatchCount,iComparisons,iNoOfInstructions,0,0);
  41. }
  42.  
  43. }
  44.  
  45. private long[] CreateResultArray(int iMatchCount,int iComparisons,int iNoOfInstructions,long lStartTime,long lEndTime)
  46. {
  47. long lResult[]=new long[4];
  48. lResult[0]=iMatchCount;
  49. lResult[1]=iComparisons;
  50. lResult[2]=iNoOfInstructions;
  51. lResult[3]=lEndTime-lStartTime;
  52. return lResult;
  53.  
  54. }
Add Comment
Please, Sign In to add comment