题意比较简单,就是数出有多少个连续的he,当有一个大案是1,二个是2,三个是3,四个是5,五个是8。。。不连续的he段之间是相乘的关系
其实出题的想法是做dp的都拿我这个就比较好玩了,直接递推了,标程题解发如下dp,直接用dp[i]表示0-i这段区间可能变成的不同句子数,首先有dp[i+1]+=dp[i],如果当前位置向后连续4个字符是hehe,那么有dp[i+4]+=dp[i],最后的答案就是dp[len]了
我的代码,可能比较难看,,,
#include<cstdio> #include<cstring> int f[10186],T,cas=0,mod=10007; char ch[10186]; int deal() { scanf("%s",ch); int ans=1,fir=0,las,heh,flag,len=strlen(ch); while(fir<len) { heh=flag=0; while(fir<len) if(ch[fir]=='h'&&ch[fir+1]=='e') fir+=flag=2,heh++; else if(++fir&&flag) break; ans=(ans*f[heh])%mod; } return ans; } int main() { scanf("%d",&T); for(int i=f[0]=f[1]=1;i<5045;i++) f[i+1]=(f[i-1]+f[i])%mod; while(++cas<=T) printf("Case %d: %d\n",cas,deal()); return 0; }
#include<stdio.h> #include<string.h> #include<math.h> #include<iostream> #include<vector> #include<map> #include<set> #include<algorithm> using namespace std; typedef __int64 lld; #define X first #define Y second #define mp make_pair #define pb push_back #define inf 0xfffffff #define mod 10007 #define maxn 100010 lld dp[maxn]; void init() { dp[0]=1; dp[1]=1; for(int i=2;i<maxn;i++) dp[i]=(dp[i-1]+dp[i-2])%mod; } char str[maxn]; int main() { init(); int cas; scanf("%d",&cas); for(int cc=1;cc<=cas;cc++) { scanf("%s",str); int len=strlen(str); lld ans=1; for(int i=0;i<len-1;i++) { if(str[i] == 'h' && str[i+1] == 'e') { int t=0; int j=i; while(j < len-1 && str[j] == 'h' && str[j+1] == 'e') { j+=2; t++; } ans=(ans*dp[t])%mod; i=j; } } printf("Case %d: ",cc); cout << ans << endl; } return 0; }