给定两个串,均由最小字母组成。求这两个串的最大公共字串LCS(Longest Common Substring)。

使用动态规划解决。

#include <iostream>
#include <vector>
#include <cstring>
#include <algorithm> using namespace std; #define MAX 100 int LCS(string left, string right){
int imax = -1;
int m = left.size();
int n = right.size(); int i,j;
int x,y; vector<vector<int> > temp(m, vector<int>(n,0)); for(i=0; i<m; i++){
for(j=0; j<n; j++){
if(left[i] == right[j]){
if (i == 0 || j == 0){
temp[i][j] = 1;
}else{
temp[i][j] = temp[i-1][j-1] + 1;
} if(temp[i][j] > imax){
imax = temp[i][j];
x = i;
y = j;
}
}
}
} /* output the common substring */
i = x, j = y;
int k = imax;
string s(min(m,n),0);
s[k--] = '\0'; while(i>=0 && j>=0){
if(left[i] == right[j]){
s[k--] = left[i];
i--;
j--;
}else{
break;
}
} cout<<s<<endl; return imax;
} int main(){
int result = 0; string query, text;
cin>>query>>text; cout<<query<<endl;
cout<<text<<endl; result = LCS(query, text); cout<<result; return 0;
}

最新文章

  1. AliSQL的编译使用
  2. [NOIP2012] 提高组 洛谷P1083 借教室
  3. C#学习笔记(九)&mdash;&mdash;集合、比较和转换
  4. Beaglebone Black&ndash; 智能家居控制系统 LAS - 网页服务器 Node.js 、Web Service、页面 和 TCP 请求转 UDP 发送
  5. sql for xml 嵌套
  6. [工作积累] bitfield
  7. Data Flow -&gt;&gt; Fuzzy Lookup &amp; Fuzzy Grouping
  8. Android学习系列(7)--App轮询服务器消息
  9. openstack创建实例测试步骤
  10. 发散问题——Spring容器及加载
  11. Printk与sched_clock_init的一点分析
  12. css实现文本缩略显示
  13. 前端性能优化 —— 添加Expires头与Cache-control区别
  14. 去掉 Chrome(V66) 新标签页的8个缩略图
  15. Redis学习一(基础入门).
  16. Asp.Net构架(Http请求处理流程)、(Http Handler 介绍)、(HttpModule 介绍)
  17. R语言的scale函数
  18. 什么是V模型?使用SDLC和STLC学习案例研究
  19. 面向对象程序设计_Task7_Summary
  20. 记一次Spring的aop代理Mybatis的DAO所遇到的问题

热门文章

  1. InfluxDB学习之InfluxDB的HTTP API查询操作
  2. spring cron表达式
  3. [django]Django站点admin支持中文显示和输入设置
  4. poj1006 / hdu1370 Biorhythms (中国剩余定理)
  5. HDU 5183 Negative and Positive (NP) --Hashmap
  6. HOLOLENS不适合加天空盒
  7. Lrc2Srt字幕转换精灵
  8. 如何动态在文档中加入<script></script>写入大段js?
  9. 使用adb shell 进入手机修改文件的权限
  10. fiddler手机抓包教程