/*************************************************************************
> 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. ssh链接云主机的一些笔记

    在window下 试了好几个ssh软件后用了putty,因为它免费而且中文不乱码 1.实现自动登陆功能 用快捷方式实现自动登陆 首先创建putty.exe的快捷方式到桌面:然后运行putty,输入ho ...

  2. js判断访问者是否来自移动端代码

    <script type="text/javascript"> function is_mobile() { var regex_match = /(nokia|iph ...

  3. Install the OpenStack command-line

    Install the OpenStack command-line Install the prerequisite software python 2.7 or later note: Curre ...

  4. 2008年NOI全国竞赛 假面舞会

    /* 分三种情况 1 有环:找环长的gcd作为max gcd的超过2的最小因子作为min 2 树:所有最长链的和作为max 3为min (最长链≥3) 3 两条相交链:找出所有的这样的两条链的差 同1 ...

  5. java SSH整合配置

    web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app version="3 ...

  6. linux执行文件命令

    1.如果path中有你的程序所在的目录,那么直接执行filename即可 2.如果path中没有程序所在目录,那么进入目录./filename或者path/filename 比如 wj@ubuntu: ...

  7. JAVA List与数组间相互转换

    1.list转数组 例如: List<String> list =  new ArrayList<String>(); list.add("aa"); li ...

  8. 用 CALayer 定制下载进度条控件

    // // RPProgressView.h // CALayer定制下载进度条控件 // // Created by RinpeChen on 16/1/2. // Copyright © 2016 ...

  9. PHP 异常处理

    PHP 异常处理 异常用于在指定的错误发生时改变脚本的正常流程. 异常是什么 PHP 5 提供了一种新的面向对象的错误处理方法. 异常处理用于在指定的错误(异常)情况发生时改变脚本的正常流程.这种情况 ...

  10. 读取XML文件的几种方式的效率分析

    第一种:使用XmlReader来读取. Stopwatch sw = Stopwatch.StartNew(); List<Dictionary<string, string>> ...