Loading

NOIP 模拟 $14\; \text{抛硬币}$

题解 \(by\;\;zj\varphi\)

签到题,自己看题解

Code
#include<bits/stdc++.h>
#define ri register signed
#define p(i) ++i
using namespace std;
namespace IO{
    char buf[1<<21],*p1=buf,*p2=buf;
    #define gc() p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++
    template<typename T>inline void read(T &x) {
        ri f=1;x=0;register char ch=gc();
        while(ch<'0'||ch>'9') {if (ch=='-') f=0;ch=gc();}
        while(ch>='0'&&ch<='9') {x=(x<<1)+(x<<3)+(ch^48);ch=gc();}
        x=f?x:-x;
    }
}
using IO::read;
namespace nanfeng{
    #define cmax(x,y) ((x)>(y)?(x):(y))
    #define cmin(x,y) ((x)>(y)?(y):(x))
    #define FI FILE *IN
    #define FO FILE *OUT
    static const int N=3e3+7,MOD=998244353;
    int dp[N][N],lst[26],n,len;
    char s[N];
    inline int main() {
        // FI=freopen("nanfeng.in","r",stdin);
        // FO=freopen("nanfeng.out","w",stdout);
        scanf("%s",s+1);
        read(n);
        len=strlen(s+1);
        dp[0][0]=dp[1][0]=dp[1][1]=1;
        lst[s[1]-'a']=1;
        for (ri i(2);i<=len;p(i)) {
            ri lm=cmin(i,n),ls=lst[s[i]-'a'];
            // printf("%c %d\n",s[i],ls);
            dp[i][0]=1;
            for (ri j(1);j<=lm;p(j)) {
                dp[i][j]=dp[i-1][j]+dp[i-1][j-1];
                if (ls-1>=0) dp[i][j]-=dp[ls-1][j-1];
                dp[i][j]=(dp[i][j]%MOD+MOD)%MOD;
            }
            lst[s[i]-'a']=i;
        }
        printf("%d\n",dp[len][n]);
        return 0;
    } 
}
int main() {return nanfeng::main();}
posted @ 2021-07-16 09:36  ナンカエデ  阅读(33)  评论(0编辑  收藏  举报