一道dp题,转移方程不是自己推出来的。
题目的意思是用‘qnmlgb’替换‘hehe’,可以替换也可以不替换,问有多少种情况。
如果结尾不是‘hehe’,那么dp[i]=dp[i-1],如果是是‘hehe’,dp[i]=dp[i]+dp[i-4],因为最后一个可以换也可以不换,如果进行替换则是dp[i-4]。
#include<stdio.h> #include<string.h> #define M 10007 char s[10100]; int dp[10100]={1}; int main() { int T,cas=1; scanf("%d",&T); while(T--) { scanf("%s",s+1); int len=strlen(s+1); for(int i=1;i<=len;i++) { dp[i]=dp[i-1]; if(s[i]=='e' && s[i-1]=='h' && s[i-2]=='e' && s[i-3]=='h') dp[i]=(dp[i]+dp[i-4])%M; } printf("Case %d: %d ",cas++,dp[len]); } return 0; }