2013多校联合4 1008 Hehe (hdu 4639)

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): 231    Accepted Submission(s): 138


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
 

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.


同是签到题:一看到这道题的标题还有题目描述呵呵了,其实就是找传中连续的"he"串,求出对应的放法数,然后分别用乘法原理乘起来就可以了,对于连续的"he"串,我们可以设dp[i]为i个连续"he"串的方法数,

易知dp[i]=dp[i-1]+dp[i-2],(i>1),其实就是个斐波那契数列。预处理即可,剩下的就是依次遍历求原字符串的连续"he"子串的个数和分别的长度了。代码如下:

#include <iostream>
#include <string.h>
#include <stdio.h>
#define maxn 100010
#define mod 10007
using namespace std;
int f[5050];
void init()
{
    int i;
    f[0]=1;
    f[1]=1;
    f[2]=2;
    for(i=3;i<=5050;i++)
    {
        f[i]=(f[i-1]+f[i-2])%mod;
    }
}
char str[11000];
int main()
{
    //freopen("dd.txt","r",stdin);
    int ncase,i,time=0;
    init();
    scanf("%d",&ncase);
    while(ncase--)
    {
        printf("Case %d: ",++time);
        scanf("%s",str);
        int len=strlen(str);
        int ans=1,tmp=0;
        for(i=0;i<len-1;)
        {
            if(str[i]=='h'&&str[i+1]=='e')
            {
                while(str[i]=='h'&&str[i+1]=='e')
                {
                    tmp++;
                    i+=2;
                }
                ans=(ans*f[tmp])%mod;
                tmp=0;
            }
            else
            {
                i++;
                tmp=0;
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}


你可能感兴趣的:(2013多校联合4 1008 Hehe (hdu 4639))