/*************************************************************************
> 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. Install Oracle 10g on Red Hat Linux 5.3 Step by Step

    一.虚拟机配置 1. 虚拟机(VBox 4.3.12) 2. 配置虚拟机网卡网络.选择host-only.VirtualBox Host-Only Network网卡IP为设置为192.168.1.1 ...

  2. JStorm 是一个分布式实时计算引擎

    alibaba/jstorm JStorm 是一个分布式实时计算引擎. JStorm 是一个类似Hadoop MapReduce的系统, 用户按照指定的接口实现一个任务,然后将这个任务递交给JStor ...

  3. angularjs手动解析表达式($parse)

    <!DOCTYPE html> <html lang="zh-CN" ng-app="app"> <head> <me ...

  4. NYOJ-129 并查集

    这个题基本上是并查集稍微一变, 只是加了一些判断条件而已,就是将点合并成树, 最后遍历一下, 统计一下有多少棵树, 如果不是1的话, 肯定不是树,所以,可以根据这个来判断 #include <s ...

  5. Apache和Nginx平滑重启

    之前修改了服务器配置都是简单粗暴的用restart重启apache/nginx,据说这样不好.需要平滑重启服务器,避免重启时打断用户行为.然后就根据官方文档了解了一下平滑重启的命令.本文根据Apach ...

  6. Java Criteria表关联查询(两个表未定义关联关系)

    Criteria criteria = this.getSession().createCriteria(Competition.class, "b"); DetachedCrit ...

  7. php调试mysql信息。

    print_r(mysql_error());会返回执行myql的成功或者失败的信息.数据库的编码方式是UTF-8.获取手机号,返回的页面的编码是gb2312.需要转换

  8. Asp.Net Mvc MapRoute .html不起作用(转)

    RegisterRoutes 注册路由示例配置: public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRo ...

  9. CactiEZ 中文版V10.1安装使用以及139邮箱短信报警设置

    说明:CactiEZ中文版V10.1是基于CentOS 6.0系统,整合Cacti等相关软件,重新编译而成的一个操作系统!   说明:CactiEZ中文版V10.1是基于CentOS 6.0系统,整合 ...

  10. JavaScript 客户端JavaScript之样式表操作(DOM API 提供模块之一)

    层叠样式 表和动态HTML   层叠样式表(CSS)是指定HTML文档或XML文档的表现的标准.     使用CSS和Javascript,可以创建出各种视觉效果,这些效果可以统称为动态HTML(DH ...