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;
}


 

你可能感兴趣的:(组合)