HDU4639 Hehe(组合|递推)

    题意描述:给一个串,其中“hehe”可以被替换成“QNMLEB”,当然也可以不替换,问有多少种情况

    那么首先找出一段连续的hehe 形如“hehe……he” 计算其可能替换数目f[ i ],然后可以运用乘法原理求总的数目即可。

    我们将一个"he"看成一个单位,i记录这个串的单位个数,那么f[ i ] = f[ i - 1 ] + f[ i - 2 ] 即增加的这个元素不替换的情况的数目等同f[ i ],而替换的方案数等同f [ i - 2 ] ,注意i>1且记得取模(比赛的时候WA到死)

 

#include<iostream>
#include<cstring>
using namespace std;

#define MOD 10007
#define ll long long
string str;
string he="hehe";
int cnt;
#define N 5055
ll f[N];
ll ret;
int s,e;
void init(){
    memset(f,0,sizeof(f));
    f[0]=1;
    f[1]=1;
    for(int i=2;i<N;i++){
        f[i]=f[i-2]+f[i-1];
        f[i]%=MOD;
    }
    ret=1;
}
void function(int n){
    //cout<<n<<endl;
    ret*=f[n];
    ret%=MOD;
}
ll doit(){
    cin>>str;
    int i,j;
    for(i=0;i<str.length()-1;i++){
        if(str[i]=='h'&&str[i+1]=='e'){
            int n=1;
            i+=2;
            while(str[i]=='h'&&str[i+1]=='e'&&i<str.length()-1){
                i+=2;
                n++;
            }
            if(n>1) function(n);
        }
    }
}

int main(){
    int t;
    cin>>t;
    for(int cas=1;cas<=t;cas++){
        init();
        doit();
        cout<<"Case "<<cas<<": "<<ret<<endl;
    }
    return 0;
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值