HDU 4639 Hehe
Hehe
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 355 Accepted Submission(s): 202
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 “hihi”, “haha” 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
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
Recommend
zhuyuanchen520
我用的是组合的方法:
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int mod=10007; char str[12000]; int C[1000][1000]; void init(){ for(int i=0;i<1000;i++){ C[i][i]=C[i][0]=1; for(int j=1;j<i;j++) C[i][j]=(C[i-1][j-1]+C[i-1][j])%mod; } } int main(){ //freopen("input.txt","r",stdin); int t,cases=0; scanf("%d",&t); init(); while(t--){ scanf("%s",str); int cnt=0,ans=1; int len=strlen(str); for(int i=0;i<len;i++){ cnt=0; int tmp=1; while(i+1<len && str[i]=='h' && str[i+1]=='e'){ //找出每次连续在一起的he的个数 i+=2; cnt++; } if(cnt>=2){ for(int i=1;i<=cnt/2;i++){ tmp=(tmp+C[i+cnt-i*2][i])%mod; } } ans=(ans*tmp)%mod; } printf("Case %d: %d\n",++cases,ans); } return 0; }
另一种思路:
思路: 斐波那契数列
#include <cstdio> #include <cstring> #include <algorithm> #include <iostream> #include <cmath> #include <vector> using namespace std; //斐波那契数列 const int V = 5050 + 50; const int MaxN = 500 + 5; const int mod = 10000 + 7; int T, ans, num[V]; char ch[10100]; int main() { int i, j, k; scanf("%d", &T); num[0] = num[1] = 1; for(i = 2; i <= V; ++i) num[i] = (num[i - 1] + num[i - 2]) % mod; for(i = 1; i <= T; ++i) { ans = 1; int sum = 0; scanf("%s", &ch); int len = strlen(ch); for(j = 1; j < len; ++j) { if(ch[j] == 'e' && ch[j - 1] == 'h') { sum++; j++; } else { ans = (ans * num[sum]) % mod; sum = 0; } } ans = (ans * num[sum]) % mod; printf("Case %d: %d\n", i, ans); } }