Data structure--correlation algorithm implementation of string

Source: Internet
Author: User

Implementation of sequential string insertion function with data structure--string correlation algorithm

When inserting a sequential string, inserting a pos into two parts (assuming a, B, length of LA, LB), and to be inserted (assuming C, length is LC), the string is converted to ACB by AB before insertion, and because it is a sequential string, the insertion causes the element to move. The following three scenarios may occur:

    • ① Insert after string length (la+lc+lb) <=maxlen, then B is moved back to the position of the LC element, and then C is inserted;
    • ② inserted after the string length >=maxlen and Pos+lc <=maxlen, then the B will be moved back to some of the characters are discarded;
    • ③ inserts the string length >maxlen and Pos+lc > MaxLen, all the characters of B are discarded (no need to move back), and C is discarded at the time of insertion.
Code implementation
#include <stdio.h> #define Max_size typedef struct{CharCh[max_size];intLast;} StringvoidINIT_STR (String * s,intMax) {intI for(i =0; i < Max; i++) S->ch[i] =0;//and whose ASCII code corresponds to the integer type of 0? is the character ' \ s 'S->last =0;}voidCREATE_STR (String * s,CharAintN) {intI=0; for(; i < n; i++) s->ch[i] = A[i]; S->ch[i+1] = '\0';    S->last = n; printf"The string you entered is:\ n"); printf"%s\ n", s->ch);}voidINSERT_STR (String * s,Char* Substr,intLenintPOS) {intIif(Pos <0|| POS >= max_size) {printf ("The location of the property is not reasonable!"\ n");return; }if(S->last + len >= max_size) {printf ("Can ' t insert, new string too long!\ n");return; }if(POS >= s->last) {/ * If the position is inserted after last * /         for(i =0; i < Len; i++) S->ch[s->last+i] = Substr[i];//Insert the newly inserted string from the group of S stringS->ch[s->last+i] =0;//After inserting a new string, the end of the text ' \ ', 0 is ' \ s ' ASCIIPuts (S-&GT;CH);    S->last + = Len; }Else{/ * If the inserted position is within CH * /         for(i =0; i < Len; i++) S->ch[pos+len+i] = s->ch[pos+i];//Before inserting a new string, divide the s string by 2 parts and move the back part of the whole back to Len length                                            //That is, to "vacate" the string to be inserted          for(i =0; i < Len; i++) S->ch[pos+i] = Substr[i];//Insert a new string into the space just releasedS->ch[s->last+len] =0;//The "string" after insertion has been determined, changing the last '/0 ' positionS->last + = Len;the maximum length of the string is also re-determined, S->last = S->last+len}}intMain () {string S;intPOS, I;Chara[ the],b[ the];    Init_str (&s, max_size); printf"Please enter a string:\ n"); scanf'%s ', a);/ * Traverse the entire string and put the characters in a[] in s, length I, then output * /     for(i =0; A[i]; i++)//for (i=0;a[i]! = ' i++ '; the latter is more accurate.; Create_str (&s, A, i);// printf"Please enter the string you want to insert:\ n"); scanf'%s ', b); for(i =0; B[i];    i++); printf"Please enter the location you want to insert:\ n"); scanf"%d", &pos); Insert_str (&s, B, I, POS);//Find the original string "s", designators the words in b[], the length of the newly inserted string is I, the inserted                               //location for POSprintfthe new string is:\ n"); printf"%s\ n", s.ch);//The new string formed after the output is inserted    return 0;}
Implementation of the Delete function of the string

Specifies the location to delete, and the length of the character to be removed from this location, to implement the delete operation of the deletion string.

Code implementation
#include <stdio.h> #define Max_size typedef struct{CharCh[max_size];intLast;} StringvoidINIT_STR (String * s,intMax) {intI for(i =0; i < Max; i++) S->ch[i] =0;//and whose ASCII code corresponds to the integer type of 0? is the character ' \ s 'S->last =0;}voidCREATE_STR (String * s,CharBintN) {intI=0; for(; i < n; i++) s->ch[i] = A[i]; S->ch[i+1] = '\0';    S->last = n; printf"The string you entered is:\ n"); printf"%s\ n", s->ch);}Delete function of//sequence stringvoidStrdelete (String * s,intPosintLen) {intIif(pos<0|| Pos> (S->last-len)) {printf ("Delete parameter is illegal!" ");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;}intMain () {string S;intPOS, I,n;Chara[ the];    Init_str (&s, max_size); printf"Please enter a string:\ n"); scanf'%s ', a); for(i =0; A[i]; i++)//for (i=0;a[i]! = ' i++ '; the latter is more accurate.; Create_str (&s, A, i);// printf"Please enter the location to delete and the length of the string:"); scanf"%d%d", &pos,&n);                                     Strdelete (&s,pos,n); printfthe new string is:\ n"); printf"%s\ n", s.ch);//The new string formed after the output is inserted    return 0;}
The realization of the comparison function of string

String comparison, that is, two strings are compared (ASCII) in this example, if the string s and equal, then return 0, if S>t returns a positive number, and vice versa returns a negative number.

