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 文中代码为了不动官网提供的初始 ...
随机推荐
- 深入理解JVM—字节码执行引擎
原文地址:http://yhjhappy234.blog.163.com/blog/static/3163283220122204355694/ 前面我们不止一次的提到,Java是一种跨平台的语言,为 ...
- ScannerTest-------double string
import java.util.*; public class ScannerTest { public static void main(String[] args){ Scanner scann ...
- Intellij IDEA调试功能
public class Demo { public static void f1() { System.out.println("one"); System.out.printl ...
- PCL—低层次视觉—点云滤波(基于点云频率)
1.点云的频率 今天在阅读分割有关的文献时,惊喜的发现,点云和图像一样,有可能也存在频率的概念.但这个概念并未在文献中出现也未被使用,谨在本博文中滥用一下“高频”一词.点云表达的是三维空间中的一种信息 ...
- 使用exe4j把JAVA GUI程序打包成exe文件时遇到的问题
1.把项目打包成jar文件时,只要勾选src目录就行了,其他的比如资源文件和jar包是不能添加进去的. 2.在D盘建一个文件夹,最好与项目同名,然后把打包好的jar包放进去,其他资源文件(图片之类的) ...
- Java实现-------网络蜘蛛
闲来无事,学习了一下网络蜘蛛的简易原理.是最简单的一种,一般新手都可以看得懂哦~~读者可以将其进行扩展,可以实现用来抓取网页js或者css等等哦... package com.insist.entit ...
- ubuntu下android源码下载
步骤一: 首先保证你的ubuntu系统电脑可以顺利游览google,我们是将etc下 hosts替换掉,推荐hosts: http://laod.cn/hosts/2015-google...host ...
- IOS代码
//// MJViewController.m// UITableView-编辑模式//// Created by mj on 13-4-11.// Copyright (c) 2013年 itcas ...
- Android中使用Parcelable
今天 在两个Activity之间传集合类型数据,看了一下,要用Parcelable 所以就看一下东西: 下面一段话是复制网友的. Android序列化对象主要有两种方法,实现Serializable接 ...
- sdut 1728 编辑距离问题( dp )
题目 思路:edit(i, j),它表示第一个字符串的长度为i的子串到第二个字符串的长度为j的子串的编辑距离. 有如下动态规划公式: if i == 0 且 j == 0,edit(i, j) = 0 ...