Evaluate the implementation of the LCS algorithm (short implementation function) and the lcs Algorithm for the longest string of two strings in C language.

Source: Internet
Author: User

Evaluate the implementation of the LCS algorithm (short implementation function) and the lcs Algorithm for the longest string of two strings in C language.


/*************************************** * *********************************> File Name: lcs. c> Author: dingzhengsheng> Mail: dingzs3@asiainfo.com> Created Time: Wednesday, May 20, 2015> 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 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: dingzs3@asiainfo.com> Created Time: Wednesday May 20, 2015 seconds> Version: v0.01> Description:> History: **************************************** * ******************************/# ifndef _ TEST_TIME_H # define _ TEST_TIME_Hvoid starts (); void ends (); long int tt (); # endif

  

Running result: (the print of the matrix is controlled by macro DEBUG.) the latter takes longer because the print matrix

 

Note: The Code does not process multiple headers. The results of the two functions may be different. You can use arrays to record multiple headers of the same length.

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.