poj1159 dp最長公用子串

來源:互聯網
上載者:User

標籤:style   blog   http   color   os   io   for   art   

 1 //Accepted    204 KB    891 ms 2 //dp最長公用子串 3 //dp[i][j]=max(dp[i-1][j],dp[i][j-1]) 4 //dp[i][j]=max(dp[i][j],dp[i-1][j-1]+1) (s1[i]==s2[j]) 5 #include <cstdio> 6 #include <cstring> 7 #include <iostream> 8 using namespace std; 9 const int imax_n = 5002;10 int dp[2][imax_n];11 char s1[imax_n];12 char s2[imax_n];13 int n;14 int max(int a,int b)15 {16     return a>b?a:b;17 }18 void Dp()19 {20     memset(dp,0,sizeof(dp));21     for (int i=1;i<=n;i++)22     {23         for (int j=1;j<=n;j++)24         {25             dp[i%2][j]=max(dp[(i-1)%2][j],dp[i%2][j-1]);26             if (s1[i-1]==s2[j-1])27             dp[i%2][j]=max(dp[i%2][j],dp[(i-1)%2][j-1]+1);28         }29     }30     printf("%d\n",n-dp[n%2][n]);31 }32 int main()33 {34     while (scanf("%d",&n)!=EOF)35     {36         scanf("%s",s1);37         for (int i=0;i<n;i++)38         s2[i]=s1[n-1-i];39         s2[n]=‘\0‘;40         Dp();41     }42     return 0;43 }
View Code

 

聯繫我們

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