[Nowcoder] 2019 Niu off summer multi-school training camp (the fifth) G-subsequence 1 | string dp

Title link: https://ac.nowcoder.com/acm/contest/885/G

Main idea:

Give a length of nnstringss of ns , and the length ismmstring of m ttt , askssHow many subsequences in s are greater thanttafter a decimal operationt is the result of a decimal operation.

Question idea:

The freshman Niu Ke has many schools, and now I make up the question, I even miss it

First of all, it is easy to think of if the length of the subsequence is greater than m, and the first character is not 0, then it must be possible

This part is easy to find out from dp, so no extra explanation will be given.

So the answer becomes the part calculated above + a subsequence of length equal to m and meeting the requirements

Then let

dp [i] [k] represents the number of schemes where the first i character of s is equal to the first k characters of t dp[i][k] represents the number of schemes where the first i character of s is equal to the first k characters of t D P [ i ] [ k ] Table shows s the front i character symbols and t the front k th character symbols phases like the square case number

It is easy to get the state transition equation:

d p [ i ] [ k ] = d p [ i ] [ k ] + d p [ i − 1 ] [ k − 1 ] , s [ i ] = = t [ k ] dp[i][k] = dp[i][k] + dp[i-1][k-1] ,s[i] == t[k] dp[i][k]=dp[i][k]+dp[i1][k1],s[i]==t[k]

So that 相等the number of solutions can be calculated

How to calculate the number of plans greater than?
Then considering the final state, it is clear that in the end there must be part equal and part greater than

So you can enumerate every 分界点iii , each demarcation pointiii can be used as thekth, {1 <= k <= m} k,\{1<=k<=m\}k,{ 1<=k<=m } positions, for each position there will beC (n − i, m − k) C(ni,mk)C(ni,mk ) kinds of choices, so you can get the contribution of the final answer

This is the second solution of a problem, and of course there is the first one 大一写的,思维不一样竟然推出了不一样的方程: https://blog.csdn.net/qq_43857314/article/details/98212272

Code:

/*** keep hungry and calm CoolGuang!  ***/
/*#pragma GCC optimize("Ofast","unroll-loops","omit-frame-pointer","inline")
#pragma GCC optimize(3)*/
#include <bits/stdc++.h>
#include<stdio.h>
#include<queue>
#include<algorithm>
#include<string.h>
#include<iostream>
#define debug(x) cout<<#x<<":"<<x<<endl;
#define d(x) printf("%lld\n",x);
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
const ll INF= 1e17;
const ll maxn = 4e5+700;
const int mod= 998244353;
const int up = 1e9;
template<typename T>inline void read(T &a){
    
    char c=getchar();T x=0,f=1;while(!isdigit(c)){
    
    if(c=='-')f=-1;c=getchar();}
while(isdigit(c)){
    
    x=(x<<1)+(x<<3)+c-'0';c=getchar();}a=f*x;}
ll n,m,p;
ll dp[3005][3005];///A的前i个,匹配了B的前j个de'd
ll ta[3005][3005];
ll c[3005][3005];
char s[maxn],t[maxn];
ll cal(ll x,ll y){
    
    
    if(y>x) return 0;
    if(y == 0 || x == y) return c[x][y] = 1;
    if(y == 1) return c[x][y] = x%mod;
    if(~c[x][y]) return c[x][y];
    return c[x][y] = (cal(x-1,y) + cal(x-1,y-1))%mod;
}
int main(){
    
    
    memset(c,-1,sizeof(c));
    int T;scanf("%d",&T);
    while(T--){
    
    
        read(n);read(m);
        scanf("%s%s",s+1,t+1);
        dp[0][0] = 1;
        ll ans = 0;
        for(int i=1;i<=n;i++){
    
    
            for(int k=0;k<=m;k++) dp[i][k] = dp[i-1][k];
            for(int k=1;k<=m;k++){
    
    
                if(s[i] == t[k])
                    dp[i][k] = (dp[i][k] + dp[i-1][k-1])%mod;
                else if(s[i] > t[k])
                   ans  = (ans + dp[i-1][k-1]*cal(n-i,m-k))%mod;
            }
        }
        dp[0][0] = 1;
        for(int i=1;i<=n;i++){
    
    
            for(int k=0;k<=n;k++) dp[i][k] = dp[i-1][k];
            if(s[i] != '0') dp[i][1]++; 
            for(int k=2;k<=n;k++) dp[i][k] = (dp[i][k] + dp[i-1][k-1])%mod;
        }
        for(int i=m+1;i<=n;i++) ans = (ans + dp[n][i])%mod;
        printf("%lld\n",ans);
    }
    return 0;
}

/***
5 7
1 2 3 4 5
1 2
2 3
3 4
4 5

3 1
3 2
2 1 5
1 2
2 1 3
3 1
3 5
****/

Guess you like

Origin blog.csdn.net/qq_43857314/article/details/111702676