Hehe(hdu4639,斐波拉契数+排列组合)

本文介绍了一道名为“笑笑问题”的编程竞赛题目,该题涉及字符串处理与组合数学中的斐波那契数列。通过分析字符串中特定子串出现的不同意义,利用斐波那契数列求解所有可能的意义数量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/*

http://acm.hdu.edu.cn/showproblem.php?pid=4639

Hehe

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 224    Accepted Submission(s): 135

Problem Description

As we all know, Fat Brother likes MeiZi every much, he always find some topic to talk with her. But as Fat Brother is so low profile that no one knows he is a rich-two-generation expect the author, MeiZi always rejects him by typing hehe” (wqnmlgb). You have to believe that there is still some idealized person just like Fat Brother. They think that the meaning of hehe” is just hehe, such like hihihaha” and so on. But indeed sometimes hehe” may really means hehe. Now you are given a sentence, every hehe” in this sentence can replace by wqnmlgb” or just hehe, please calculate that how many different meaning of this sentence may be. Note that wqnmlgb” means “我去年买了个表” in Chinese.

 

Input

The first line contains only one integer T, which is the number of test cases.Each test case contains a string means the given sentence. Note that the given sentence just consists of lowercase letters.

T<=100

The length of each sentence <= 10086

Output

For each test case, output the case number first, and then output the number of the different meaning of this sentence may be. Since this number may be quite large, you should output the answer modulo 10007.

Sample Input

4

wanshangniyoukongme

womenyiqichuqukanxingxingba

bulehehewohaiyoushi

eheheheh

Sample Output

Case 1: 1

Case 2: 1

Case 3: 2

Case 4: 3

Source

2013 Multi-University Training Contest 4

Recommend

zhuyuanchen520

解析:

题意:“hehe”有两种意思,给出字符串,问这个字符串有几种含义

思路:斐波拉契数+排列组合,”he“的个数满足斐波拉契数,然后把每段连续含有"he "的个数的斐波拉契数相乘

Accepted 15MS 272K 691 B C++

*/

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
#include<iostream>
using namespace std;
const int maxn=10100+10;
const int mod=10007;
int f[maxn];
char s[maxn];
void init()
{
f[1]=f[0]=1;
for(int i=2;i<maxn;i++)
f[i]=(f[i-1]+f[i-2])%mod;
}
int main()
{
int T,i,c=0;
init();
scanf("%d",&T);
while(T--)
 {
 	scanf("%s",s);
 	int len=strlen(s);
 	int ans=1,t=0;
 	for(i=0;i<len;i++)
 	 {
 	 	if(s[i]=='h'&&s[i+1]=='e')
 	 	{
 	 	 t=1;
 	 	 i+=2;
 	 	 while(s[i]=='h'&&s[i+1]=='e'&&(i+1)<len)
 	 	 {
 	 	 t++;
 	 	 i+=2;
 	 	 }
 	 	 ans=(ans*f[t])%mod;
 	 	}
 	 }
 	 printf("Case %d: %d\n",++c,ans);
 }
 return 0;
}


### HDU OJ 排列组合问题解法 排列组合问题是算法竞赛中的常见题型之一,涉及学基础以及高效的实现技巧。以下是关于如何解决此类问题的一些通用方法和具体实例。 #### 学基础知识 在处理排列组合问题时,需要熟悉以下几个基本概念: - **阶乘计算**:用于求解全排列的量 $ n! = n \times (n-1) \times ... \times 1 $[^4]。 - **组合公式**:$ C(n, k) = \frac{n!}{k!(n-k)!} $ 表示从 $ n $ 中选取 $ k $ 的方案[^5]。 - **快速幂运算**:当涉及到模运算时,可以利用费马小定理优化逆元的计算[^6]。 #### 题目推荐与分析 以下是一些典型的 HDU OJ 上的排列组合题目及其可能的解法: ##### 1. 基础排列组合 - **HDU 2039 近似** - 描述:给定两个整 $ a $ 和 $ b $,统计区间内的近似量。 - 方法:通过枚举每一位上的可能性来构建合法字并计[^7]。 ```cpp #include <iostream> using namespace std; long long comb(int n, int r){ if(r > n || r < 0)return 0; long long res=1; for(int i=1;i<=r;i++)res=res*(n-i+1)/i; return res; } int main(){ int t,n,k; cin>>t; while(t--){ cin>>n>>k; cout<<comb(n+k-1,k)<<endl; // 组合应用 } } ``` ##### 2. 动态规划的应用 - **HDU 1028 Ignatius and the Princess III** - 描述:给出正整 $ m $ 和 $ n $,问有多少种方式把 $ m $ 分成最多 $ n $ 份。 - 方法:定义状态转移方程 $ dp[i][j]=dp[i-1][j]+dp[i][j-i] $ 来表示当前总和为 $ j $ 并分成至多 $ i $ 份的情况目[^8]。 ```cpp #include<bits/stdc++.h> using namespace std; const int MAXN=1e3+5; long long c[MAXN][MAXN]; void init(){ memset(c,0,sizeof(c)); c[0][0]=1; for(int i=1;i<MAXN;i++){ c[i][0]=c[i][i]=1; for(int j=1;j<i;j++) c[i][j]=(c[i-1][j-1]+c[i-1][j])%(1e9+7); } } int main(){ init(); int T,m,n; scanf("%d",&T); while(T--){ scanf("%d%d",&m,&n); printf("%lld\n",c[m+n-1][min(m,n)]); } } ``` #### 总结 针对不同类型的排列组合问题,可以选择合适的工具和技术加以应对。无论是简单的直接计算还是复杂的动态规划模型,都需要扎实的基础知识作为支撑。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值