栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

手写算法:最长公共连续子序列

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

手写算法:最长公共连续子序列

参考回答:

int substr(string & str1, string &str2){int len1 = str1.length();int len2 = str2.length();vector<vector<int>>dp(len1,vector<int>(len2,0));for (int i = 0; i < len1; i++){dp[i][0] = str1[i]==str1[0]?1:0;}for (int j = 0; j <= len2; j++){dp[0][j] = str1[0]==str2[j]?1:0;}for (int i = 1; i < len1; i++){for (int j = 1; j < len2; j++){if (str1[i] == str2[j]){dp[i][j] = dp[i - 1][j - 1]+1;}}}int longest = 0;int longest_index = 0;for (int i = 0; i < len1; i++){for (int j = 0; j < len2; j++){if (longest < dp[i][j]){longest = dp[i][j];longest_index = i;}}}//字符串为从第i个开始往前数longest个    for (int i = longest_index-longest+1; i <=longest_index; i++)    {    cout << str1[i] << endl;    }    return longest;}

 

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/365940.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号