后缀数组,字符串中连续出现最多的字串

来源:互联网 发布:网络调教女m的任务详细 编辑:程序博客网 时间:2024/05/16 01:53
 

【啊哈,算法】之九、后缀数组,字符串中连续出现最多的字串

分类: Algorithm 110人阅读 评论(0) 收藏 举报

转载自:http://blog.csdn.net/ysu108/article/details/7795479

字符串“abababc”,最多连续出现的为ab,连续出现三次。要和求一个字符串中的最长重复子串区分开来,还是上面的字符串,那么最长的重复子串为abab。两个题目的解法有些类似,都用到了后缀数组这个数据结构。求一个字符串中连续出现的次数最多的子串,首先生成后缀数组例如上面的字符串为:

abababc
bababc
ababc
babc
abc
bc
c

可以看出第一个后缀数组和第三个后缀数组的起始都为ab,第5个后缀数组也为ab。可以看出规律来,一个字符串s,如果第一次出现在后缀数组i的前面,那么如果它重复出现,下一次出现应该在第i+len(s)个后缀数组的前面。这个规律也不难看出。那么从头到尾按照这个规律搜索下不难得出结果。下面是代码:

[cpp] view plaincopy
  1. #include <iostream>    
  2. using namespace std;    
  3.     
  4. int con_sub(char *str, char **ret);    
  5.     
  6. int main()    
  7. {    
  8.         char str[] = "abcabcabcabcabcabbbb";    
  9.         char *ret = NULL;    
  10.         int time = con_sub(str, &ret);    
  11.         printf("%s occuers %d times\n", ret, time);    
  12.         return 0;    
  13. }    
  14.     
  15. int con_sub(char *str, char **ret)    
  16. {    
  17.         int max_time = 0;//连续出现的最多次数    
  18.         int ret_len = 0;//连续出现的字符串的长度    
  19.         char *addr = NULL;//连续出现字符串的起始地址    
  20.     
  21.         int len = strlen(str);    
  22.         char **a = (char **)malloc(sizeof(char *)*len);    
  23.         //生成后缀数组    
  24.         for(int i=0; i<len; i++)    
  25.                 a[i] = &str[i];    
  26.     
  27.         //重复字符串的长度范围为1到(len+1)/2    
  28.         for(int i=1; i<=(len+1)/2; i++)    
  29.         {    
  30.                 //当重复的字符串长度为i的时候,如果是连续出现的,那么第j和第j+i个后缀数组前面为重复的字符串    
  31.                 for(int j=0; j+i<=len-1; j+=i)    
  32.                 {    
  33.                         int k = j;    
  34.                         int temp_time = 1;    
  35.                         while(k+i <= len-1 && strncmp(a[k], a[k+i], i) == 0)    
  36.                         {    
  37.                                 temp_time++;    
  38.                                 k += i;    
  39.                         }    
  40.                         if(temp_time > max_time)    
  41.                         {    
  42.                                 max_time = temp_time;    
  43.                                 ret_len = i;    
  44.                                 addr = a[k];    
  45.                         }    
  46.                 }    
  47.         }    
  48.         *ret = new char[len+1];    
  49.         strncpy(*ret, addr, ret_len);    
  50.         return max_time;    
  51. }    
 

【啊哈,算法】之十、后缀数组,求最长重复子串

分类: Algorithm 105人阅读 评论(0) 收藏 举报

转自:http://blog.csdn.net/hackbuteer1/article/details/7968623


直观的解法是,首先检测长度为 n - 1 的字符串情况,如果不存在重复则检测 n - 2, 一直递减下去,直到 1 。

这种方法的时间复杂度是 O(N * N * N),其中包括三部分,长度纬度、根据长度检测的字符串数目、字符串检测。


