2019牛客暑期多校训练营(第五场) subsequence 1

链接:https://ac.nowcoder.com/acm/contest/885/G
来源:牛客网
 

时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 262144K,其他语言524288K
64bit IO Format: %lld

题目描述

You are given two strings s and t composed by digits (characters '0' ∼\sim∼ '9'). The length of s is n and the length of t is m. The first character of both s and t aren't '0'.
 

Please calculate the number of valid subsequences of s that are larger than t if viewed as positive integers. A subsequence is valid if and only if its first character is not '0'.

Two subsequences are different if they are composed of different locations in the original string. For example, string "1223" has 2 different subsequences "23".


Because the answer may be huge, please output the answer modulo 998244353.

输入描述:

The first line contains one integer T, indicating that there are T tests.

Each test consists of 3 lines.

The first line of each test contains two integers n and m, denoting the length of strings s and t.

The second line of each test contains the string s.

The third line of each test contains the string t.

* 1≤m≤n≤30001 \le m \le n \le 30001≤m≤n≤3000.

* sum of n in all tests ≤3000\le 3000≤3000.

 

* the first character of both s and t aren't '0'.
 

输出描述:

For each test, output one integer in a line representing the answer modulo 998244353.

示例1

输入

复制

3
4 2
1234
13
4 2
1034
13
4 1
1111
2

输出

复制

9
6
11

说明

For the last test, there are 6 subsequences "11", 4 subsequcnes "111" and 1 subsequence "1111" that are valid, so the answer is 11.

题目大意:给出s串和t串,求s有多少个子序列大于t。

解题思路:容易想到,只解决长度相等的子序列,用组合数处理长度大于t串的子序列。

长度相等的就用去搜索。

搜索的过程是:

枚举S的位置i,和T的位置j

分两种情况:

1。如当S[i]==T[j]我们就看i后边有多少个长度为(n-i)的子序列>= (j+1到m所组成的数)

举个例子:比如

122345645

1245

我们发现前两位相等,我们就去找i的第三位到第n位有多少个子序列大于等于45所组成的数。

2.如果当前s[i]>T[j]的话,因为前边一定是大于等于的,所以后边就随便取喽。

容易发现这是一个递归处理的过程,并且可以记忆化。。。。

在我T了10几发后依然没发现错误。。。。。

#include<bits/stdc++.h>
#define LL long long
#define pb(x) push_back(x)
#define inf 0x3f3f3f3f
#define sca(x) scanf("%d",&x)
#define lowb(x) (x&(-x))
  
  
using namespace std;
  
const int N  = 3005+5;
const int mod = 998244353;
  
char S[N],T[N];
LL c[N][N];
int sum[13][N];
vector<int>idx;
int n,m;
  
  
void init()
{
    c[0][0]=1;
    for(int i=1; i<=3001; i++)
    {
        c[i][0]=1;
        for(int j=1; j<=i; j++)
        {
            c[i][j]=(c[i-1][j-1]+c[i-1][j])%mod;
        }
    }
}
  
void add(int id,int x,int val)
{
    for(int i=x; i; i-=lowb(i))
    {
        sum[id][i]+=val;
    }
}
  
int ask(int id,int x)
{
    int ans=0;
    for(int i=x; i<=n; i+=lowb(i))
        ans+=sum[id][i];
    return ans;
}
  
LL Find(int pos,int k)
{
    LL ans=0;
    for(int i=k+1; i<=9; i++)
    {
        ans+=ask(i,pos);
    }
    return ans;
}
  
LL C(int x,int k)
{
    return c[x][k];
}
  
  
LL dp[N][N];
  
LL dfs(int now,int step)
{
    if(n-now<m-step) return 0;
    if(dp[now][step]) return dp[now][step];
  
    if(step==m)
    {
        LL tmp=Find(now,T[step]-'0');
        return tmp;
    }
  
    if(now>n)
    {
        return 0;
    }
  
    LL ans=0;
    if(S[now]>T[step]&&(n-now)>=m-step)
    {
        ans=(ans+C(n-now,m-step))%mod;
        dp[now][m-step+1]=ans;
    }
  
    if(S[now]==T[step]&&step!=m)
    {
        LL del=dfs(now+1,step+1);
        ans=(ans+del)%mod;
        dp[now][m-step+1]=ans;
    }
    LL ss=dfs(now+1,step);
    ans=(ans+ss)%mod;
    return ans;
}
  
