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

时间:2023-03-08 21:49:08

题目:点击打开链接

多校练习赛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;
}