【斐波那契DP】HDU 4639——HeHe

來源:互聯網
上載者:User

題目:點擊開啟連結

多校練習賽4的簡單題,但是比賽的時候想到了推導公式f(n)=f(n-1)+f(n-2)(就是斐波那契數列),最後卻沒做出來。

首先手寫一下he(不是hehe)連續時的規律。0-1 1-1 2-2 3-3 4-5,斐波那契無誤。

比賽的時候沒有分清楚連續的he和間斷的he的不同,只有連續的he才能用斐波那契數列來表示,而間斷點應該重新計算he出現的次數,最後根據組合數的原理相乘,即可得到最終的答案。

  

//dp,但是找規律也可以發現連續的是FIb數列. #include <iostream>#include <cstring>using namespace std;long long tar[10100];void create_fib(){tar[0]=1;tar[1]=1;for(int i=2;i<=10086;i++){tar[i]=tar[i-1]+tar[i-2];tar[i]%=10007;}}int main(){int testcase;string str;create_fib();cin>>testcase;for(int t=1;t<=testcase;t++){int answer=1,count=0;cin>>str;for(int i=0;i<str.length();){if(str[i]=='h' && str[i+1]=='e'){count++;i+=2;}else{answer*=tar[count];answer%=10007;count=0;i++;}}answer*=tar[count]; //最後一段要乘上answer%=10007;cout<<"Case "<<t<<": "<<answer<<endl;}return 0;}

聯繫我們

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