Data Structures - Implementation of Algorithms Related to Strings

Data Structures - Implementation of Algorithms Related to Strings

Insertion function implementation of sequence string

When inserting a sequence string, inserting pos divides the string into two parts (assuming A, B, length LA, LB) and the part to be inserted (assuming C, length LC), then the string is composed of the pre-insertion AB becomes ACB. Since it is a sequential string, insertion will cause the movement of elements. The following three situations may occur:

  • ①The length of the string after insertion (LA+LC+LB) <=MAXLEN, then move B backward by LC elements, and then insert C;
  • ②If the length of the inserted string is >=MAXLEN and pos+LC <=MAXLEN, some characters will be discarded when B is moved backward;
  • ③ If the length of the inserted string is >MAXLEN and pos+LC > MAXLEN, then all the characters of B are discarded (no need to move backwards), and some characters of C are also discarded when inserting.

Code

#include<stdio.h>
 
#define MAX_SIZE 128
 
typedef struct
{
    char ch[MAX_SIZE];
    int last;
}string;
 
void init_str(string * s, int max)
{
    int i;
 
    for(i = 0; i < max; i++)
        s->ch[i] = 0;//而谁的 ASCII 码对应的是 整型的0呢?是字符'\0'
    s->last = 0;
}
 
void create_str(string * s, char * a, int n)
{
    int i=0; 
    for(;i < n; i++)
        s->ch[i] = a[i];
 
    s->ch[i+1] = '\0';
    s->last = n;
    printf("你输入的字符串为:\n");
    printf("%s\n",s->ch);
}
 
void insert_str(string * s,char * substr, int len, int pos)
{
    int i;
 
    if(pos < 0 || pos >= MAX_SIZE)
    {
        printf("输入的位置不合理!\n");
        return;
    }
 
    if (s->last + len >= MAX_SIZE) {
        printf("Can't insert, new string too long!\n");
        return;
    }
 
    if (pos >= s->last) {    /* 若插入的位置在last之后*/
        for (i = 0; i < len; i++)
            s->ch[s->last+i] = substr[i];//把新插入的串从s串的组后开始插入 
        s->ch[s->last+i] = 0;//插入新串之后,字符串的最末尾'\0' ,0 是'\0' 的 ASCII 
        puts(s->ch);
        s->last += len;
    } else {    /* 若插入的位置在ch内*/
        for (i = 0; i < len; i++)
            s->ch[pos+len+i] = s->ch[pos+i];//在没有插入新的串之前把s串分2部分,然后把后面部分整体向后移动len个长度
                                            //即,给即将要插入的串“腾空 ” 
 
        for (i = 0; i < len; i++)
            s->ch[pos+i] = substr[i]; //把新的串插入到刚刚腾出来的地方 
        s->ch[s->last+len] = 0; //插入后的“串”已经确定,改变了最后'/0'位置 
        s->last += len; //串的最大长度也重新确定 即,s->last = s->last+len 
    }
}
int main()
{
    string s;
    int pos, i;
    char a[80],b[80];
 
    init_str(&s, MAX_SIZE);
    printf("请输入字符串:\n");
    scanf("%s",a);
 
    /* 遍历整个字符串,并把a[]中的字符存在s中,长度为i ,然后输出*/
    for(i = 0; a[i]; i++)//for(i=0;a[i]!='\0';i++) 后者这个准确些 
        ;
    create_str(&s, a, i);// 
    printf("请输入你要插入的字符串:\n");
    scanf("%s", b);
 
    for(i = 0; b[i]; i++)
        ;
    printf("请输入你要插入的位置:\n");
    scanf("%d",&pos);
    insert_str(&s, b, i, pos);//找到原来的串“s”,把b[]中的字符存进去,新插入的字符串的长度为i,插入的
                               // 位置为pos
                                 
    printf("新的字符串为:\n");
    printf("%s\n",s.ch);//输出插入后 所组成的 新串 
    return 0;
}

String delete function implementation

Specify the position to be deleted and the length of the characters to be deleted from this position to implement the delete operation of the deleted string.

Code

#include<stdio.h>
 
#define MAX_SIZE 128
 
typedef struct
{
    char ch[MAX_SIZE];
    int last;
}string;
 
void init_str(string * s, int max)
{
    int i;
 
    for(i = 0; i < max; i++)
        s->ch[i] = 0;//而谁的 ASCII 码对应的是 整型的0呢?是字符'\0'
    s->last = 0;
}
 
