华为OJ 75 公共子串计算 动态规划 最长公共子串

1. 题目描述

1.1. Limit

Time Limit: 1000 ms

Memory Limit: 131072 kB

1.2. Problem Description

计算两个字符串的最大公共字串的长度,字符不区分大小写


1.3. Input

输入两个字符串


1.4. Output

输出一个整数


1.5. Sample Input

asdfas
werasdfaswer

1.6. Sample Output

6

1.8. Source

华为OJ 75 公共子串计算


2. 解读

动态规划。 list[i][j]表示以str1[i]和str2[j]结尾的最长公共子串, 递推方程如下。

l i s t [ i ] [ j ] = { l i s t [ i − 1 ] [ j − 1 ] + 1 , ( s t r 1 [ i ] = s t r 2 [ j ] ) 0 , ( s t r 1 [ i ] ≠ s t r 2 [ j ] ) list[i][j] ={\left\{ \begin{aligned} & list[i - 1][j - 1] + 1 & , (str1[i] = str2[j]) \\ & 0 & , (str1[i] \neq str2[j]) \\ \end{aligned}\right. } list[i][j]={list[i1][j1]+10,(str1[i]=str2[j]),(str1[i]=str2[j])

3. 代码

#include<iostream>
using namespace std;

const int MAXM = 1e3 + 1;

int main(){
    string str1, str2;
    cin>>str1>>str2;
    int list[MAXM][MAXM] = {{0}};
    int maxAns = 0;
    for(int i = 1; i <= str1.size(); i++){
        for(int j = 1; j <= str2.size(); j++){
            if(toupper(str1[i-1])==toupper(str2[j-1])){
                // list[i][j]表示以str1[i]和str2[j]结尾的最长公共子串
                list[i][j] = list[i - 1][j - 1] + 1;
                maxAns = max(maxAns, list[i][j]);
            }else{
                list[i][j] = 0;
            }
        }
    }
    cout<<maxAns<<endl;
}

联系邮箱:curren_wong@163.com

CSDN:https://me.csdn.net/qq_41729780

知乎:https://zhuanlan.zhihu.com/c_1225417532351741952

公众号:复杂网络与机器学习

欢迎关注/转载,有问题欢迎通过邮箱交流。

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
动态规划是一种解决复杂问题的算法思想,它将原问题分解为多个子问题,并通过保存子问题的解来避免重复计算,从而实现高效的求解。在Python中,动态规划可以用来解决迷宫问题。 迷宫问题是指在一个迷宫中找到从起点到终点的路径。在动态规划的思想下,可以将迷宫看作一个二维的网格,其中每个网格可以是墙壁或者可以通过的空地。通过定义状态和状态转移方程,可以利用动态规划算法来找到最短路径或者所有可能的路径。 具体地说,可以使用一个二维数组来表示迷宫,其中每个元素代表一个网格的状态。然后,通过定义初始状态、状态转移方程和边界条件,可以逐步计算出所有网格的最优解,最终得到从起点到终点的最短路径或者所有可能的路径。 在解决迷宫问题时,可以利用递归或者迭代的方式进行求解。递归方式可以通过定义递归函数来实现,每次递归调用时,根据当前位置和当前状态,判断是否继续向下一步前进,并更新状态。迭代方式则可以使用动态规划的思想,通过迭代更新网格的状态,直到达到终点。 参考资料中提供了一份关于动态规划迷宫问题的python代码,你可以通过阅读该代码来了解具体的实现方法。同时,你也可以通过搜索其他相关资料来深入学习和了解动态规划在解决迷宫问题中的应用。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [动态规划(DP)的整理-Python描述](https://blog.csdn.net/MrLevo520/article/details/75676160)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [Python解法华为OJ机试-迷宫问题](https://blog.csdn.net/qq_39802810/article/details/115522203)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值