Subsequence 1] [Dynamic Programming

Meaning of the questions:

Two strings, s t, s, find all substrings of the number is greater than t

answer:

  DP [i] [j] denotes the front i-s, prior to the j-th matching the number of kinds of t,
  So if (s [i] == t [j])
      dp[i][j] = dp[i -1][j] + dp[i - 1][j - 1];
    else
      dp[i][j] = dp[i - 1][j]; 
  T is not greater than the length of the leading 0 are met, then look at a length equal to t on it,
  When the match i, j when, if (s [i]> t [j]) then the contribution:
    Front matching category number j-1 * later pick len2-j, namely the contribution of the current to dp [i - 1] [j - 1] * C [len1 - i] [len2 - j].
 

 
 
. 1 #include <bits / STDC ++ H.>
 2  the using  namespace STD;
 . 3 typedef Long  Long LL;
 . 4  const  int N = 3005 ;
 . 5  const LL MOD = 998 244 353 ;
 . 6  char S [N], T [N];
 . 7  LL C [N] [N];
 . 8  LL DP [N] [N];
 . 9  int main ()
 10  {
 . 11      // play table Pascal's triangle 
12 is      C [ 0 ] [ 0 ] = . 1 ;
 13 is      C [ . 1][0] = C[1][1] = 1 ;
14     for(int i=2;i<=3000;i++){
15         C[i][0] = 1 ;
16         for(int j=1;j<=i;j++){
17             C[i][j] = C[i-1][j-1] + C[i-1][j] ;
18             if(C[i][j]>=mod) C[i][j] -= mod ;
19         }
20     }
21 
22 is      int T, n-, m;
 23 is      for (Scanf ( " % D " , & T); T; T-- ) {
 24          Scanf ( " % D% D " , & n-, & m);
 25          Scanf ( " % S % S " , S + . 1 , T + . 1 );
 26 is  
27          for ( int I = 0 ; I <= n-; I ++ ) {
 28              DP [I] [ 0 ] = . 1 ;
 29          }
 30          // length of the same, a a position larger than the position t, the contribution to the answer of 
31         ll ans = 0 ;
32         for(int i=1;i<=n;i++){
33             for(int j=1;j<=min(i,m);j++){
34                 dp[i][j] = dp[i-1][j] ;
35                 if( s[i] == t[j] ){
36                     dp[i][j] = (dp[i][j] + dp[i-1][j-1]) % mod ;
37                 }
38                 else if( s[i] > t[j] ){
39                     = ANS (ANS + (LL) DP [I- . 1 ] [J- . 1 ] * C [Ni] [MJ])% MOD;
 40                  }
 41 is              }
 42 is          }
 43 is          // Length of run length greater than t, answers the contribution of 
44 is          for ( int I = . 1 ; I <= n-; I ++ ) {
 45              IF (S [I] == ' 0 ' ) Continue ;
 46 is              for ( int J = m; J <= Ni; J ++ ) {
 47                  = ANS (ANS C + [Ni] [J])% MOD;
 48              }
49         }
50         printf("%lld\n",ans);
51     }
52     return 0;
53 }
View Code

 

 
 
 

Guess you like

Origin www.cnblogs.com/Osea/p/11297874.html