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

题目:点击打开链接

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值