字串的最長重複子串(轉)

來源:互聯網
上載者:User

標籤:

給定一個字串,輸出最長的重複子串

舉例:ask not what your country can do for you,but what youcan do for your country

最長的重複子串:can do for you

思路:使用尾碼數組解決

分析:

1、由於要求最長公用子序列,則需要找到字串的所有子串,即通過產生字串的尾碼數組實現。

2、由於要求最長的重複子串,則需要對所有子串進行排序,這樣可以把相同的字串排在一起

3、比較相鄰字串,找出兩個子串中,相同的字元的個數。

注意,對於一個子串,一個與其重複最多的字串肯定是緊挨著自己的兩個字串。

步驟:

      1、對待處理的字串產生尾碼數組

      2、對尾碼數組排序

      3、依次檢測相鄰兩個尾碼的公用長度

      4、取出最大公用長度的首碼

 

舉例:輸入字串 banana

1、字串產生的尾碼數組:
    a[0]:banana
    a[1]:anana
    a[2]:nana
    a[3]:ana
    a[4]:na
    a[5]:a

2、對尾碼數組進行快速排序,以將尾碼相近的(變位詞)子串集中在一起

    a[0]:a
    a[1]:ana
    a[2]:anana
    a[3]:banana
    a[4]:na
    a[5]:nana

之後可以依次檢測相鄰兩個尾碼的公用長度並取出最大公用的首碼

代碼:

 1 /*給定出一個字串,輸出最長的重複子字串*/   2 #include <iostream>   3 #include <algorithm>   4 #include <string>   5 using namespace std;   6 const int MaxCharNum = 5000000;   7    8 bool StrCmp(char* str1,char* str2);   9 void GenSuffixArray(char* str,char* suffixStr[]);  10 int ComStrLen(char* str1,char* str2);  11 void GenMaxReStr(char* str);   12   13 int main()  14 {  15     char str[MaxCharNum];  16     cin.getline(str,MaxCharNum);//遇到斷行符號結束  17     GenMaxReStr(str);  18     system("pause");  19     return 1;  20 }  21   22 void GenMaxReStr(char* str)  23 {  24     int len = strlen(str);  25     int comReStrLen = 0;  26     int maxLoc = 0;  27     int maxLen = 0;  28     char* suffixStr[MaxCharNum];  29     GenSuffixArray(str,suffixStr);//產生尾碼數組  30     //對尾碼數組進行排序  31     sort(suffixStr,suffixStr+len,StrCmp);  32   33     //統計相鄰單詞中相同的字元數,並輸出結果  34     for (int i = 0;i < len-1;i++ )  35     {  36         comReStrLen =  ComStrLen(suffixStr[i],suffixStr[i+1]);  37         if (comReStrLen > maxLen)  38         {  39             maxLoc = i;  40             maxLen = comReStrLen;  41         }  42     }  43     //輸出結果  44     for (int i = 0;i < maxLen;i++)  45     {  46         cout<<suffixStr[maxLoc][i];  47     }  48     cout<<endl;  49 }  50 /*為字串產生其尾碼數組,並存放到數組suffixStr中*/  51 void GenSuffixArray(char* str,char* suffixStr[])  52 {  53     int len = strlen(str);  54     for (int i = 0;i < len;i++)  55     {  56         suffixStr[i] = &str[i];  57     }  58 }  59 /*返回str1和str2的共同首碼的長度*/  60 int ComStrLen(char* str1,char* str2)  61 {  62     int comLen = 0;  63     while(*str1 && *str2)  64     {  65         if (*str1 == *str2)  66         {  67             comLen++;  68         }  69         str1++;  70         str2++;  71     }  72     return comLen;  73 }  74   75 //字串升序排序  76 bool StrCmp(char* str1,char* str2)  77 {  78     if (strcmp(str1,str2) >=0 )  79     {  80         return false;  81     }  82     return true;  83 }  84

  

ask not what your country can do for you,but what you can do for your country

輸出:can do for you

時間複雜度分析:產生尾碼數組-時間複雜度O(N)、對尾碼數組排序是O(N*NlogN),第一個N表示字串的比較,後面NlogN使用快排排序。依次檢測相鄰兩個尾碼的公用長度-時間複雜度O(N*N)、取出最大公用長度的首碼-時間複雜度O(N)。

總的時間複雜度是O(N*NlogN)

這裡使用系統函數sort和strcmp產生有序的尾碼數組,他們沒有充分的利用數組重複的特性

我們可以使用倍增演算法高效的產生排好序的尾碼數組,從而提高效率

字串的最長重複子串(轉)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.