博客
关于我
反恐训练营
阅读量:200 次
发布时间:2019-02-28

本文共 1602 字,大约阅读时间需要 5 分钟。

根据上述分析,我们可以使用动态规划的方法来解决这个问题。具体步骤如下:

  • 初始化一个二维数组 f,大小为 (n+1) x (m+1),其中 n 是子弹和恐怖分子的类型数,m 是序列的长度。
  • 填充边界条件:当 i=0j=0 时,f[i][j] = 0
  • 遍历每个 ij,计算 f[i][j]
    • 如果 a[i] == b[j],则 f[i][j] = max(f[i-1][j], f[i][j-1], f[i-1][j-1] + score[a[i]])
    • 否则,f[i][j] = max(f[i-1][j], f[i][j-1])
  • 最终,f[m][n] 就是炜炜能获得的最大分数。
  • 通过这种方法,我们可以高效地计算出最优解。

    以下是实现代码:

    #include 
    #include
    #include
    #include
    using namespace std;int main() { int n; while (cin >> n) { string a_sub, b_sub; for (int i = 1; i <= n; ++i) { a_sub += tolower(cin >> (char&c)); b_sub += tolower(cin >> (char&c)); } map
    score; for (int i = 1; i <= n; ++i) { int x; cin >> x; score[tolower(a_sub[i-1])] = x; } int len_a = a_sub.size(); int len_b = b_sub.size(); vector
    > dp(len_a + 1, vector
    (len_b + 1, 0)); for (int i = 1; i <= len_a; ++i) { for (int j = 1; j <= len_b; ++j) { if (a_sub[i-1] == b_sub[j-1]) { if (i == 1 || j == 1) { dp[i][j] = score[a_sub[i-1]]; } else { dp[i][j] = dp[i-1][j-1] + score[a_sub[i-1]]; } } else { dp[i][j] = max(dp[i-1][j], dp[i][j-1]); } } } cout << dp[len_a][len_b] << endl; } return 0;}

    代码解释:

  • 输入处理: 读取子弹和恐怖分子的类型数 n,然后读取各自的序列 a_subb_sub。接着读取每个字母对应的得分 score
  • 动态规划初始化: 创建一个二维数组 dp,大小为 (len_a + 1) x (len_b + 1),初始化为0。
  • 填充边界条件:i=1j=1 时,如果 a_sub[i-1] == b_sub[j-1],则 dp[i][j] = score[a_sub[i-1]],否则 dp[i][j] = 0
  • 状态转移: 遍历每个 ij,根据子弹和恐怖分子的当前字符是否匹配,更新 dp[i][j] 的值。
  • 输出结果: 打印最终的最大分数 dp[len_a][len_b]
  • 通过这种方法,我们可以高效地解决问题,并找到炜炜能获得的最大分数。

    转载地址:http://hptn.baihongyu.com/

    你可能感兴趣的文章
    no connection could be made because the target machine actively refused it.问题解决
    查看>>
    No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
    查看>>
    No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
    查看>>
    No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
    查看>>
    No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
    查看>>
    No mapping found for HTTP request with URI [/logout.do] in DispatcherServlet with name 'springmvc'
    查看>>
    No module named 'crispy_forms'等使用pycharm开发
    查看>>
    No module named cv2
    查看>>
    No module named tensorboard.main在安装tensorboardX的时候遇到的问题
    查看>>
    No module named ‘MySQLdb‘错误解决No module named ‘MySQLdb‘错误解决
    查看>>
    No new migrations found. Your system is up-to-date.
    查看>>
    No qualifying bean of type XXX found for dependency XXX.
    查看>>
    No qualifying bean of type ‘com.netflix.discovery.AbstractDiscoveryClientOptionalArgs<?>‘ available
    查看>>
    No resource identifier found for attribute 'srcCompat' in package的解决办法
    查看>>
    no session found for current thread
    查看>>
    No toolchains found in the NDK toolchains folder for ABI with prefix: mips64el-linux-android
    查看>>
    NO.23 ZenTaoPHP目录结构
    查看>>
    no1
    查看>>
    NO32 网络层次及OSI7层模型--TCP三次握手四次断开--子网划分
    查看>>
    NOAA(美国海洋和大气管理局)气象数据获取与POI点数据获取
    查看>>