#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的更多相关文章

  1. SPOJ LCS2 - Longest Common Substring II

    LCS2 - Longest Common Substring II A string is finite sequence of characters over a non-empty finite ...

  2. 14. Longest Common Prefix

    题目: Write a function to find the longest common prefix string amongst an array of strings. Subscribe ...

  3. 【LeetCode】14. Longest Common Prefix 最长前缀子串

    题目: Write a function to find the longest common prefix string amongst an array of strings. 思路:求最长前缀子 ...

  4. 【LeetCode】14 - Longest Common Prefix

    Write a function to find the longest common prefix string amongst an array of strings. Solution: cla ...

  5. hdu 1403 Longest Common Substring(最长公共子字符串)(后缀数组)

    http://acm.hdu.edu.cn/showproblem.php?pid=1403 Longest Common Substring Time Limit: 8000/4000 MS (Ja ...

  6. leetcode【14题】Longest Common Prefix

    题目:Longest Common Prefix 内容: Write a function to find the longest common prefix string amongst an ar ...

  7. Leetcode 14——Longest Common Prefix

    题目:Write a function to find the longest common prefix string amongst an array of strings. 很简单的一个描述,最 ...

  8. [Swift]LeetCode14. 最长公共前缀 | Longest Common Prefix

    Write a function to find the longest common prefix string amongst an array of strings. If there is n ...

  9. LeetCode专题-Python实现之第14题:Longest Common Prefix

    导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...

随机推荐

  1. 【Linux高频命令专题(15)】more

    more命令,功能类似 cat ,cat命令是整个文件的内容从上到下显示在屏幕上. more会以一页一页的显示方便使用者逐页阅读,而最基本的指令就是按空白键(space)就往下一页显示,按 b 键就会 ...

  2. compiler 学习

    一款强大的编译器LLVM:http://llvm.org/docs/GettingStarted.html#layout http://llvm.org/docs/LangRef.html http: ...

  3. Go语言博客

    http://www.cnblogs.com/concurrency/p/4293613.html#3130523

  4. 1、Singleton 单件(创建模式)

    一.Singleton模式主要应用在一些特殊的类,在整个系统运行中,有且仅有一个实例的场景 二.Singleton模式分为单线程与多线程情况,当然多线程一样适应单线程 单线程:在这种情况下比较容易,因 ...

  5. 231. Power of Two

    题目: Given an integer, write a function to determine if it is a power of two. 链接: http://leetcode.com ...

  6. Linux下安装、配置、启动Apache

    http://www.cnblogs.com/zhuque/archive/2012/11/03/2763352.html#

  7. java学习笔记[2015-1-19]

    想到哪儿说到哪儿: 1.关于import,全限定名导入和按需导入(即只使用*),影响的只是编译速度,编译过后生成的是全限定名,全限定名还一个好处是不容易产生冲突 2.每个java文件只能有一个publ ...

  8. JVM学习笔记(四)------内存调优

    首先需要注意的是在对JVM内存调优的时候不能只看操作系统级别Java进程所占用的内存,这个数值不能准确的反应堆内存的真实占用情况,因为GC过后这个值是不会变化的,因此内存调优的时候要更多地使用JDK提 ...

  9. Android XML使用的学习记录

    1. 注释其中一段代码或是一行,可以采用<!-- -->,示例如下 <!--       <EditText         android:layout_width=&quo ...

  10. BZOJ2252: [2010Beijing wc]矩阵距离

    题解: 我脑子里都是翔??? bfs一下就行了 我居然还想什么kd tree!真是too naive,,, #include<cstdio> #include<cstdlib> ...