牛客小白月赛21I love you

I love you

题目链接I love you
在这里插入图片描述
解题思路

简单的dp思想,因为大小写不敏感,所以先将字符串中所有的大写字母换成小写字母

 f[0]=(f[0]+(s[i]=='i'))%Mod;
 f[1]=(f[1]+(s[i]=='l')*f[0])%Mod;
 f[2]=(f[2]+(s[i]=='o')*f[1])%Mod;

看之前有多少’i’存在,那么现在出现的’l’就有dp[0] 个"il"组合,以此类推

附上代码

  • 代码1
    自己写的,代码有点儿长
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int INF=0x3f3f3f;
const int M=20010905;
int dp[10];
string s;
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);cout.tie(0);
 
	cin>>s;
	for(int i=0;i<s.length();i++){
		s[i]=tolower(s[i]);
 		if(s[i]=='i') dp[0]++;
  		if(s[i]=='l') dp[1]=(dp[1]+dp[0])%M;
 		if(s[i]=='o') dp[2]=(dp[2]+dp[1])%M;
 		if(s[i]=='v') dp[3]=(dp[3]+dp[2])%M;
 		if(s[i]=='e') dp[4]=(dp[4]+dp[3])%M;
 		if(s[i]=='y') dp[5]=(dp[5]+dp[4])%M;
 		if(s[i]=='o') dp[6]=(dp[6]+dp[5])%M;
 		if(s[i]=='u') dp[7]=(dp[7]+dp[6])%M;
	}
 	cout<<dp[7]<<endl;
	return 0;
}
  • 代码2
    网上看到一位大佬写的,比较精简
#include <bits/stdc++.h>
using namespace std; 
typedef long long ll;
const ll mod = 20010905;
ll dp[10];
string t = ".iloveyou";
string s;
int main(){
    ios::sync_with_stdio(0);    
    cin.tie(0);    
    cin >> s;    
    int n = s.size();    
    dp[0] = 1;    
    for (int i = 0; i < n; i++){
         char c = tolower(s[i]); 
         for (int j = 0; j <= 8; j++){ 
              if(c == t[j]){  
                  dp[j] = (dp[j] + dp[j-1]) % mod;            
              }        
          }    
    }    
     cout << dp[8];    
     return 0;}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值