Codeforces 1426F Number of Subsequences dp计数

这篇博客探讨了一个字符串处理问题,其中包含'abc'和'?'字符。作者使用动态规划方法计算以'abc'结尾的子序列数量。对于不含'?'的字符串,通过维护状态转移数组来计数。当遇到'?'时,每个'?'会增加三种可能的分支,因此方案数会按3的指数增长。最终,博主给出了完整的C++代码实现并求得结果对特定模数的余数。
摘要由CSDN通过智能技术生成

题目链接
好久没打div3了 写虚拟场居然能AK了 挺简单的dp

给了我们一个字符串 含有abc 和? ?可以变化为a b c
问有多少个子序列是abc
由于? 会引出三个分支, 也就是说每多一个 ? 方案数就会扩大3^k
先考虑下不含有?的字符串如何对abc计数
令dp[flag][i] 表示在第i位 为flag种字符时 的贡献
那么 - - 害 直接搬我的英文题解上来 太懒了

We are given a string with the length of N, and the position containing “?” can be replaced by “a” or “B” or “C”. Finally, how many subsequences of “ABC” can be obtained, and the modulus of its number is 100000007

First of all, let’s consider the case that there is no “?” in the string. Define DP [s [J] — a '+ 1]] [J] to represent the contribution of the current character, and j to the position of the character in the string. For example, if the current character is “a”, then DP [1] [J] = (DP [1] [J-1] + pre [CNT])% mods The pre array represents the contribution to “a” after the replacement of “?, which is equivalent to the conversion of”? Into three branches. Similarly, for DP [2] [J] = (DP [2] [J-1] + DP [1] [J-1])% mods represents the contribution made by the previous “a” when the current character is “B”. Similarly, for the character “C”, since “?” produces three branches, we have to multiply the contribution of “?” by 3
94214264 Please give me some advice if there is something wrong

附代码

 #include<bits/stdc++.h>
#include<stdlib.h>
#include<algorithm>
#include<stdio.h>
#include<string.h>
#include<queue>
#include<time.h>
#include <cstdio>
#include <iostream>
#include <vector>
#define ll long long
#define int long long
#define inf 0x3f3f3f3f
#define mods 1000000007
#define modd 998244353
#define PI acos(-1)
#define fi first
#define se second
#define lowbit(x) (x&(-x))
#define mp make_pair
#define pb push_back
#define si size()
#define E exp(1.0)
#define fixed cout.setf(ios::fixed)
#define fixeds(x) setprecision(x)
#define IOS ios::sync_with_stdio(false);cin.tie(0)
 using namespace std;
 ll gcd(ll a,ll b){if(a<0)a=-a;if(b<0)b=-b;return b==0?a:gcd(b,a%b);}
template<typename T>void read(T &res){bool flag=false;char ch;while(!isdigit(ch=getchar()))(ch=='-')&&(flag=true);
for(res=ch-48;isdigit(ch=getchar());res=(res<<1)+(res<<3)+ch - 48);flag&&(res=-res);}
ll lcm(ll a,ll b){return a*b/gcd(a,b);}
ll qp(ll a,ll b,ll mod){ll ans=1;if(b==0){return ans%mod;}while(b){if(b%2==1){b--;ans=ans*a%mod;}a=a*a%mod;b=b/2;}return ans%mod;}//快速幂%
ll qpn(ll a,ll b, ll p){ll ans = 1;a%=p;while(b){if(b&1){ans = (ans*a)%p;--b;}a =(a*a)%p;b >>= 1;}return ans%p;}//逆元   (分子*qp(分母,mod-2,mod))%mod;

ll dp[4][222222];
char s[222222];
ll pre[222222];
signed main(){
ll n;
read(n);
scanf("%s",s+1);
ll cnt=0;
pre[0]=1;
for(int i=1;i<=n;i++){
    pre[i]=pre[i-1]*3%mods; //处理3^k
}
for(int i=1;i<=n;i++){
for(int j=1;j<=3;j++){
  dp[j][i]=dp[j][i-1];
}
if(s[i]=='a'){
dp[1][i]=(dp[1][i-1]+pre[cnt])%mods;
}
if(s[i]=='b'){
dp[2][i]=(dp[2][i-1]+dp[1][i-1])%mods;
}
if(s[i]=='c'){
dp[3][i]=(dp[2][i-1]+dp[3][i-1])%mods;
}
if(s[i]=='?'){
dp[1][i]=(dp[1][i-1]*3+pre[cnt])%mods;
dp[2][i]=(dp[2][i-1]*3+dp[1][i-1])%mods;
dp[3][i]=(dp[2][i-1]+dp[3][i-1]*3)%mods;
cnt++;
}

}
printf("%lld\n",dp[3][n]%mods);


}
 
Python网络爬虫与推荐算法新闻推荐平台:网络爬虫:通过Python实现新浪新闻的爬取,可爬取新闻页面上的标题、文本、图片、视频链接(保留排版) 推荐算法:权重衰减+标签推荐+区域推荐+热点推荐.zip项目工程资源经过严格测试可直接运行成功且功能正常的情况才上传,可轻松复刻,拿到资料包后可轻松复现出一样的项目,本人系统开发经验充足(全领域),有任何使用问题欢迎随时与我联系,我会及时为您解惑,提供帮助。 【资源内容】:包含完整源码+工程文件+说明(如有)等。答辩评审平均分达到96分,放心下载使用!可轻松复现,设计报告也可借鉴此项目,该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的。 【提供帮助】:有任何使用问题欢迎随时与我联系,我会及时解答解惑,提供帮助 【附带帮助】:若还需要相关开发工具、学习资料等,我会提供帮助,提供资料,鼓励学习进步 【项目价值】:可用在相关项目设计中,皆可应用在项目、毕业设计、课程设计、期末/期中/大作业、工程实训、大创等学科竞赛比赛、初期项目立项、学习/练手等方面,可借鉴此优质项目实现复刻,设计报告也可借鉴此项目,也可基于此项目来扩展开发出更多功能 下载后请首先打开README文件(如有),项目工程可直接复现复刻,如果基础还行,也可在此程序基础上进行修改,以实现其它功能。供开源学习/技术交流/学习参考,勿用于商业用途。质量优质,放心下载使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值