void create_str(string * s, char * a, int n)
{
    int i=0; 
    for(;i < n; i++)
        s->ch[i] = a[i];
 
    s->ch[i+1] = '\0';
    s->last = n;
    printf("你输入的字符串为:\n");
    printf("%s\n",s->ch);
}
 

//顺序串的删除函数

void StrDelete(string * s,int pos,int len)
{
    int i;
    if(pos<0||pos>(s->last-len))
        {
            printf("删除参数不合法!");
            return; 
        }
    for(i=pos+len;i<s->last;i++)
    {
        s->ch[i-len] = s->ch[i];
    }
    s->ch[s->last-len] = 0;
    s->last = s->last - len;
}


 
int main()
{
    string s;
    int pos, i,n;
    char a[80];
 
    init_str(&s, MAX_SIZE);
    printf("请输入字符串:\n");
    scanf("%s",a);
 
   for(i = 0; a[i]; i++)//for(i=0;a[i]!='\0';i++) 后者这个准确些 
        ;
    create_str(&s, a, i);// 
    
                               
    printf("请输入要删除的位置 及字符串长度:");
    scanf("%d %d",&pos,&n);
    StrDelete(&s,pos,n);
                                 
    printf("新的字符串为:\n");
    printf("%s\n",s.ch);//输出插入后 所组成的 新串 
    return 0;
}

String comparison function implementation

String comparison, that is, comparing two strings (ascii) In this example, if the sum of the strings s is equal, it returns 0, if s>t, it returns a positive number, otherwise, it returns a negative number.

Code

#include<stdio.h>
 
#define MAX_SIZE 128
 
typedef struct
{
    char ch[MAX_SIZE];
    int last;
}string;
 
void init_str(string * s, int max)
{
    int i;
 
    for(i = 0; i < max; i++)
        s->ch[i] = 0;//而谁的 ASCII 码对应的是 整型的0呢?是字符'\0'
    s->last = 0;
}
 
void create_str(string * s, char * a, int n)
{
    int i=0; 
    for(;i < n; i++)
        s->ch[i] = a[i];
 
    s->ch[i+1] = '\0';
    s->last = n;
    printf("你输入的字符串为:\n");
    printf("%s\n",s->ch);
}
 


//串比较函数

int StrCompare(string s,string t)
{
    /*若串s和t相等则返回0,若s>t则返回正数;若s<t,则返回负数*/
    int i;
    int equle = 0;
    for(i=0;i<s.last&&i<t.last;i++)
    {
        if(s.ch[i]!=t.ch[i])
            return(s.ch[i]-t.ch[i]);//都是ascii码 
        
    }
    return equle;
    
} 
 
int main()
{
    string s,t;
    int pos,i;
    int m;
    char a[80];
    char b[80];
 
    init_str(&s, MAX_SIZE);
    printf("请输入字符串1:\n");
    scanf("%s",a);
    printf("请输入字符串2:\n");
    scanf("%s",b);
 
   for(i = 0; a[i]; i++)//for(i=0;a[i]!='\0';i++) 后者这个准确些 
        ;
    create_str(&s, a, i);// 
    for(i = 0; b[i]; i++)//for(i=0;a[i]!='\0';i++) 后者这个准确些 
        ;
    
    create_str(&t, b, i);
    m = StrCompare(s,t); 
    printf("%d",m);                        
    
                                 
    
    return 0;
}

Simple Pattern Matching BF Algorithm for Strings

The simple pattern matching algorithm is a matching algorithm with backtracking. The basic idea of ​​the algorithm will not be described here. You can find good answers on the Internet or in books. The time complexity of this algorithm is high.

Code

/*串的简单模式匹配函数*/

#include<stdio.h>
 
#define MAX_SIZE 128
 
typedef struct
{
    char ch[MAX_SIZE];
    int last;//串的长度 
}string;
 
void init_str(string * s, int max)
{
    int i;
 
    for(i = 0; i < max; i++)
        s->ch[i] = 0;//而谁的 ASCII 码对应的是 整型的0呢?是字符'\0'
    s->last = 0;
}
 
