最長公用子序列(LCS)的兩種求解方法

來源:互聯網
上載者:User
#include <iostream>#include <memory.h>using namespace std;const int MAX = 1000;int dp[MAX][MAX];int main(){    char str1[MAX], str2[MAX], LCS[MAX];    int i, j, len1, len2;    while (1){          cin.getline(str1, MAX);          cin.getline(str2, MAX);          len1 = strlen(str1);          len2 = strlen(str2);          memset(dp, 0, sizeof(dp));                    for (i = 1; i <= len1; i++){              for (j = 1; j <= len2; j++){                  if (str1[i-1] == str2[j-1]){                      dp[i][j] = dp[i-1][j-1] + 1;                  }                  else if (dp[i-1][j] > dp[i][j-1]){                       dp[i][j] = dp[i-1][j];                  }                  else {                       dp[i][j] = dp[i][j-1];                  }              }          }          printf("The Length of LCS is: %d\n", dp[len1][len2]);          //for (i = 0; i <= len1; i++){          //    for (j = 0; j <= len2; j++)          //        printf("%d ", dp[i][j]);          //    printf("\n");          //}                    //輸出LCS 本來是逆序列印的,可以寫一遞迴函式完成正序列印         //這裡採用的方法是將Y作為臨時儲存LCS的數組,最後輸出Y         i = len1, j = len2;         int k = dp[i][j];         LCS[k] = '\0';         while (i && j){               if (dp[i][j] == dp[i-1][j-1] + 1){                   LCS[--k] = str1[i-1];                   --i;                   --j;               }               else if (dp[i-1][j] >= dp[i][j-1]){                    --i;               }               else{                    --j;               }         }         printf("%s\n", LCS);    }         system("pause");}#include <iostream>using namespace std;const int MAX = 1000;int dp[2][MAX];//滾動數組儲存LCS的長度 int main(){    char str1[MAX], str2[MAX];    int xlen, ylen, i, j, k;    while (1){          cin.getline(str1, MAX);          cin.getline(str2, MAX);          xlen = strlen(str1);          ylen = strlen(str2);                    for (i = 1; i <= xlen; ++i){              k = i & 1;//i與1的與運算               for (j = 1; j <= ylen; ++j){                  if (str1[i-1] == str2[j-1]){                      dp[k][j] = dp[k^1][j-1] + 1;//k^1為異或運算                   }                  else if (dp[k][j-1] > dp[k^1][j]){                       dp[k][j] = dp[k][j-1];                  }                  else{                       dp[k][j] = dp[k^1][j];                       }              }          }          printf("The len of LCS is: %d\n", dp[k][ylen]);    }        system("pause");}/*ABDESKTDJBDKSEGC*/

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.