基本演算法實現(C)

來源:互聯網
上載者:User

模式串匹配演算法:

#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;//如果j=0就相當於將i右移一位            j=0;        }    }    if(j>=tlen)        return i-tlen;    else        return -1;}void main(){    int i=0;     char s[]="assacashhhghkjklklklljlhghfgfdfsdshjhjjjjjjjjjjjjghfgfgdfdfdsdsadghyyy6577777756fgdssd";     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(最長公用字串):

#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("沒有公用子串");    else    {        printf("公用子串長度:%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[]="assacashhhghkjklklklljlhghfgfdfsdshjhjjjjjjjjjjjjghfgfgdfdfdsdsadghyyy6577777756fgdssd";    char s2[]="hhhgh";    LCS(s1,s2);}
漢諾塔實現:
/* 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);    }}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.