String Game Gym - 102800C(动规)

本文介绍了一种算法,用于计算一个字符串作为另一个字符串子序列出现的次数。特别地,讨论了如何通过动态规划解决这个问题,并给出了解决方案的代码实现。

String Game
Input file: standard input
Output file: standard output
Time limit: 1 second
Memory limit: 256 megabytes
Clair and Bob play a game. Clair writes a string of lowercase characters, in which Bob sets the puzzle
by selecting one of his favorite subsequence as the key word of the game. But Bob was so stupid that he
might get some letters wrong.
Now Clair wants to know whether Bob’s string is a subsequence of Clair’s string and how many times
does Bob’s string appear as a subsequence in Clair’s string. As the answer may be very large, you should
output the answer modulo 109 + 7.
Input
The first line is Clair’s string (whose length is no more than 5000), and the second line is Bob’s string
(whose length is no more than 1000).
Output
Output one line, including a single integer representing how many times Bob’s string appears as a
subsequence in Clair’s string. The answer should modulo 109 + 7.
Examples

输入:
eeettt
et
输出:
9
输入:
eeettt
te
输出:
0

#include <bits/stdc++.h>
using namespace std;
const int M = 1e9 + 7;
char a[5005], b[1005];
int dp[1005][5005];   //第一位存已经匹配到的数位,第二位存当前进行到的数位
int n, m, i, j;
int main() 
{
    scanf("%s%s", a + 1, b + 1);
    memset(dp, 0, sizeof(dp));
    n = strlen(a + 1);
    m = strlen(b + 1);
    for (i = 1; i <= m; i++) 
    {
        for (j = 1; j <= n; j++) 
        {
            if (a[j] == b[i]) 
            {
                if (i == 1)
                    dp[i][j] = (1 + dp[i][j - 1]) % M;
                else
                    dp[i][j] = (dp[i - 1][j - 1] + dp[i][j - 1]) % M;
            } 
            else 
            {
                dp[i][j] = dp[i][j - 1];
            }
        }
    }
    printf("%d\n", dp[m][n]);
}

------------------------------------------------------------ av\logging.pyx:216:22: Cannot assign type 'const char *(void *) except? NULL nogil' to 'const char *(*)(void *) noexcept nogil'. Exception values are incompatible. Suggest adding 'noexcept' to the type of 'log_context_name'. Error compiling Cython file: ------------------------------------------------------------ ... # Start the magic! # We allow the user to fully disable the logging system as it will not play # nicely with subinterpreters due to FFmpeg-created threads. if os.environ.get('PYAV_LOGGING') != 'off': lib.av_log_set_callback(log_callback) ^ ------------------------------------------------------------ av\logging.pyx:351:28: Cannot assign type 'void (void *, int, const char *, va_list) except * nogil' to 'av_log_callback' (alias of 'void (*)(void *, int, const char *, va_list) noexcept nogil'). Exception values are incompatible. Suggest adding 'noexcept' to the type of 'log_callback'. Compiling av\logging.pyx because it changed. [1/1] Cythonizing av\logging.pyx Traceback (most recent call last): File "D:\software\Anaconda\envs\Game\lib\site-packages\pip\_vendor\pyproject_hooks\_in_process\_in_process.py", line 389, in <module> main() File "D:\software\Anaconda\envs\Game\lib\site-packages\pip\_vendor\pyproject_hooks\_in_process\_in_process.py", line 373, in main json_out["return_val"] = hook(**hook_input["kwargs"]) File "D:\software\Anaconda\envs\Game\lib\site-packages\pip\_vendor\pyproject_hooks\_in_process\_in_process.py", line 143, in get_requires_for_build_wheel return hook(config_settings) File "C:\Users\admin\AppData\Local\Temp\pip-build-env-6b0uw08f\overlay\Lib\site-packages\setuptools\build_meta.py", line 331, in get_requires_for_build_wheel return self._get_build_requires(config_settings, requirements=[]) File "C:\Users\admin\AppData\Local\Temp\pip-build-env-6b0uw08f\overlay\Lib\site-packages\setuptools\build_meta.py", line 301, in _get_build_requires self.run_setup() File "C:\Users\admin\AppData\Local\Temp\pip-build-env-6b0uw08f\overlay\Lib\site-packages\setuptools\build_meta.py", line 512, in run_setup super().run_setup(setup_script=setup_script) File "C:\Users\admin\AppData\Local\Temp\pip-build-env-6b0uw08f\overlay\Lib\site-packages\setuptools\build_meta.py", line 317, in run_setup exec(code, locals()) File "<string>", line 157, in <module> File "C:\Users\admin\AppData\Local\Temp\pip-build-env-6b0uw08f\overlay\Lib\site-packages\Cython\Build\Dependencies.py", line 1153, in cythonize cythonize_one(*args) File "C:\Users\admin\AppData\Local\Temp\pip-build-env-6b0uw08f\overlay\Lib\site-packages\Cython\Build\Dependencies.py", line 1297, in cythonize_one raise CompileError(None, pyx_file) Cython.Compiler.Errors.CompileError: av\logging.pyx [end of output] note: This error originates from a subprocess, and is likely not a problem with pip. ERROR: Failed to build 'av' when getting requirements to build wheel
最新发布
12-29
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值