求两个字符串最长子串的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/ 给定一个字符串, ...
随机推荐
- Jquery UI的datepicker插件使用
原文链接;http://www.ido321.com/375.html Jquery UI是一个非常丰富的Jquery插件,而且UI的各部分插件能够独自分离出来使用.这是其它非常多Jquery插件没有 ...
- Android 自定义View (二) 进阶
转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/24300125 继续自定义View之旅,前面已经介绍过一个自定义View的基础的例 ...
- shell 中条件判断
if 中的 -z 到 -d 的意思 2011-09-05 10:30 [ -a FILE ] 如果 FILE 存在则为真. [ -b FILE ] 如果 FILE 存在且是一个块特殊文件则为真. [ ...
- 用switch判断月份的练习
import java.util.Scanner; public class SwitchTest01 { public static void main(String[] args) { Syste ...
- 无限循环的ViewPager
目前情况 在不修改源码的情况下,当ViewPager滑动到最后一个item的时候,他就无法再往右滑动:当ViewPager滑动到第一个item的时候,他也无法再往前滑动.(以上全是废话) 设想 我们可 ...
- Oracle Linux 挂载存储
#启动多路径multipathd服务 service multipathd restart #设置开机自动启动multipathd服务 chkconfig multipathd on #查看信息mul ...
- linux配置时间同步
目标环境,5台linux centos 6.3, 一台作为NTPD服务与外部公共NTP服务同步时间,同时作为内网的NTPD服务器,其他机器与这台服务做时间同步. 服务器IP 角色 说明 同步方式 ...
- UVA 11770 Lighting Away
RunID User Problem Result Memory Time Language Length Submit Time 2482977 zhyfzy J Accepted 0 KB 138 ...
- OD: Ring0 & Kernel
开发技术讲究封装与模块化,安全技术强调底层安全性.安全技术需要打开封装.追根溯源! <0day 安全:软件漏洞分析技术(第2版)> 第21章 探索 Ring0 笔记 Intel x86 系 ...
- UITableView出现卡顿如何处理
tableView的beginUpdate和endUpdate要比reloadData和reloadRowsAtIndexPaths好,因为beginUpdate和endUpdate会执行一个动画bl ...