Niu Ke-Hiding

Niu Ke-Hiding

Title description

XHRlyb and her little friend Cwbc are playing hide and seek.
Cwbc is hidden in multiple case-insensitive strings.
The curious XHRlyb wants to know how many times Cwbc appears as a subsequence in each string.
Because Cwbc may appear too many times, you only need to output the result of each answer modulo 2000120420010122.
After reading the question carefully, you will be able to solve this problem smoothly!

Enter description

The input data has multiple lines, and each line has a character string.

Output description

The output data should have multiple lines, and each line represents the result of an answer modulo.

Example 1

enter

Cwbc

Output

1

It shows that
Cwbc only appears once as a subsequence.

Example 2

enter

acdcecfwgwhwibjbkblcmcnco

Output

81

It shows that
Cwbc appears as a subsequence 34=81 times.

Topic analysis

This question is obviously a dynamic programming question, dp[i][j], means the number of j matched in the first i characters, that is, dp[i][1] means the letter'c' matches in the first i characters The number of. Then you can list the state transition equation
dp[i][1] = (dp[i][1] + (s[i] =='c' ))% mod;
dp[i][2] = (dp [i][2] + (s[i] =='w') * dp[i-1][1])% mod;
(here multiplying by dp[i-1][1] is because of'w' The number of matches is determined by'c', and the same
applies below) dp[i][3] = (dp[i][3] + (s[i] =='b' )* dp[i-1][ 2])% mod;
dp[i][4] = (dp[i][4] + (s[i] =='c')* dp[i-1][3])% mod;

But in this question, the two-dimensional array will be spaced by the card, so dimensionality reduction is required. Since the current state is only related to the previous state, then the first dimension can be deleted, which becomes:
dp[1] = (dp[ 1] + (s[i] =='c' ))% mod;
dp[2] = (dp[2] + (s[i] =='w' )* dp[1])% mod;
dp [3] = (dp[3] + (s[i] =='b') * dp[2])% mod;
dp[4] = (dp[4] + (s[i] =='c ') * dp[3])% mod;

#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long ll;
const ll mod = 2000120420010122;
char s[1000005];
ll dp[5];

int main(){
    while(cin >> s){
          int l = strlen(s);
          memset(dp, 0, sizeof(dp));
          for (int i = 0; i < l; i++)
              if (s[i] >= 'A' && s[i] <= 'Z')
                  s[i] = s[i] + 32;
          for (int i = 0; i < l; i++){
              if (s[i] == 'c')
                  dp[1] = (dp[1] + 1) % mod;
              else
                  dp[1] = dp[1] % mod;
              if (s[i] == 'w')
                  dp[2] = (dp[2] + 1 * dp[1]) % mod;
              else
                  dp[2] = dp[2] % mod;
              if (s[i] == 'b')
                  dp[3] = (dp[3] + 1 * dp[2]) % mod;
              else
                  dp[3] = dp[3] % mod;
              if (s[i] == 'c')
                  dp[4] = (dp[4] + 1 * dp[3]) % mod;
              else
                  dp[4] = dp[4] % mod;
        }
        cout << dp[4] << endl;  
    }


    return 0;
}

Guess you like

Origin blog.csdn.net/qq_45964820/article/details/115050994