题解 | 躲藏
躲藏
https://www.nowcoder.com/practice/b19eda9490d54505a1065a70c3005b47
dp数组的含义:匹配到子串当前位置的子序列的个数。也就是说dp[2]意思是匹配到'b'这个字符的子序列个数是多少,匹配到b,那么前面一定是有cw的,所以当前dp[2]+=dp[1]
状态转移方程为:dp[i]+=dp[i-1]但是注意若当前字符与前面有字符相同,也就是这里判断到c时dp[0]要继续自增,原因是这个c也可以作为开头的c,所以这题如果上升一点难度就是在子串中间多加几个相同的。
最后输出dp[3]也就是匹配到子串末尾的子序列个数。
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define int long long
#define ld long double
const int mod = 2000120420010122;
void solve() {
string s;
while (cin >> s) {
vector<int>dp(4);
int ans = 0;
for (auto p : s) {
if (p == 'c' || p == 'C') {
dp[0]++;
dp[3] += dp[2];
} else if (p == 'w' || p == 'W') {
dp[1] += dp[0];
} else if (p == 'b' || p == 'B') {
dp[2] += dp[1];
}
}
cout << dp[3] % mod << '\n';
}
}
signed main() {
ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
solve();
return 0;
}

SHEIN希音公司福利 370人发布