int main()
{
    int t;
    sca(t);
    init();
    while(t--)
    {
        sca(n),sca(m);
        scanf("%s%s",S+1,T+1);
        for(int i=1; i<=n; i++)
        {
            for(int j=1; j<=m; j++)
            {
                dp[i][j]=0;
            }
        }
  
        for(int i=1; i<=n; i++)
        {
            int id=S[i]-'0';
            add(id,i,1);
        }
  
        LL tmp=dfs(1,1)%mod;
        LL ans = 0;
        for(int i=1; i<=n; i++)
        {
            if(S[i]!='0')
            {
                for(int j=m; j<=n&&(n-i)>=j; j++)
                {
                    ans=(ans+C(n-i,j))%mod;
                }
            }
        }
        for(int i=1; i<=n; i++)
        {
            int id=S[i]-'0';
            add(id,i,-1);
        }
        ans=(ans+tmp)%mod;
        printf("%lld\n",ans);
    }
}

 

我吐了。。。。。。。

今天帮同学debug的时候突然想到。

我每次都是想处理前边的答案,然后在递归处理后边的答案。。。。

这样的记忆化的效果就不太明显。。。。

于是,调换了一下dfs的位置。

#include<bits/stdc++.h>
#define LL long long
#define pb(x) push_back(x)
#define inf 0x3f3f3f3f
#define sca(x) scanf("%d",&x)
#define lowb(x) (x&(-x))
 
 
using namespace std;
 
const int N  = 3005+5;
const int mod = 998244353;
 
char S[N],T[N];
LL c[N][N];
int sum[13][N];
vector<int>idx;
int n,m;
 
 
void init()
{
    c[0][0]=1;
    for(int i=1; i<=3001; i++)
    {
        c[i][0]=1;
        for(int j=1; j<=i; j++)
        {
            c[i][j]=(c[i-1][j-1]+c[i-1][j])%mod;
        }
    }
}
 
void add(int id,int x,int val)
{
    for(int i=x; i; i-=lowb(i))
    {
        sum[id][i]+=val;
    }
}
 
int ask(int id,int x)
{
    int ans=0;
    for(int i=x; i<=n; i+=lowb(i))
        ans+=sum[id][i];
    return ans;
}
 
LL Find(int pos,int k)
{
    LL ans=0;
    for(int i=k+1; i<=9; i++)
    {
        ans+=ask(i,pos);
    }
    return ans;
}
 
LL C(int x,int k)
{
    return c[x][k];
}
 
 
LL dp[N][N];
 
LL dfs(int now,int step)
{
    if(n-now<m-step) return 0;
    if(dp[now][step]) return dp[now][step];
 
    if(step==m)
    {
        LL tmp=Find(now,T[step]-'0');
        return tmp;
    }
 
    if(now>n)
    {
        return 0;
    }
 
    LL ans=0;
    LL ss=dfs(now+1,step);
    
    if(S[now]>T[step]&&(n-now)>=m-step)
    {
        ans=(ans+C(n-now,m-step))%mod;
    }
 
    if(S[now]==T[step]&&step!=m)
    {
        LL del=dfs(now+1,step+1);
        ans=(ans+del)%mod;
    }
    ans=(ans+ss)%mod;
    dp[now][step]=ans;
    return ans;
}
 
int main()
{
    int t;
    sca(t);
    init();
    while(t--)
    {
        sca(n),sca(m);
        scanf("%s%s",S+1,T+1);
        for(int i=1; i<=n; i++)
        {
            for(int j=1; j<=m; j++)
            {
                dp[i][j]=0;
            }
        }
 
        for(int i=1; i<=n; i++)
        {
            int id=S[i]-'0';
            add(id,i,1);
        }
 
        LL tmp=dfs(1,1)%mod;
        LL ans = 0;
        for(int i=1; i<=n; i++)
        {
            if(S[i]!='0')
            {
                for(int j=m; j<=n&&(n-i)>=j; j++)
                {
                    ans=(ans+C(n-i,j))%mod;
                }
            }
        }
        for(int i=1; i<=n; i++)
        {
            int id=S[i]-'0';
            add(id,i,-1);
        }
        ans=(ans+tmp)%mod;
        printf("%lld\n",ans);
    }
}

我吐了。。。。。。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值