/*************************************************************************
> File Name: lcs.c
> Author: dingzhengsheng
> Mail: dingzs3@asiainfo.com
> Created Time: 2015年05月20日 星期三 16时07分50秒
> Version: v0.01
> Description:
> History:
************************************************************************/ #include<stdio.h>
#include<time.h>
#include<stdlib.h>
#include <unistd.h>
#include<string.h>
#include<sys/time.h>
#include<ctype.h>
#include"test_time.h" #define DEBUG 1 void slow_bb(char *a,int lena,char *b,int lenb, char *c)
{
int i;
int j;
int index;
int max=0;
int num=0;
int start; for(i=0; i<lena; i++)
{
for(j=0; j<lenb; j++)
{
int start1 = i;
int start2 = j; while((start1 < lena-1) && (start2<lenb-1) && (a[start1++] == b[start2++]))
num++; if(num > max)
{
max = num;
start = i;
}
num = 0;
}
} strncpy(c, a+start, max); } void lcs(char *a, int lena, char *b, int lenb, char *c)
{
int i;
int j;
int s[lena+1];
memset(s, 0, sizeof(int)*(lena+1));
int maxlen = 0;
int pos; #ifdef DEBUG
int *dbg;
dbg= (int *)malloc(sizeof(int)*(lenb+1)*(lena+1));
memset(dbg, 0, sizeof(int)*(lenb+1)*(lena+1));
#endif for(j=lenb-1; j>=0; j--)
{
for(i=0; i<lena; i++)
{
if(b[j] == a[i])
{
#ifdef DEBUG
*(dbg+j*(lena+1)+i) = s[i] = s[i+1]+1;
#else
s[i] = s[i+1]+1;
#endif
if(s[i] > maxlen)
{
maxlen = s[i] ;
pos = i;
}
}
else
{
/*
if(s[i+1] > maxlen)
{
maxlen = s[i+1];
pos = i+1;
}
*/
s[i] = 0;
}
}
} #ifdef DEBUG
for(i=0; i<lenb+1; i++)
{
if(i == 0)
{
printf(" ");
for(j=0; j<lena; j++)
printf("%c ", a[j]); printf("\n");
}
if(i == lenb)
printf(" ");
for(j=0; j<lena+1; j++)
{
if(j == 0)
printf("%c ", b[i]);
printf("%d ", *(dbg+i*(lena+1)+j));
}
printf("\n");
}
#endif strncpy(c,&a[pos], maxlen); } void main(int argc, char *argv[])
{
char *a;
char *b;
int a_len;
int b_len;
char *c;
int n = 100;
int i=0; if(argc >= 1)
n = atoi(argv[1]);
if(argc >= 3)
{
a_len = atoi(argv[2]);
b_len = atoi(argv[3]);
} a = malloc(a_len);
b = malloc(b_len);
c = malloc(a_len); for(i=0; i<a_len; i++)
a[i] =random()%4 + 0x30; for(i=0; i<b_len; i++)
b[i] =random()%4 + 0x30; printf("a=%s\n", a);
printf("b=%s\n", b);
memset(c, 0, sizeof(c));
starts();
for(i=0;i<n;i++)
slow_bb(a, a_len, b, b_len, c);
ends();
printf("slow_bb c=%s ts=%lld\n", c, tt()); memset(c, 0, sizeof(c));
starts();
for(i=0;i<n;i++)
lcs(a, a_len, b, b_len, c);
ends();
printf("slow_bb c=%s ts=%lld\n", c, tt()); free(a);
free(b);
free(c); }

 test_time.c:

#include<stdio.h>
#include<time.h>
#include <unistd.h>
#include<string.h>
#include<sys/time.h>
#include<sys/resource.h> #include"test_time.h" #define SECTOUSEC 1000000 static struct timeval tv1,tv2; void starts()
{
gettimeofday(&tv1,NULL);
} void ends()
{
gettimeofday(&tv2,NULL);
} long int get_diff_time(struct timeval *tv1, struct timeval *tv2)
{ long int n; n = (tv2->tv_sec - tv1->tv_sec)*SECTOUSEC + tv2->tv_usec - tv1->tv_usec;
return n;
} long int tt()
{
return get_diff_time(&tv1, &tv2);
}

  test_time.h:

/*************************************************************************
> File Name: test_time.h
> Author: dingzhengsheng
> Mail: dingzs3@asiainfo.com
> Created Time: 2015年05月20日 星期三 20时02分00秒
> Version: v0.01
> Description:
> History:
************************************************************************/ #ifndef __TEST_TIME_H
#define __TEST_TIME_H void starts();
void ends();
long int tt(); #endif

  

运行结果:(矩阵的打印受宏DEBUG控制),后者耗时更长就是因为打印矩阵

注:代码中没有对最长子串有多个的情况做处理,两个函数的结果可能会不一样,可以用数组记录多个相同长度最长子串

求两个字符串最长子串的LCS算法 C语言实现(简短的实现函数)的更多相关文章

  1. Python基础-求两个字符串最长公共前轴

    最长公共前缀,输入两个字符串,如果存在公共前缀,求出最长的前缀,如果没有输出no.如“distance”和“discuss”的最长公共前缀是“dis”. s1 = input('请输入第1个字符串-- ...

  2. 求两个字符串的最长公共子串——Java实现

    要求:求两个字符串的最长公共子串,如“abcdefg”和“adefgwgeweg”的最长公共子串为“defg”(子串必须是连续的) public class Main03{ // 求解两个字符号的最长 ...

  3. java实现字符串匹配问题之求两个字符串的最大公共子串

    转载请注明出处:http://blog.csdn.net/xiaojimanman/article/details/38924981 近期在项目工作中有一个关于文本对照的需求,经过这段时间的学习,总结 ...

  4. [URAL-1517][求两个字符串的最长公共子串]

    Freedom of Choice URAL - 1517 Background Before Albanian people could bear with the freedom of speec ...

  5. 2019牛客多校第四场 I题 后缀自动机_后缀数组_求两个串de公共子串的种类数

    目录 求若干个串的公共子串个数相关变形题 对一个串建后缀自动机,另一个串在上面跑同时计数 广义后缀自动机 后缀数组 其他:POJ 3415 求两个串长度至少为k的公共子串数量 @(牛客多校第四场 I题 ...

  6. 【LeetCode】无重复字符串最长子串

    题目描述 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 示例 1: 输入: "abcabcbb" 输出: 3 解释: 因为无重复字符的最长子串是 "a ...

  7. EditDistance,求两个字符串最小编辑距离,动态规划

    问题描述: 题目描述Edit DistanceGiven two words word1 and word2, find the minimum number of steps required to ...

  8. SQLServer中求两个字符串的交集(字符串以符号分隔)

    两个字符串,以特定符号分隔(例如‘,’号),求交集 第一种情况: declare @m varchar(100),@n varchar(100)select @m=',2,3,5,7,8,9,10,' ...

  9. C++双指针滑动和利用Vector实现无重复字符的最长子串—力扣算法

    题目: 力扣原题链接:https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/ 给定一个字符串, ...

随机推荐

  1. tomcat安全配置之禁用Directory Listing

    什么是Directory Listing?通俗点讲,就是在webapp的目录下如果没有放置index.html或者类似的文件,如果从IE或者其它浏览器文章这个路径时,会惊喜的发现这个目录下的文件列表被 ...

  2. Linux下python升级

    Centos即使用Yum更新也是Python2.6.6所以需要升级到Python2.7.8 1.先下载源码包 1 wget https://www.python.org/ftp/python/2.7. ...

  3. 精通Django或Rails框架

    Django是一个开放源代码的Web应用框架,由Python写成. Rubyon Rails 是一个用于开发数据库驱动的网络应用程序的完整框架.

  4. Example_07_05录音提示open failed: EACCES (Permission denied)

    在AndroidManifest.xml文件中加入下面这句话:<uses-permission android:name="android.permission.WRITE_EXTER ...

  5. SAE下的Memcache使用方法

    SAE里面有Memcache,可以较大幅度改善数据库的鸭梨~ 之前一直想学习Memcache,却愁于不知如何下手,对这个名词完全没有概念,同时在SAE的文档里面,也很少对于Memcache的使用教程~ ...

  6. C# 连接SQL数据库

    感觉很有必要总结一下 一:C# 连接SQL数据库 Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;P ...

  7. 重装eclipse要做的事

    当我们要在新环境上安装eclipse时,往往会做很多的个性修改和安装一些插件,下面就这些做一下总结: 一.插件 1.svn插件(subclipse) 插件官网下载地址:http://subclipse ...

  8. mysql innodb myisam 主要区别与更改方法

    一.主要区别 1.事务处理 innodb 支持事务功能,myisam 不支持. Myisam 的执行速度更快,性能更好.   2.select ,update ,insert ,delete 操作   ...

  9. Nt内核函数原型and中文

    NtLoadDriver 服务控制管理器加载设备驱动. NtUnloadDriver 服务控制管理器支持卸载指定的驱动程序. NtRegisterNewDevice 加载新驱动文件. NtQueryI ...

  10. MyEclipse 点击 部署 按钮 无效的解决办法

    问题描述 1 通常情况下,当我们点击MyEclipse任务栏上的部署按钮时,会弹出项目部署框,如下图:   2 但我们有时也会遇到点击部署按钮怎么也弹不出项目部署框的问题. END 解决方法一: 1 ...