01串 ---- 遞推

來源:互聯網
上載者:User

標籤:

這是一個符合費伯納西數列的dp問題,動態轉移方程:dp[i]=dp[i-1]+dp[i-2];      解釋:長度為i的01串組成:長度為i-1的串末尾的0的個數*2+長度為i-1的串末尾的1的個數*1,而長度為i-1的末尾的0的個數等於長度為i-2的串的個數,等量替換以後就是上面的轉移方程。
 1 #include<stdio.h> 2 #include<string.h> 3 #include<math.h> 4 #include<iostream> 5 #include<limits.h> 6 #include<algorithm> 7 #include<queue> 8 #include<vector> 9 #include<set>10 #include<stack>11 #include<string>12 #include<sstream>13 #include<map>14 #include<cctype>15 using namespace std;16 int main()17 {18     int t,dp[45],n;19     scanf("%d",&t);20     dp[2]=3,dp[3]=5;21     for(int i=4;i<=40;i++)22         dp[i]=dp[i-1]+dp[i-2];23     while(t--)24     {25         scanf("%d",&n);26         printf("%d\n",dp[n]);27     }28     return 0;29 }

 

01串 ---- 遞推

聯繫我們

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