zoukankan      html  css  js  c++  java
  • [转]后缀数组求最长重复子串

    问题描述
    给定一个字符串,求出其最长重复子串
    例如:abcdabcd
    最长重复子串是 abcd,最长重复子串可以重叠
    例如:abcdabcda,这时最长重复子串是 abcda,中间的 a 是被重叠的。

    直观的解法是,首先检测长度为 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)


          对于类似从给定的文本中,查找其中最长的重复子字符串的问题,可以采用“后缀数组”来高效地完成此任务。后缀数组使用文本本身和n个附加指针(与文本数组相应的指针数组)来表示输入文本中的n个字符的每个子字符串。
        首先,如果输入字符串存储在c[0..n-1]中,那么就可以使用类似于下面的代码比较每对子字符串:
    1. int main(void)  
    2. {  
    3.     int i , j , thislen , maxlen = -1;  
    4.     ......  
    5.     ......  
    6.     ......  
    7.     for(i = 0 ; i < n ; ++i )  
    8.     {  
    9.         for(j = i+1 ; j < n ; ++j )  
    10.         {  
    11.             if((thislen = comlen(&c[i] , &c[j])) > maxlen)  
    12.             {  
    13.                 maxlen = thislen;  
    14.                 maxi = i;  
    15.                 maxj = j;  
    16.             }  
    17.         }  
    18.     }  
    19.     ......  
    20.     ......  
    21.     ......  
    22.     return 0;  
    23. }  
    当作为comlen函数参数的两个字符串长度相等时,该函数便返回这个长度值,从第一个字符开始:
    1. int comlen( char *p, char *q )  
    2. {  
    3.     int i = 0;  
    4.     while( *p && (*p++ == *q++) )  
    5.         ++i;  
    6.     return i;  
    7. }  
        由于该算法查看所有的字符串对,所以它的时间和n的平方成正比。下面便是使用“后缀数组”的解决办法。
        如果程序至多可以处理MAXN个字符,这些字符被存储在数组c中:
    1. #define MAXCHAR 5000 //最长处理5000个字符  
    2.   
    3. char c[MAXCHAR], *a[MAXCHAR];  
    在读取输入时,首先初始化a,这样,每个元素就都指向输入字符串中的相应字符:
    1. n = 0;  
    2. while( (ch=getchar())!=' ' )  
    3. {  
    4.      a[n] = &c[n];  
    5.      c[n++] = ch;  
    6. }  
    7. c[n]='';     // 将数组c中的最后一个元素设为空字符,以终止所有字符串  
    这样,元素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函数对数组进行扫描比较邻接元素,以找出最长重复的字符串:
    1. for(i = 0 ; i < n-1 ; ++i )  
    2. {  
    3.         temp=comlen( a[i], a[i+1] );  
    4.         if( temp>maxlen )  
    5.     {  
    6.               maxlen=temp;  
    7.               maxi=i;  
    8.         }  
    9. }  
    10. printf("%.*s ",maxlen, a[maxi]);  
    完整的实现代码如下:
    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: ");  
    29.   
    30.     n = 0;  
    31.     while( (ch=getchar())!=' ' )  
    32.     {  
    33.         a[n] = &c[n];  
    34.         c[n++] = ch;  
    35.     }  
    36.     c[n]='';     // 将数组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 ",maxlen, a[maxi]);  
    49.       
    50.     return 0;  
    51. }  
    方法二:KMP
    通过使用next数组的特性,同样可以求最长重复子串,不过时间复杂度有点高挖。。

    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 != '' ; 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
    1. #include <iostream>  
    2. #include <list>  
    3. using namespace std;  
    4.   
    5. //思路:用一个数组保存字符出现的次数。用i和j进行遍历整个字符串。  
    6. //当某个字符没有出现过,次数+1;出现字符已经出现过,次数+1,找到这个字符前面出现的位置的下一个位置,设为i  
    7. //并将之前的那些字符次数都-1。继续遍历,直到''  
    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] != '')  
    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 = '';  
    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. }  

  • 相关阅读:
    java基本输入型数据Scanner
    java基本·数据类型
    【PyCharm编辑器】之报:Spellchecker inspection helps locate typos and misspelling in your code, comments and literals, and fix them in one click.问题
    【转】【selenium+Python WebDriver】之元素定位不到解决办法
    【selenium】Selenium基于Python3的Web自动化测试脚本在IE上运行慢的解决方法
    【转载】【selenium+Python WebDriver】之元素定位
    【Python基础】之异常
    【Python基础】之不同的文件在不同目录下导入指定模块的方法
    【Python基础】之函数、类和方法
    【Python基础】之for循环、数组字典
  • 原文地址:https://www.cnblogs.com/anyuan9/p/6171716.html
Copyright © 2011-2022 走看看