longest common str
#include <vector>
#include <iostream>
#include <string>
using namespace std; int longest_common_string(string& str_a, string& str_b) {
int len_a = str_a.length();
int len_b = str_b.length();
if ( == len_a || == len_b) {
return ;
}
int rows = len_a + ;
int cols = len_b + ;
int ** comm_len_array = new int * [rows];
int i = ;
for (i=; i < rows; i++) {
comm_len_array[i] = new int[cols];
} int j = ;
for (i = ; i < rows; i++) {
for (j = ; j < cols; j++) {
comm_len_array[i][j] = ;
}
} int max = INT_MIN;
int end_sub_str_a = -;
int end_sub_str_b = -;
for (i = ; i < rows; i++) {
for (j = ; j < cols; j++) {
if (str_a[i - ] == str_b[j - ]) {
comm_len_array[i][j] = comm_len_array[i - ][j - ] + ;
} else {
comm_len_array[i][j] = ;
} if (comm_len_array[i][j] > max) {
max = comm_len_array[i][j];
end_sub_str_a = i;
end_sub_str_b = j;
}
}
} for (i=; i < rows; i++) {
delete[] comm_len_array[i];
}
delete[] comm_len_array; return max;
} int main() {
string stra = "abcedefg";
string strb = "abcf";
cout << longest_common_string(stra, strb) << endl;
}
longest common str的更多相关文章
- SPOJ LCS2 - Longest Common Substring II
LCS2 - Longest Common Substring II A string is finite sequence of characters over a non-empty finite ...
- 14. Longest Common Prefix
题目: Write a function to find the longest common prefix string amongst an array of strings. Subscribe ...
- 【LeetCode】14. Longest Common Prefix 最长前缀子串
题目: Write a function to find the longest common prefix string amongst an array of strings. 思路:求最长前缀子 ...
- 【LeetCode】14 - Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings. Solution: cla ...
- hdu 1403 Longest Common Substring(最长公共子字符串)(后缀数组)
http://acm.hdu.edu.cn/showproblem.php?pid=1403 Longest Common Substring Time Limit: 8000/4000 MS (Ja ...
- leetcode【14题】Longest Common Prefix
题目:Longest Common Prefix 内容: Write a function to find the longest common prefix string amongst an ar ...
- Leetcode 14——Longest Common Prefix
题目:Write a function to find the longest common prefix string amongst an array of strings. 很简单的一个描述,最 ...
- [Swift]LeetCode14. 最长公共前缀 | Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings. If there is n ...
- LeetCode专题-Python实现之第14题:Longest Common Prefix
导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...
随机推荐
- Javascript 中childNodes和children的区别
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...
- Javascript表格中搜索
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...
- VisualSVN Server的windows 2003配置和使用方法(图文并茂)
1.为什么要用VisualSVN Server,而不用Subversion? 回答: 因为如果直接使用Subversion,那么在Windows 系统上,要想让它随系统启动,就要封装SVN Serve ...
- HDU 1573 X问题 (中国剩余定理)
题目链接 题意 : 中文题不详述. 思路 : 中国剩余定理.求中国剩余定理中解的个数.看这里看这里 #include <stdio.h> #include <iostream> ...
- hdu1875
http://acm.hdu.edu.cn/showproblem.php?pid=1875 2 2 10 10 20 20 3 1 1 2 2 1000 1000 给定坐标 //最小生成树 #inc ...
- U盘安装Win7 64位
试了好几遍,失败了的就不说了,直接记下成功的方案,方便下次. 方法为:用UltraISO刻镜像文件到U盘,然后U盘启动安装. 具体如下: 刻u盘之前一定要验证iso镜像的完整性啊(可以用文件校验工具与 ...
- 用于主题检测的临时日志(383b4f88-5dc7-4b08-a585-27104eb4ee7f - 3bfe001a-32de-4114-a6b4-4005b770f6d7)
这是一个未删除的临时日志.请手动删除它.(1e2a0af2-731b-4f82-9aa0-4e2d10ed7a1a - 3bfe001a-32de-4114-a6b4-4005b770f6d7)
- QTP10&QTP11&UFT11.5的安装和破解
QTP10的安装和破解方法 下载QTP10.0并安装. 安装成功后,在C:\Program Files\Common Files\Mercury Interactive下创建文件夹:License M ...
- python - PipeMapRed.waitOutputThreads(): subprocess failed with code 1
hadoop上执行mapreduce streaming python程序报错, 报错详细信息为 python - PipeMapRed.waitOutputThreads(): subprocess ...
- Linux kmalloc/kfree 源码解读
kmalloc/kfree用于划分和回收内核空间低区内存的方法.改组方法没有直接通过伙伴系统进行内存的划分,通过slab算法进行分配的.同时也为每个CPU提供一个阵列缓存,用于提高分配效率.下面对改组 ...