Basic Algorithm Implementation (c)

Source: Internet
Author: User

Pattern string matching algorithm:

# Include "stdio. H "int index (char * s, char * t, int POS) {int I, j, slen, tlen; slen = strlen (s); tlen = strlen (t ); I = Pos; j = 0; while (I <slen & J <tlen) {If (s [I] = T [J]) {I ++; j ++;} else {I = I-j + 1; // If J = 0, it is equivalent to moving I one J = 0 ;}} if (j> = tlen) return I-tlen; else return-1;} void main () {int I = 0; char s [] = "Expect "; char P [] = "hhhgh"; I = index (S, P, 1); printf ("I = % d \ n", I );}
#include "stdio.h"void Get_next(char *p,int next[]){    int i,j,slen;    slen=strlen(p);    i=0;    next[0]=-1;    //next[1]=0;    j=-1;    while(i<slen)    {        if(j==-1||p[i]==p[j])        {            i++;            j++;            next[i]=j;        }        else j=next[j];    }}int Index_KMP(char *s,char *p,int pos,int next[]){    int i,j,slen,plen;    i=pos-1;    j=-1;    slen=strlen(s);    plen=strlen(p);    while(i<slen&&j<plen)    {        if(j==-1||s[i]==p[j])        {            i++;            j++;        }        else j=next[j];    }    if(j>=plen) return i-plen;    else return -1;}void main(){    char s[]="assacashhhghkjklklklljlhghfgfdfsdshjhjjjjjjjjjjjjklkloiiuui9   igghfgfgdfdfdsdsadghyyy6577777756fgdssd";    char p[]="jjjjjjjklkloiiuui9   igghfgfgdf";    int next[100];    Get_next(p,next);    printf("%d\n",Index_KMP(s,p,0,next));}

LCS (longest Public String ):

# Include "stdio. H "Void LCS (char S1 [], char S2 []) {int I, j, begin = 0, L1, L2, maxlen = 0, * matrix; l1 = strlen (S1); L2 = strlen (S2); matrix = (int *) malloc (L1 * sizeof (INT); // for (I = 0; I <L1; I ++) matrix [I] = 0; for (I = 0; I <L2; I ++) for (j = l1-1; j> = 0; -- j) {If (s2 [I] = S1 [J]) {If (j = 0) | (I = 0 )) matrix [J] = 1; else matrix [J] = matrix [J-1] + 1;} else matrix [J] = 0; If (Matrix [J]> maxlen) {maxlen = matrix [J]; begin = J ;}} if (maxlen = 0) printf ("no public substrings "); else {printf ("Public substring length: % d \ n", maxlen); printf ("begin = % d \ n", begin ); for (I = begin-maxlen + 1; I <= begin; I ++) printf ("% C", S1 [I]);} Free (matrix );} void main () {char S1 [] = "bytes"; char S2 [] = "hhhgh"; LCS (S1, S2 );}
Tower of Hanoi implementation:
/* Note:Your choice is C IDE */#include "stdio.h"void main(){    void hanoi(int n,char a,char b,char c);    hanoi(34,'A','B','C');  }void hanoi(int n,char a,char b,char c){    if(n==1)printf("%c=>%c\n",a,c);    else    {        hanoi(n-1,a,c,b);        printf("%c=>%c\n",a,c);        hanoi(n-1,b,a,c);    }}

 

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.