Chinaunix首页 | 论坛 | 博客
  • 博客访问: 613895
  • 博文数量: 79
  • 博客积分: 848
  • 博客等级: 军士长
  • 技术积分: 1800
  • 用 户 组: 普通用户
  • 注册时间: 2012-06-26 19:30
文章分类

全部博文(79)

文章存档

2015年(4)

2013年(39)

2012年(36)

分类: C/C++

2013-04-28 22:32:28

要找工作了感觉自己好久没有码代码了,今天看了珠矶就来实现一个上面提到的寻找给定的字符串最大重复子串的算法。
想到这个问题其实大家一般肯定都能想到的是循环查找整个字符串然后比较输出结果,但是在这样的操作中存在大量的无用操作。
好了我们先来看下,常规的方法:

点击(此处)折叠或打开

  1. /*************************************************************************
  2.     > File Name: maxMutiSubStr.cpp
  3.     > Author: dongdaoxiang
  4.     > Func: find the Max Mutiple SubString
  5.     > Mail: dongdaoxiang@ncic.ac.cn
  6.     > Created Time: 2013年04月28日 星期日 13时58分11秒
  7.  ************************************************************************/
  8. #include<iostream>
  9. #include<string>
  10. #include<cstring>
  11. #include<cstdlib>
  12. using namespace std;

  13. int compareTwoStr(char *a, char *b) //return the length of the same subtring between two strings
  14. {
  15.     int len = 0; //the first char of the two strings is same
  16.     while(*a && *b)
  17.     {
  18.         if(*a == *b)
  19.         {
  20.             ++len;
  21.         }
  22.         else
  23.         {
  24.             break;
  25.         }
  26.         a++;
  27.         b++;
  28.     }
  29.     return len;
  30. }

  31. void find(string dest)
  32. {
  33.     int maxlen = 0;
  34.     int temp;
  35.     string::size_type pos, it, jt;
  36.     for(it = 0; it != dest.size(); it++)
  37.     {
  38.         for(jt = it + 1; jt != dest.size(); jt++)
  39.         {
  40.             if(dest[it] == dest[jt]) //find the first same char's pos
  41.             {
  42.                 temp = compareTwoStr(&dest[it], &dest[jt]);//call the compareTwoStr and return the len
  43.                 if(temp > maxlen)
  44.                 {
  45.                     maxlen = temp;
  46.                     pos = it;
  47.                 }
  48.                 continue;
  49.             }
  50.         }
  51.     }

  52.     cout << "The length of the max mutiple substring is: " << maxlen << endl;
  53.     cout << "The substring is: ";
  54.     cout << "pos: " << pos << endl;
  55.     for(it = 0; it != maxlen; it++)
  56.     {
  57.         cout << dest[it + pos];
  58.     }
  59.     cout << endl;
  60. }
  61. int main()
    {
        string dest("what is your name is dong dao xiang your name is dong dao xiang");
        find(dest);
        return 0;
    }
相信大家一看就能明白上面的算法。这里不再多说,我们看下面的算法,因为在上述算法中我们要两重循环遍历整个字符数组,然后比较字串的长度。其实有很多比较是没有必要的,所以我们可以利用一个后缀数组,将该字符数组所有的顺序子串的首地址传递给该后缀数组的每个元素,然后按照字典顺序将所有的顺序子串排序,比较相邻的子串,输出最大的子串

点击(此处)折叠或打开

  1. /*************************************************************************
  2.     > File Name: maxMutiSubStr.cpp
  3.     > Author: dongdaoxiang
  4.     > Func: find the Max Mutiple SubString
  5.     > Mail: dongdaoxiang@ncic.ac.cn
  6.     > Created Time: 2013年04月28日 星期日 13时58分11秒
  7.  ************************************************************************/
  8. #include<iostream>
  9. #include<string>
  10. #include<cstring>
  11. #include<cstdlib>
  12. using namespace std;

  13. int compareTwoStr(char *a, char *b) //return the length of the same subtring between two strings
  14. {
  15.     int len = 0; //the first char of the two strings is same
  16.     while(*a && *b)
  17.     {
  18.         if(*a == *b)
  19.         {
  20.             ++len;
  21.         }
  22.         else
  23.         {
  24.             break;
  25.         }
  26.         a++;
  27.         b++;
  28.     }
  29.     return len;
  30. }

  31. int pstrcmp(const void *p, const void *q)
  32. {
  33.     const char **_p = (const char**)p;
  34.     const char **_q = (const char**)q;
  35.     return strcmp((*_p), (*_q));
  36. }

  37. void quickFind(string dest)
  38. {
  39.     int maxlen = 0;
  40.     int temp = 0;
  41.     int flag;
  42.     char* suffixArr[dest.size()]; //create a suffix array
  43.     string::size_type it, pos;
  44.     for(it = 0; it != dest.size(); it++)
  45.     {
  46.         suffixArr[it] = &dest[it];
  47.     }
  48.     qsort(suffixArr, dest.size(), sizeof(char*), pstrcmp); //sort the array by the order of the word
  49.     cout << "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" << endl;
  50.     for(it = 0; it != dest.size(); it++)
  51.     {
  52.         cout << suffixArr[it] << endl;
  53.     }
  54.     cout << "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" << endl;
  55.     for(it = 0; it < dest.size(); it++)
  56.     {
  57.         temp = compareTwoStr(suffixArr[it], suffixArr[it + 1]); //cmp the nearby strings
  58.         if(temp > maxlen)
  59.         {
  60.             maxlen = temp;
  61.             pos = it;
  62.         }
  63.     }
  64.     cout << "The length of the max mutiple substring is: " << maxlen << endl;
  65.     cout << "The substring is: ";
  66.     cout << suffixArr[pos];
  67.     cout << endl;

  68. }

  69. int main()
  70. {
  71.     string dest("what is your name is dong dao xiang your name is dong dao xiang");
  72.     string dest2("abcefabcdhabcefabcdh");
  73.     find(dest2);
  74.     quickFind(dest2);
  75.     return 0;
  76. }


阅读(1791) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~