3.28 daily a problem solution to a problem

tokitsukaze and Segmentation

Involving knowledge points:

  • Optimization dp / number combination

solution:

  • First of all routes to apologize, this item difficulty a bit big QAQ
  • Represents the prefix string s1s2 .... si, in this embodiment the number of substrings divided by dp [i]
  • If you encounter s [i] is 0, then all the position after position i, the last position is not divided before this position i, subtracting the division number string prefix scheme si-1 to 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;
}

Guess you like

Origin www.cnblogs.com/QFNU-ACM/p/12586514.html