subsequence 1(牛客多校第五场记忆化搜索+组合数学)
鏈接:https://ac.nowcoder.com/acm/contest/885/G
來源:牛客網
題目描述
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 \le m \le n \le 30001≤m≤n≤3000.
-
sum of n in all tests \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.
當時在寫B題的時候隊友就把這個題討論完了。。結束之后又看了看,覺得挺好的一個題目。分兩種情況,一種是和下面的字符串長度相等的時候,另一種是長于下面字符串的時候。第一種時候我們可以直接寫出來,但是第二種的時候,我們就需要討論了,因為有時候在某一位的數字是一樣的。。可以用dp來做,但是我們隊的dp實在是不咋樣。就用搜索了。一開始就是暴力搜索,T了一發之后改的記憶化搜索。還有用c++11交的時候溢出了,用c++14過得。
代碼如下:
努力加油a啊,(o)/~
總結
以上是生活随笔為你收集整理的subsequence 1(牛客多校第五场记忆化搜索+组合数学)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Basketball Exercise
- 下一篇: Minimum Inversion Nu