void create_str(string * s, char * a, int n)
{
    int i=0; 
    for(;i < n; i++)
        s->ch[i] = a[i];
 
    s->ch[i+1] = '\0';
    s->last = n;
    printf("你输入的字符串为:\n");
    printf("%s\n",s->ch);
}
 


//顺序串的基本匹配算法

void StrIndex (string s,int pos,string t)
{
    /*求从主串的下标pos起....*/
    int i,j,start;
    if(t.last == 0)
        printf("匹配成功!");
     start = pos;
     i = start;//主串不一定从索引为0的字符开始,具体位置又客户指定 
     j = 0;//模式串从索引0开始 
     while(i<s.last&&j<t.last)
     {
        if(s.ch[i]==t.ch[j])
        {
            i++;
            j++;
        }
        else
        {
            start++;
            i = start;
            j=0;
        }
        
        
     } 
      if(j>=t.last)
        printf("匹配成功!匹配成功的位置索引为%d",start);
      else
        printf("匹配失败!"); 
    
}


 
int main()
{
    string s,t;
    int pos,i;
    int m;
    char a[80];
    char b[80];
 
    init_str(&s, MAX_SIZE);
    printf("请输入字符串1:\n");
    scanf("%s",a);
    printf("请输入字符串2:\n");
    scanf("%s",b);
 
   for(i = 0; a[i]; i++)//for(i=0;a[i]!='\0';i++) 后者这个准确些 
        ;
   create_str(&s, a, i);// 
   for(i = 0; b[i]; i++)//for(i=0;bi]!='\0';i++) 后者这个准确些 
        ;
    
    create_str(&t, b, i);
                
    StrIndex (s,0,t);
                                 
    
    return 0;
}

An Improved KMP Algorithm for String Pattern Matching

The efficiency of the KMP algorithm is faster than that of the Bf algorithm. The specific algorithm will not be described, but the relevant code implementation is simply given here. The content of the specific algorithm implementation can refer to the textbook and the online explanation.

Code

/*串的KMP匹配函数 */
/*http://www.ruanyifeng.com/blog/2013/05/Knuth%E2%80%93Morris%E2%80%93Pratt_algorithm.html*/
/*上面的是对KMF算法的讲解,我个人觉得写的很清晰了emmmmm*/ 

#include<stdio.h>
 
#define MAX_SIZE 128
 
typedef struct
{
    char ch[MAX_SIZE];
    int last;//串的长度 
}string;
 
void init_str(string * s, int max)
{
    int i;
 
    for(i = 0; i < max; i++)
        s->ch[i] = 0;//而谁的 ASCII 码对应的是 整型的0呢?是字符'\0'
    s->last = 0;
}
 
void create_str(string * s, char * a, int n)
{
    int i=0; 
    for(;i < n; i++)
        s->ch[i] = a[i];
 
    s->ch[i+1] = '\0';
    s->last = n;
    printf("你输入的字符串为:\n");
    printf("%s\n",s->ch);
}
 


//顺序串的基KMP算法

void Index_KMP(string s,int pos,string t)
{
    int i,j;
    int next[MAX_SIZE];
    i = pos;
    j = 1;//规定若指向了空格的话就是0,所以这里从1开始 
    while(i<s.last&&j<t.last)
    {
        if(j == 0||s.ch[i] == t.ch[j])
        {
            i++;
            j++;
        }
        else
        {
            j = next[j];
        }
         
    }
    if(j>=t.last)
        printf("匹配成功!匹配成功的位置索引为%d",i-j);//i是模式串移动后“最后”的位置索引, 
    else                                              //如果想要开始索引,减去“移动的块的长度就可以了” 
        printf("匹配失败!"); 

}


 
int main()
{
    string s,t;
    int pos,i;
    int m;
    char a[80];
    char b[80];
 
    init_str(&s, MAX_SIZE);
    printf("请输入字符串1:\n");
    
    printf("请输入字符串2:\n");
    scanf("%s",b);
 
   for(i = 0; a[i]; i++)//for(i=0;a[i]!='\0';i++) 后者这个准确些 
        ;
   create_str(&s, a, i);// 
   for(i = 0; b[i]; i++)//for(i=0;bi]!='\0';i++) 后者这个准确些 
        ;
    
    create_str(&t, b, i);
                
    Index_KMP (s,0,t);
                                 
    
    return 0;
}

references

  • Data Structure - Described in C Language (Second Edition) [Geng Guohua]

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325348165&siteId=291194637