Code implementation
#include <stdio.h> #define Max_size typedef struct{CharCh[max_size];intLast;} StringvoidINIT_STR (String * s,intMax) {intI for(i =0; i < Max; i++) S->ch[i] =0;//and whose ASCII code corresponds to the integer type of 0? is the character ' \ s 'S->last =0;}voidCREATE_STR (String * s,CharAintN) {intI=0; for(; i < n; i++) s->ch[i] = A[i]; S->ch[i+1] = '\0';    S->last = n; printf"The string you entered is:\ n"); printf"%s\ n", s->ch);}//string comparison functionintStrcompare (String s,string t) {/* If string s and T are equal, return 0 if S>t returns a positive number, or if s<t, returns a negative number */    intIintEqule =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]);//Both ASCII codes}returnEqule; }intMain () {string s,t;intPos,i;intMChara[ the];Charb[ the];    Init_str (&s, max_size); printf"Please enter string 1:\ n"); scanf'%s ', a); printf"Please enter string 2:\ n"); scanf'%s ', b); for(i =0; A[i]; i++)//for (i=0;a[i]! = ' i++ '; the latter is more accurate.; Create_str (&s, A, i);//      for(i =0; B[i]; i++)//for (i=0;a[i]! = ' i++ '; the latter is more accurate.;    Create_str (&t, B, i);     m = Strcompare (s,t); printf"%d", m);return 0;}
Simple pattern matching BF algorithm for string

Simple pattern matching algorithm is a matching algorithm with backtracking, the basic idea of the algorithm is no longer described here, you can find a good answer on the Internet or in the book. This algorithm has a high complexity of time.

Code implementation
/ * Simple pattern matching function for String * /#include <stdio.h> #define Max_size typedef struct{CharCh[max_size];intLast//Length of the string}string;voidINIT_STR (String * s,intMax) {intI for(i =0; i < Max; i++) S->ch[i] =0;//and whose ASCII code corresponds to the integer type of 0? is the character ' \ s 'S->last =0;}voidCREATE_STR (String * s,CharBintN) {intI=0; for(; i < n; i++) s->ch[i] = A[i]; S->ch[i+1] = '\0';    S->last = n; printf"The string you entered is:\ n"); printf"%s\ n", s->ch);}//sequential string basic matching algorithmvoidStrindex (String s,intPos,string t) {/ * Request from Subscript pos of main string ....    intI,j,start;if(T.last = =0) printf ("Successful match!" ");     start = pos; i = start;//The main string does not necessarily start with a character indexed to 0, and the specific location is specified by the customerj =0;//pattern string starting from index 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 ("Successful match!" The match succeeded with a location index of%d ", start);Elseprintf"The match failed!" "); }intMain () {string s,t;intPos,i;intMChara[ the];Charb[ the];    Init_str (&s, max_size); printf"Please enter string 1:\ n"); scanf'%s ', a); printf"Please enter string 2:\ n"); scanf'%s ', b); for(i =0; A[i]; i++)//for (i=0;a[i]! = ' i++ '; the latter is more accurate.; Create_str (&s, A, i);//     for(i =0; B[i]; i++)//for (i=0;bi]! = ' i++ '; the latter is more accurate.;                    Create_str (&t, B, i); Strindex (s),0, t);return 0;}
Improved KMP algorithm for pattern matching of strings

The efficiency of the KMP algorithm is faster than the BF algorithm, the specific algorithm is no longer described, here is simply the implementation of the relevant code. The content of the specific algorithm can be referred to the textbook and the online explanation.

Code implementation
/ * KMP matching function for string * //*http://www.ruanyifeng.com/blog/2013/05/knuth%e2%80%93morris%e2%80%93pratt_algorithm.html*// * Above is the explanation of the KMF algorithm, I personally think the writing is very clear emmmmm*/ #include <stdio.h> #define Max_size typedef struct{CharCh[max_size];intLast//Length of the string}string;voidINIT_STR (String * s,intMax) {intI for(i =0; i < Max; i++) S->ch[i] =0;//and whose ASCII code corresponds to the integer type of 0? is the character ' \ s 'S->last =0;}voidCREATE_STR (String * s,CharBintN) {intI=0; for(; i < n; i++) s->ch[i] = A[i]; S->ch[i+1] = '\0';    S->last = n; printf"The string you entered is:\ n"); printf"%s\ n", s->ch);}//sequential string based KMP algorithmvoidINDEX_KMP (String s,intPos,string t) {intI,j;intNext[max_size];    i = pos; j =1;//Rules If you point to a space, it's 0, so here it starts with 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 ("Successful match!" The match succeeded with a location index of%d ", i-j);//i is the "last" position index after the pattern string is moved,    Else                                              //If you want to start indexing, subtract "the length of the moved block is OK"printf"The match failed!" "); }intMain () {string s,t;intPos,i;intMChara[ the];Charb[ the];    Init_str (&s, max_size); printf"Please enter string 1:\ n"); printf"Please enter string 2:\ n"); scanf'%s ', b); for(i =0; A[i]; i++)//for (i=0;a[i]! = ' i++ '; the latter is more accurate.; Create_str (&s, A, i);//     for(i =0; B[i]; i++)//for (i=0;bi]! = ' i++ '; the latter is more accurate.;                    Create_str (&t, B, i); INDEX_KMP (s),0, t);return 0;}
Reference documents
    • Data structure-in C language Description (second edition) [Shang]

Data structure--correlation algorithm implementation of string

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.