求两个字符串最长子串的LCS算法 C语言实现(简短的实现函数)
/*************************************************************************
> 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语言实现(简短的实现函数)的更多相关文章
- Python基础-求两个字符串最长公共前轴
最长公共前缀,输入两个字符串,如果存在公共前缀,求出最长的前缀,如果没有输出no.如“distance”和“discuss”的最长公共前缀是“dis”. s1 = input('请输入第1个字符串-- ...
- 求两个字符串的最长公共子串——Java实现
要求:求两个字符串的最长公共子串,如“abcdefg”和“adefgwgeweg”的最长公共子串为“defg”(子串必须是连续的) public class Main03{ // 求解两个字符号的最长 ...
- java实现字符串匹配问题之求两个字符串的最大公共子串
转载请注明出处:http://blog.csdn.net/xiaojimanman/article/details/38924981 近期在项目工作中有一个关于文本对照的需求,经过这段时间的学习,总结 ...
- [URAL-1517][求两个字符串的最长公共子串]
Freedom of Choice URAL - 1517 Background Before Albanian people could bear with the freedom of speec ...
- 2019牛客多校第四场 I题 后缀自动机_后缀数组_求两个串de公共子串的种类数
目录 求若干个串的公共子串个数相关变形题 对一个串建后缀自动机,另一个串在上面跑同时计数 广义后缀自动机 后缀数组 其他:POJ 3415 求两个串长度至少为k的公共子串数量 @(牛客多校第四场 I题 ...
- 【LeetCode】无重复字符串最长子串
题目描述 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 示例 1: 输入: "abcabcbb" 输出: 3 解释: 因为无重复字符的最长子串是 "a ...
- EditDistance,求两个字符串最小编辑距离,动态规划
问题描述: 题目描述Edit DistanceGiven two words word1 and word2, find the minimum number of steps required to ...
- SQLServer中求两个字符串的交集(字符串以符号分隔)
两个字符串,以特定符号分隔(例如‘,’号),求交集 第一种情况: declare @m varchar(100),@n varchar(100)select @m=',2,3,5,7,8,9,10,' ...
- C++双指针滑动和利用Vector实现无重复字符的最长子串—力扣算法
题目: 力扣原题链接:https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/ 给定一个字符串, ...
随机推荐
- Understand Rails Authenticity Token
翻译整理自: http://stackoverflow.com/questions/941594/understand-rails-authenticity-token 主要翻译的是第一个回答,另外结 ...
- [RxJS] Returning subscriptions from the subscribe function
So far, when writing these subscribe functions, we haven't returned anything. It is possible return ...
- Fragment 基础
FragmentActivity与Activity区别 fragment是3.0以后的东西,为了在低版本中使用fragment就要用到android-support-v4.jar兼容包,而fragme ...
- 排序算法之奇偶排序 JAVA奇偶排序算法
奇偶排序法的思路是在数组中重复两趟扫描.第一趟扫描选择所有的数据项对,a[j]和a[j+1],j是奇数(j=1, 3, 5……).如果它们的关键字的值次序颠倒,就交换它们.第二趟扫描对所有的偶数数据项 ...
- js 获取元素在页面上的偏移量的最佳方式
使用js制作效果时,我们常常要获取某个元素在页面上的偏移量(例如tip提示框功能).而获取偏移量可以直接获取相对于document的偏移量,也可以获取相对与视口的偏移量(viewpoint)加上页面滚 ...
- DotNet中的计时器线程计时器
转载自:http://hi.baidu.com/wingingbob/item/9f1c9615f3b24d5f2b3e225c 基于多线程设计,计时器工作在ThreadPool线程上,存在事件的重入 ...
- SQL Server 2008将数据导出为脚本
之前我们要将一个表中的数据导出为脚本,那么只有在网上找一个导出数据的Script,然后运行就可以导出数据脚本了.现在在SQL Server 2008的Management Studio中增加了一个新特 ...
- Swift - 35 - 使用闭包简化语法
//: Playground - noun: a place where people can play import UIKit // 初始化一个整数数组 var arr = [1, 3, 5, 7 ...
- 在MyEclipse中设置Source folders和output folder
在一个项目中可能会有多个资源文件,它们共同编译输出到输出文件.那么除了默认的src以外,如何把其他文件设置成资源文件(Source folders)呢?
- gui界面2048小游戏
坑的地方 JLabel色块要调透明 方向键要用press方法 主界面 package game; import java.awt.BorderLayout; import java.awt.Event ...