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