[Codeforces Round #674 (Div. 3)]- F. Number of Subsequences(动态规划DP)
[Codeforces Round #674 (Div. 3)]- F. Number of Subsequences(动态规划DP)
题面:
题意:
给定一个长度为\(\mathit n\)的字符串\(str\),只包含四种字符\('a','b','c','?'\)。 其中\('?'\)字符可以替换成\('a','b','c'\) 中的任意一个。
现在问你所有可能替换成的字符串中,字符串"abc"作为子序列会出现多少次?
思路:
设\(dp[i][0]\)代表到字符串的第\(\mathit i\) 个位置时,可以匹配到字符串"abc"中‘a' 多少次。
设\(dp[i][1]\)代表到字符串的第\(\mathit i\) 个位置时,可以匹配到字符串"abc"中‘b' 多少次。
设\(dp[i][2]\)代表到字符串的第\(\mathit i\) 个位置时,可以匹配到字符串"abc"中‘c' 多少次。
设\(base\) 代表到字符串的第\(\mathit i\) 个位置时,一共有可能有多少种字符串。
显然答案应该为\(dp[n][2]\)。
转移参考代码即可。
代码:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <bits/stdc++.h>
#define ALL(x) (x).begin(), (x).end()
#define sz(a) int(a.size())
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define chu(x) if(DEBUG_Switch) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
#define du3(a,b,c) scanf("%d %d %d",&(a),&(b),&(c))
#define du2(a,b) scanf("%d %d",&(a),&(b))
#define du1(a) scanf("%d",&(a));
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
ll powmod(ll a, ll b, ll MOD) { if (a == 0ll) {return 0ll;} a %= MOD; ll ans = 1; while (b) {if (b & 1) {ans = ans * a % MOD;} a = a * a % MOD; b >>= 1;} return ans;}
ll poww(ll a, ll b) { if (a == 0ll) {return 0ll;} ll ans = 1; while (b) {if (b & 1) {ans = ans * a ;} a = a * a ; b >>= 1;} return ans;}
void Pv(const vector<int> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%d", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("\n");}}}
void Pvl(const vector<ll> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%lld", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("\n");}}}
inline long long readll() {long long tmp = 0, fh = 1; char c = getchar(); while (c < '0' || c > '9') {if (c == '-') fh = -1; c = getchar();} while (c >= '0' && c <= '9') tmp = tmp * 10 + c - 48, c = getchar(); return tmp * fh;}
inline int readint() {int tmp = 0, fh = 1; char c = getchar(); while (c < '0' || c > '9') {if (c == '-') fh = -1; c = getchar();} while (c >= '0' && c <= '9') tmp = tmp * 10 + c - 48, c = getchar(); return tmp * fh;}
void pvarr_int(int *arr, int n, int strat = 1) {if (strat == 0) {n--;} repd(i, strat, n) {printf("%d%c", arr[i], i == n ? '\n' : ' ');}}
void pvarr_LL(ll *arr, int n, int strat = 1) {if (strat == 0) {n--;} repd(i, strat, n) {printf("%lld%c", arr[i], i == n ? '\n' : ' ');}}
const int maxn = 200010;
const int inf = 0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
#define DEBUG_Switch 0
const ll mod = 1e9 + 7ll;
ll dp[maxn][3];
char s[maxn];
int n;
int main()
{
#if DEBUG_Switch
freopen("C:\\code\\input.txt", "r", stdin);
#endif
//freopen("C:\\code\\output.txt","w",stdout);
scanf("%d", &n);
scanf("%s", s + 1);
ll base = 1ll;
repd(i, 1, n)
{
if (s[i] == 'a')
{
dp[i][0] = (dp[i - 1][0] + base) % mod;
dp[i][1] = dp[i - 1][1];
dp[i][2] = dp[i - 1][2];
}
if (s[i] == 'b')
{
dp[i][1] = (dp[i - 1][1] + dp[i - 1][0]) % mod;
dp[i][0] = dp[i - 1][0];
dp[i][2] = dp[i - 1][2];
}
if (s[i] == 'c')
{
dp[i][1] = dp[i - 1][1];
dp[i][0] = dp[i - 1][0];
dp[i][2] = (dp[i - 1][2] + dp[i - 1][1]) % mod;
}
if (s[i] == '?')
{
dp[i][0] = (dp[i - 1][0] * 3ll + base) % mod;
dp[i][1] = (dp[i - 1][1] * 3ll + dp[i - 1][0]) % mod;
dp[i][2] = (dp[i - 1][2] * 3ll + dp[i - 1][1]) % mod;
base = base * 3ll % mod;
}
// printf("%lld %lld %lld\n", dp[i][0], dp[i][1], dp[i][2] );
}
printf("%lld\n", dp[n][2] );
return 0;
}
本博客为本人原创,如需转载,请必须声明博客的源地址。
本人博客地址为:www.cnblogs.com/qieqiemin/
希望所写的文章对您有帮助。