长春机械加工厂:一步一步写算法(之字符串查找 下篇)

来源:百度文库 编辑:偶看新闻 时间:2024/05/04 15:46:26

【 声明:版权所有,欢迎转载,请勿用于商业用途。 联系信箱:feixiaoxing @163.com】


前面我们谈到了KMP算法,但是讲的还不是很详细。今天我们可以把这个问题讲的稍微详细一点。假设在字符串A中寻找字符串B,其中字符串B的长度为n,字符串A的长度远大于n,在此我们先忽略。

假设现在开始在字符串A中查找,并且假设双方在第p个字符的时候发现查找出错了,也就是下面的情况:

view plaincopy to clipboardprint?

  1. /*
  2. * A: A1 A2 A3 A4 ... Ap ............
  3. * B: B1 B2 B3 B4 ... Bp ...Bn
  4. * (p)
  5. */

那么这时候,A有什么选择呢?它可以左移1位,用A2~A(p-1)比较B1~B(p-2),然后再用A(p)~A(n+1)比较B(p-1)~B(n)位;或者左移2位,用A3~A(p-1)比较B1~B(p-3),然后再用A(p)~A(n+2)比较B(p-2)~B(n)位; 依次类推,直到左移(p-2)位,用A(p-1)比较B(1),然后再用A(p)~A(p+n-2)比较B(2)~B(n)位。

不知道细心的朋友们发现什么规律没?因为A和B在前面(p-1)个数据是相等的,所以上面的计算其实可以这样看:用A2~A(p-1)比较B1~B(p-2),实际上就是B2~B(p-1)比较B1~B(p-2); 用A3~A(p-1)比较B1~B(p-3),实际上就是B3~B(p-1)比较B1~B(p-3);最后直到B(p)和B(1)两者相比较。既然这些数据都是B自身的数据,所以当然我们可以提前把这些结果都算出来的。

那么这么多的选择,我们应该左移多少位呢?

其实判断很简单。假设我们左移1位,发现A2~A(p-1)的结果和B1~B(p-2)是一致的,那么两者可以直接从第(p-1)位开始比较了; 如果不成功呢,那么只能左移2位,并判断A2~A(p-1)和B1~B(p-2)的比较结果了,......,这样以此类推进行比较。如果不幸发现所有的数据都不能比较成功呢,那么只能从头再来,从第1位数据依次进行比较了。

不知道讲清楚了没,还没有明白的朋友可以看看下面的代码:

view plaincopy to clipboardprint?

  1. int calculate_for_special_index(char str[], int index)
  2. {
  3. int loop;
  4. int value;
  5. value = 0;
  6. for(loop = 1; loop < index; loop ++){
  7. if(!strncmp(&str[loop], str, (index - loop))){
  8. value = index - loop;
  9. break;
  10. }
  11. }
  12. return (value == 0) ? 1 : (index - value);
  13. }
  14. void calculate_for_max_positon(char str[], int len, int data[])
  15. {
  16. int index;
  17. for(index = 0; index < len; index++)
  18. data[index] = calculate_for_special_index(str, index);
  19. }

当然,上面当然都是为了计算在索引n比较失败的时候,判断此时字符应该向左移动多少位。

view plaincopy to clipboardprint?

  1. char* strstr_kmp(const char* str, char* data)
  2. {
  3. int index;
  4. int len;
  5. int value;
  6. int* pData;
  7. if(NULL == str || NULL == str)
  8. return NULL;
  9. len = strlen(data);
  10. pData = (int*)malloc(len * sizeof(int));
  11. memset(pData, 0, len * sizeof(int));
  12. calculate_for_max_positon((char*)str, len, pData);
  13. index = 0;
  14. while(*str && ((int)strlen(str) >= len)){
  15. for(; index < len; index ++){
  16. if(str[index] != data[index])
  17. break;
  18. }
  19. if(index == len){
  20. free(pData);
  21. return (char*) str;
  22. }
  23. value = pData[index];
  24. str += value;
  25. if(value == 1)
  26. index = 0;
  27. else
  28. index = index -value;
  29. }
  30. free(pData);
  31. return NULL;
  32. }

可能朋友们看到了,上面的strlen又回来了?说明代码本身还有优化的空间。大家可以自己先试一试。

view plaincopy to clipboardprint?

  1. int check_valid_for_kmp(char str[], int start, int len)
  2. {
  3. int index;
  4. for(index = start; index < len; index++)
  5. if('\0' == str[index])
  6. return 0;
  7. return 1;
  8. }
  9. char* strstr_kmp(const char* str, char* data)
  10. {
  11. int index;
  12. int len;
  13. int value;
  14. int* pData;
  15. if(NULL == str || NULL == str)
  16. return NULL;
  17. len = strlen(data);
  18. pData = (int*)malloc(len * sizeof(int));
  19. memset(pData, 0, len * sizeof(int));
  20. calculate_for_max_positon((char*)str, len, pData);
  21. index = 0;
  22. while(*str && check_valid_for_kmp((char*)str, index, len)){
  23. for(; index < len; index ++){
  24. if(str[index] != data[index])
  25. break;
  26. }
  27. if(index == len){
  28. free(pData);
  29. return (char*) str;
  30. }
  31. value = pData[index];
  32. str += value;
  33. if(value == 1)
  34. index = 0;
  35. else
  36. index = index -value;
  37. }
  38. free(pData);
  39. return NULL;
  40. }

(三)、多核查找

多核查找其实不新鲜,就是把查找分成多份,不同的查找过程在不同的核上面完成。举例来说,我们现在使用的cpu一般是双核cpu,那么我们可以把待查找的字符分成两份,这样两份查找就可以分别在两个核上面同时进行了。具体怎么做呢,其实不复杂。首先我们要定义一个数据结构:

view plaincopy to clipboardprint?

  1. typedef struct _STRING_PART
  2. {
  3. char * str;
  4. int len;
  5. }STRING_PART;
接着,我们要做的就是把字符串分成两等分,分别运算起始地址和长度。

view plaincopy to clipboardprint?

  1. void set_value_for_string_part(char str[], int len, STRING_PART part[])
  2. {
  3. char* middle = str + (len >> 1);
  4. while(' ' != *middle)
  5. middle --;
  6. part[0].str = str;
  7. part[0].len = middle - (str -1);
  8. part[1].str = middle + 1;
  9. part[1].len = len - (middle - (str - 1));
  10. }
分好之后,就可以开始并行运算了。

view plaincopy to clipboardprint?

  1. char* strstr_omp(char str[], char data[])
  2. {
  3. int index;
  4. STRING_PART part[2] = {0};
  5. char* result[2] = {0};
  6. int len = strlen(str);
  7. set_value_for_string_part(str, len, part);
  8. #pragma omp parellel for
  9. for(index = 0; index < 2; index ++)
  10. result[index] = strstr(part[index].str, part[index].len, data);
  11. if(NULL == result[0] && NULL == result[1])
  12. return NULL;
  13. return (NULL != result[0]) ? result[0] : result[1];
  14. }
注意事项:

(1)这里omp宏要在VS2005或者更高的版本上面才能运行,同时需要添加头文件#include,打开openmp的开关;

(2)这里调用的strstr函数第2个参数是目标字符串的长度,和我们前面介绍的普通查找函数稍微不一样,前面的函数不能直接使用,但稍作改变即可。