[lintcode] 79.最长公共子串[Medium]

描述

给出两个字符串,找到最长公共子串,并返回其长度。

 

样例

给出A=“ABCD”,B=“CBCE”,返回 2

挑战

O(n x m) time and memory.

 

解决思路

是典型的动态规划问题。一开始和字符串匹配搞混了。

csize[i][j]表示以A[i], B[j] 结尾的最长序列(不论是在什么地方开头的)。

csize[i][j] = csize[i-1][j-1] + 1 当且仅当 A[i] = B[j]

csize[i][j] = 0 , 当且仅当 A[i] != B[j]

class Solution {
public:
    /**
     * @param A: A string
     * @param B: A string
     * @return: the length of the longest common substring.
     */
    int longestCommonSubstring(string &A, string &B) {
        // write your code here
        int posA = 0;
        int posB = 0;
        int max=0;
        
        if(A.size() == 0 || B.size() == 0)
        {
            return 0;
        }
        
        int** csize = new int*[A.size()+1];
        for(int i=0; i<=A.size(); i++)
        {
            csize[i] = new int[B.size()+1];
        }

            
            for(; posB < B.size(); ++posB)
            {
                if(A[0] == B[posB])
                {
                    csize[0][posB] = 1;
                    max = 1;
                }
                else
                {
                    csize[0][posB] = 0;
                }
            }
            
            for(; posA < A.size(); ++posA)
            {
                if(A[posA] == B[0])
                {
                    csize[posA][0] = 1;
                    max = 1;
                }
                else
                {
                    csize[posA][0] = 0;
                }
            }
            
            
        for(posA = 1; posA < A.size(); ++posA )
        {

            for(posB = 1; posB < B.size(); ++posB)
            {
                if(A[posA] == B[posB])
                {
                    csize[posA][posB] = csize[posA-1][posB-1]+1;
                    if(csize[posA][posB] > max)
                    {
                        max = csize[posA][posB];
                    }
                }
                else
                {
                    csize[posA][posB] = 0;
                }
            }
        }
        
        return max;

    }
};

 

回答: 最长回文子串可以通过两种方法来实现。第一种是使用中心扩展法,代码如下: ```python class Solution: def check(self, s, l, r): while l >= 0 and r < len(s) and s[l == s[r]: l -= 1 r += 1 return l + 1, r - 1 def longestPalindrome(self, s: str) -> str: start, end = 0, 0 for x in range(len(s)): l1, r1 = self.check(s, x, x) l2, r2 = self.check(s, x, x + 1) if r1 - l1 > end - start: start, end = l1, r1 if r2 - l2 > end - start: start, end = l2, r2 return s[start:end+1] ``` 第二种方法是使用动态规划,代码如下: ```python class Solution: def longestPalindrome(self, s: str) -> str: res = '' for i in range(len(s)): start = max(0, i - len(res) - 1) temp = s[start:i+1] if temp == temp[::-1]: res = temp else: temp = temp<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [5. 最长回文子串(Python 实现)](https://blog.csdn.net/d_l_w_d_l_w/article/details/118861851)[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^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [LeetCode(Python3)5.最长回文子串](https://blog.csdn.net/weixin_52593484/article/details/124718655)[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^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [力扣 (LeetCode)刷题笔记5.最长回文子串 python](https://blog.csdn.net/qq_44672855/article/details/115339324)[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^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值