An LCS algorithm for finding two string maximum substrings

Source: Internet
Author: User


/************************************************************************* > File name:lcs.c > Author:dingzhengsheng > Mail: [email protected] > Created Tim E: May 20, 2015 Wednesday 16:07 50 seconds > 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 1void 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 star T1 = 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) {MAXL En = 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 1000000static 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:                             [email protected]     > Created time:             Wednesday, May 20, 2015 20:02 00 sec    > Version:                  v0.01    > Description:    > History: ********************************** /#ifndef __test_time_h#define __test_time_hvoid starts (); void ends (); long int TT (); #endif

  

Run Result: (the print of the matrix is controlled by macro debug), the latter time is much longer because the printing matrix

Note: The code does not have multiple cases of the oldest string, the results of two functions may be different, you can use the array to record many of the same length of the oldest string

An LCS algorithm for finding two string maximum substrings

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.