3.28 每日一题题解

tokitsukaze and Segmentation

涉及知识点:

  • dp的优化/组合数

solution:

  • 首先要道个歉,这个题目难度有点大QAQ
  • 用dp[i]表示字符串前缀s1s2....si,这个子串的分割方案数
  • 如果遇到s[i]是0,那么在i位置之后所有位置,最后一次分割的位置不能在i之前这个位置,减去字符串前缀si-1的分割方案数dp[i-1]

std:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn = 100005;
const ll mod = 998244353;
char s[maxn];
ll dp[maxn];
int main()
{
    int n;
    scanf("%d",&n);
    scanf("%s",s+1);
    ll ans = 1;
    int cnt = 0;
    dp[0] = 1;
    for(int i=1;i<=n;i++){
        cnt = (cnt + (s[i]-'0'))%3;
        if(cnt)continue ;
        dp[i] = ans ;
        if(s[i] == '0')
            ans = (ans - dp[i-1] + mod)%mod;
        ans = (ans + dp[i])%mod;
    }
    cout<<dp[n]<<endl;
    return 0;
}
原文地址:https://www.cnblogs.com/QFNU-ACM/p/12586514.html