利用后缀数组
后缀数组是一种数据结构,对一个字符串生成相应的后缀数组后,然后再排序,排完序依次检测相邻的两个字符串的开头公共部分。
这样的时间复杂度为:生成后缀数组 O(N),排序 O(NlogN*N) 最后面的 N 是因为字符串比较也是 O(N)
依次检测相邻的两个字符串 O(N * N),总的时间复杂度是 O(N^2*logN),优于第一种方法的 O(N^3)

demo如下:

其中:当作为comlen函数参数的两个字符串长度相等时,该函数便返回这个长度值,从第一个字符开始

[cpp] view plaincopy
  1. int comlen( char *p, char *q )    
  2. {    
  3.     int i = 0;    
  4.     while( *p && (*p++ == *q++) )    
  5.         ++i;    
  6.     return i;    
  7. }    
  8.   
  9. int main(void)    
  10. {    
  11.     int i , j , thislen , maxlen = -1;    
  12.     ......    
  13.     ......    
  14.     ......    
  15.     for(i = 0 ; i < n ; ++i )    
  16.     {    
  17.         for(j = i+1 ; j < n ; ++j )    
  18.         {    
  19.             if((thislen = comlen(&c[i] , &c[j])) > maxlen)    
  20.             {    
  21.                 maxlen = thislen;    
  22.                 maxi = i;    
  23.                 maxj = j;    
  24.             }    
  25.         }    
  26.     }    
  27.     ......    
  28.     ......    
  29.     ......    
  30.     return 0;    
  31. }    

完整代码如下:

[cpp] view plaincopy
  1. #include <iostream>    
  2. using namespace std;    
  3.     
  4. #define MAXCHAR 5000 //最长处理5000个字符    
  5.     
  6. char c[MAXCHAR], *a[MAXCHAR];    
  7.     
  8. int comlen( char *p, char *q )    
  9. {    
  10.     int i = 0;    
  11.     while( *p && (*p++ == *q++) )    
  12.         ++i;    
  13.     return i;    
  14. }    
  15.     
  16. int pstrcmp( const void *p1, const void *p2 )    
  17. {    
  18.     return strcmp( *(charconst *)p1, *(charconst*)p2 );    
  19. }    
  20.     
  21.     
  22. int main(void)    
  23. {    
  24.     char ch;    
  25.     int  n=0;    
  26.     int  i, temp;    
  27.     int  maxlen=0, maxi=0;    
  28.     printf("Please input your string:\n");    
  29.     
  30.     n = 0;    
  31.     while( (ch=getchar())!='\n' )    
  32.     {    
  33.         a[n] = &c[n];    
  34.         c[n++] = ch;    
  35.     }    
  36.     c[n]='\0';     // 将数组c中的最后一个元素设为空字符,以终止所有字符串    
  37.     
  38.     qsort( a, n, sizeof(char*), pstrcmp );    
  39.     for(i = 0 ; i < n-1 ; ++i )    
  40.     {    
  41.         temp=comlen( a[i], a[i+1] );    
  42.         if( temp>maxlen )    
  43.         {    
  44.             maxlen=temp;    
  45.             maxi=i;    
  46.         }    
  47.     }    
  48.     printf("%.*s\n",maxlen, a[maxi]);    
  49.         
  50.     return 0;    
  51. }    

元素a[0]指向整个字符串,下一个元素指向以第二个字符开始的数组的后缀,等等。如若输入字符串为"banana",该数组将表示这些后缀:
a[0]:banana
a[1]:anana
a[2]:nana
a[3]:ana
a[4]:na
a[5]:a
由于数组a中的指针分别指向字符串中的每个后缀,所以将数组a命名为"后缀数组"


对后缀数组进行快速排序,以将后缀相近的(变位词)子串集中在一起
qsort(a, n, sizeof(char*), pstrcmp)后
a[0]:a
a[1]:ana
a[2]:anana
a[3]:banana
a[4]:na
a[5]:nana


用comlen函数对数组进行扫描比较邻接元素,以找出最长重复的字符串。



