码迷,mamicode.com
首页 > 其他好文 > 详细

最长子串问题

时间:2019-09-21 00:48:48      阅读:91      评论:0      收藏:0      [点我收藏+]

标签:code   namespace   turn   else   int   col   pac   公共子串   mes   

//关于最长公共子串的一些简单想法
//  if 求 str1 与 str2 的最长公共子串,可以将str1 每个字符与str2 每个字符建立矩阵 Grape[len1][len2]
//  遍历 如果 str1[i]==str2[j] 则Grape[i][j] = 1
//  因此最长的公共子串为Grape图中最长的1对角线长
//  因此优化下
//  if
//      str1[i] != str2[j] Grape[i][j] = 0;
//  else
//      Grape[i][j] = Grape[i-1][j-1] + 1
// 最长公共子串即为图中最大的值
//  C实现
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
const int maxn = 1e4 + 15;
int dp[maxn][maxn];
int lset(const string& s1,const string& s2)
{
    memset(dp,0,sizeof(dp));
    int maxLen = 0;
    for(int i=1;i<=s1.length();++i)
    {
        for(int j=1;j<=s2.length();++j)
        {
            if(s1[i-1]==s2[j-1])
            {
                dp[i][j] = dp[i-1][j-1] + 1;
                maxLen = max(maxLen,dp[i][j]);
            }
        }
    }
    return maxLen;
}
int main()
{
    string str1,str2;
    while(cin>>str1>>str2)
    {
        cout<<lset(str1,str2)<<endl;
    }
}

 

最长子串问题

标签:code   namespace   turn   else   int   col   pac   公共子串   mes   

原文地址:https://www.cnblogs.com/newstartCY/p/11561112.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!