Ultraviolet A 531: Compromise

Source: Internet
Author: User

Obtain the longest common subsequence (LCS) and print the sequence. Dp Solution: mark the final sequence selection of each node and print it recursively. My problem-solving code is as follows:

 #include <iostream>  #include <cstdio>  #include <cstring>  #include <cstdlib>  #include <algorithm>  using namespace std;    #define max(a,b) (a>b?a:b)  #define WORDLEN 32  #define SENTENCELEN 102  char P1[SENTENCELEN][WORDLEN], P2[SENTENCELEN][WORDLEN];  int dp[SENTENCELEN][SENTENCELEN];  int PathSel[SENTENCELEN][SENTENCELEN];    void PrintSeq(int i, int j)  {      if(i<0 || j<0 || !dp[i][j]) return ;      if(!PathSel[i][j])      {          PrintSeq(i-1,j-1);          if(dp[i][j]==1) cout << P1[i];          else cout << ' ' << P1[i];      }      else if(PathSel[i][j]==1) PrintSeq(i-1,j);      else PrintSeq(i,j-1);     }  int main()  {      int m,n;      while(cin >> P1[0])      {          m = 0, n = 0;          while(P1[m++][0]!='#' && cin >> P1[m]) ; m--;          while(cin >> P2[n] && P2[n++][0]!='#') ; n--;                    dp[0][0] = 0;          for(int i=0; i<m; i++)          {              for(int j=0; j<n; j++)              {                  if(!strcmp(P1[i],P2[j]))                  {                      if(!i || !j) dp[i][j] = 1;                      else dp[i][j] = dp[i-1][j-1]+1;                      PathSel[i][j] = 0;                  }                  else if(!i || !j)                  {                      if(i) { dp[i][j] = dp[i-1][j]; PathSel[i][j] = 1;}                      if(j) { dp[i][j] = dp[i][j-1]; PathSel[i][j] = 2;}                  }                  else if(dp[i-1][j] > dp[i][j-1])                  {                      dp[i][j] = dp[i-1][j];                      PathSel[i][j] = 1;                  }                  else                  {                      dp[i][j] = dp[i][j-1];                      PathSel[i][j] = 2;                  }              }          }          PrintSeq(m-1,n-1);          cout << endl;      }      return 0;  }  

 


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.