第二种方法是通过kmp算法,next数组的方式:

[cpp] view plaincopy
  1. #include<iostream>    
  2. using namespace std;    
  3.     
  4. const int MAX = 100000;    
  5. int next[MAX];    
  6. char str[MAX];    
  7.     
  8. void GetNext(char *t)    
  9. {    
  10.     int len = strlen(t);    
  11.     next[0] = -1;    
  12.     int i = 0 , j = -1;    
  13.     while(i < len)    
  14.     {    
  15.         if(j == -1 || t[i] == t[j])    
  16.         {    
  17.             i++;    
  18.             j++;    
  19.             if(t[i] != t[j])    
  20.                 next[i] = j;    
  21.             else    
  22.                 next[i] = next[j];    
  23.         }    
  24.         else    
  25.             j = next[j];    
  26.     }    
  27. }    
  28. int main(void)    
  29. {    
  30.     int i , j , index , len;    
  31.     cout<<"Please input your string:"<<endl;    
  32.     
  33.     cin>>str;    
  34.     char *s = str;    
  35.     len = 0;    
  36.     for(i = 0 ; *s != '\0' ; s++ , ++i)    
  37.     {    
  38.         GetNext(s);    
  39.         for(j = 1 ; j <= strlen(s) ; ++j)    
  40.         {    
  41.             if(next[j] > len)    
  42.             {    
  43.                 len = next[j];    
  44.                 index = i + j;    //index是第一个最长重复串在str中的位置     
  45.             }    
  46.         }    
  47.     }    
  48.     if(len > 0)    
  49.     {    
  50.         for(i = index - len ; i < index ; ++i)    
  51.             cout<<str[i];    
  52.         cout<<endl;    
  53.     }    
  54.     else    
  55.         cout<<"none"<<endl;    
  56.     
  57.     return 0;    
  58. }    

题目描述:求最长不重复子串,如abcdefgegcsgcasse,最长不重复子串为abcdefg,长度为7

[cpp] view plaincopy
  1. #include <iostream>  
  2. #include <list>  
  3. using namespace std;  
  4.   
  5. //思路:用一个数组保存字符出现的次数。用i和j进行遍历整个字符串。  
  6. //当某个字符没有出现过,次数+1;出现字符已经出现过,次数+1,找到这个字符前面出现的位置的下一个位置,设为i  
  7. //并将之前的那些字符次数都-1。继续遍历,直到'\0'  
  8. int find(char str[],char *output)  
  9. {  
  10.     int i = 0 , j = 0;  
  11.     int cnt[26] = {0};  
  12.     int res = 0 , temp = 0;  
  13.     char *out = output;  
  14.     int final;  
  15.     while(str[j] != '\0')  
  16.     {  
  17.         if(cnt[str[j]-'a'] == 0)  
  18.         {  
  19.             cnt[str[j]-'a']++;  
  20.   
  21.         }  
  22.         else  
  23.         {  
  24.             cnt[str[j]-'a']++;  
  25.             while(str[i] != str[j])  
  26.             {     
  27.                 cnt[str[i]-'a']--;  
  28.                 i++;  
  29.             }  
  30.             cnt[str[i]-'a']--;  
  31.             i++;  
  32.         }     
  33.   
  34.         j++;  
  35.         temp = j-i;  
  36.         if(temp > res)  
  37.         {  
  38.             res = temp;  
  39.             final = i;  
  40.         }  
  41.     }  
  42.     //结果保存在output里面  
  43.     for(i = 0 ; i < res ; ++i)  
  44.         *out++ = str[final++];  
  45.     *out = '\0';  
  46.     return res;  
  47. }  
  48. int main(void)  
  49. {  
  50.     char a[] = "abcdefg";  
  51.     char b[100];  
  52.     int max = find(a,b);  
  53.     cout<<b<<endl;  
  54.     cout<<max<<endl;  
  55.     return 0;  
  56. }  


原创粉丝点击