字符串模式匹配之:Sunday算法

介紹我就不說了,這裏給出c代碼供參考

#include "stdio.h"
#include <string.h>

char *qsearch(const char *text, int n, const char *patt, int m)
{
 // get the length of the text and the pattern, if necessary
 if (n < 0)
  n = strlen(text);
 if (m < 0)
  m = strlen(patt);
 if (m == 0)
  return (char*)text;
  
 // construct delta shift table
 int td[128];
 for (int c = 0; c < 128; c++)
  td[c] = m + 1;
 const char* p;
 for (p=patt; *p; p++)
  td[*p] = m - (p - patt);
  
 // start searching...
 const char *t, *tx = text;
 // the main searching loop
 while ((tx + m) <= (text + n)) {
  for (p = patt, t = tx; *p; ++p, ++t) {
   if (*p != *t) // found a mismatch
    break;
  }
  if (*p == '/0') // Yes! we found it!
   return (char*)tx;
  tx += td[tx[m]]; // move the pattern by a distance
 }
 return NULL;
}
 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章