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

介绍我就不说了,这里给出c代码供参考

#include "stdio.h"
#include

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;
}
 

你可能感兴趣的:(算法与数据结构)