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 文中代码为了不动官网提供的初始 ...
随机推荐
- POJ1442Black Box
http://poj.org/problem?id=1442 题意 : 题目中对给出的数字有两种操作ADD(I)操作,将ADD括号里的数字 I 加到数列里边去,然后是自动排好序的,每一个数列前边都会有 ...
- ubuntu -LDAP的配置
本文内容来自 http://blog.csdn.net/jl19861101/article/details/5582841 1. LDAP Server1.1. 安装主要安装一下套件: 代码: # ...
- *[topcoder]LCMSetEasy
http://community.topcoder.com/stat?c=problem_statement&pm=13040 DFS集合全排列+LCM和GCD.但事实上,有更简单的算法,列在 ...
- LR_问题_运行场景时提示scripts you are running in invalid
问题描述 脚本在virtual user generator中运行正常. 在Controller中运行场景时报错: the target you defined cannot be reached. ...
- python自省指南
深入python中对自省的定义: python的众多强大功能之一,自省,正如你所知道的,python中万物皆对象,自省是指代码可以查看内存中以对象形式存在的其他模块和函数,获取它们的信息,并对它们进行 ...
- ssm框架整合小结
1.整合思路 一.Dao层:整合mybatis和spring 需要的jar包: 1.mybatis的jar包 2.Mysql数据库驱动 3.数据库连接池 4.Mybatis和spring的整合包. 5 ...
- 257. Binary Tree Paths
题目: Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree ...
- C++:虚基类
4.4.3 虚基类1.没什么要引入虚基类 如果一个类有多个直接基类,而这些直接基类又有一个共同的基类,则在最底层的派生类中会保留这个间接的共同基类数据成员的多分同名成员.在访问这些同名的成员时,必须在 ...
- C++类型转化
dynamic_cast在多继承中可以判断无关类之间是否可以转换,例如基类B1和基类B2之间,static_cast可以在隐式转化及其逆转换中进行,但不允许无关类型之间的转换.,dynamic_cas ...
- linux kernel启动流程
linux kernel启动是从./init/main.c中开始的,其大概流程是: 1. 调用start_kernel()函数: 2. start_kernel()调用rest_init()函数: 3 ...