传送门:牛客
共一行:一封若干个字符的情书(大小写不敏感)。
情书不会超过684594个字符(大写、小写字母)。
共一行:包含一个整数,即iloveyou在情书中作为子序列出现的次数。
由于答案可能很大,请输出对20010905取模后的值。
输入:
IloveyouNotonlyforwhatyouareButforwhatIamWhenIamwithyouIloveyouNotonlyforwhatYouhavemadeofyourselfButforwhatYouaremakingofme
输出:
2864
主要思路:
- 首先注意求的是子串的数量,子串是可以分开的,比如
ilkove
同样算包含了ilove
这一个子串 - 这道题虽然是一道一星题,并且放在了模拟的题单部分,但是是一道实实在在的
dp
题,可以认真对待 - 下面讲讲dp的主要实现过程,我们假设存在一个位置pos,在pos之前存在
ilove
的子串有a个,那么假设pos位置是y
,那么到pos位置我们就会拥有ilovey
这样的子串a个,并且之后再碰到iloveyou
中的其他部分,都只要加上前面子串的数量即可,可以笼统的这么理解,每一个子串的数量就等于该子串去掉最后一个字符的子串数量乘上最后一个子串的数量(当然这里需要保证最后一个字符是在最后的,当然如何保证后面的字符不会被后面中的前面字符干扰呢,我们采用逐个读入的方法,保证了后面的不会影响前面的)
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <string.h>
#include <stack>
using namespace std;
typedef long long ll;
#define inf 0x3f3f3f3f
#define root 1,n,1
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
inline ll read() {
ll x=0,w=1;char ch=getchar();
for(;ch>'9'||ch<'0';ch=getchar()) if(ch=='-') w=-1;
for(;ch>='0'&&ch<='9';ch=getchar()) x=x*10+ch-'0';
return x*w;
}
#define maxn 1000000
const int mod=20010905;
string iLove="iloveyou";
int f[8];
int main() {
char a;
memset(f,0,sizeof(f));f[0]=1;
while(scanf("%c",&a)!=EOF) {
for(int i=0;i<8;i++) {
if(a==iLove[i]||a+32==iLove[i]) {
f[i+1]=(f[i+1]+f[i])%mod;
}
}
}
cout<<f[8]<<endl;
}