Virtual implementation of string manipulation algorithm

Source: Internet
Author: User
Tags truncated

String: It is a finite sequence of 0 or more characters in a finite character set//a special linear table int Indexz (string s,string t,int pos) {//t is a non-empty string, if there is a substring equal to T after the first POS character in the main string S, Returns the position of the first such substring in S if (pos>0) {n=strlength (S);//Find the length of the main string m=strlength (T);//Find out the length of a non-empty string I=pos;while (i<=n-m+1) { SubString (sub,s,i,m); if (Strcompare (sub,t)!=0)//Let non-empty string and main string compare equality i++;//if not, let non-empty string move on main string one Elsereturn i;//if found the same, Returns the position in the main string}}return 0;} 1. Define the length of the string # # # Maxstrlen 255//users can define the maximum string length typedef unsigned char sstring[maxstrlen+1];//0 number cell to store the lengths of the string//2. String Link: Returns a new string of links from S1 and S2 with T, if not truncated, returns Truestatus Concat (sstring &t,sstring s1,sstring S2) {if (S1[0]+s2[0]<=maxstrlen) If the sum of two string lengths is less than the length of the new string T {T[1...s1[0]=s1[1..s1[0]];//s1[0] represents the last element, assign all the elements in the S1 to T T[s1[0]+1..s1[0]+s2[0]]=s2[1...s2[0]] ;//The S2 element is assigned to the S1 element +1, just link t[0]=s1[0]+s2[0];//the last T string length is two string length and uncut=true;//is not truncated}else if (s1[0]<maxstrsize)// In the second case, the S1 length is less than T, but S1+S2 is greater than T, so that the S1 is fully inserted, and S2 does not fully {t[1..s1[0]=s1[1...s1[0]];//assigns the elements in S1 to T t[s1[0]+1...maxstrlen]=s2[1: maxstrlen-s1[0]];//to link some elements of S2 to S1 the length of the t[0]=maxstrlen;//t is equal to the allocation length of the start T uncut=false;//the string is truncated}else//The third case, the string T equals the length of the string S1. {t[0...maxstrlen]=s1[0...maxstrlen];//S1 all elements into T uncut=false;//be truncated}return Uncut;} 3. The function of the substring//returns the first POS character of the string s with a sub string of length Len//1<=pos<=strlength (s) and 0<=len<=strlength (s)-pos+1status SubString (sstring &sub,sstring s,int pos,int len) {if (pos<1| | pos>s[0]| | len<0| | LEN&GT;S[0]-POS+1)//If the POS is illegal or the length overflows, return error;//returns error Sub[1..len]=s[pos: pos+len-1];//assigns a substring of the matching condition to the new string sub[0]=len;//sub[0] to hold the length of the array return OK; 4. SUBSTRING matching algorithm. : Returns the position of the substring T after the POS character in the main string S,//If it does not exist, returns the 0{i=pos;//i pointer to the main string j=1;//j the pointer while (I&LT;=S[0]&AMP;&AMP;J&LT;=T[0])// If the two pointers are less than the respective maximum length {if (s[i]==t[j])//If the comparison is equal {i++;//let the main string pointer move to the next position j++;//the substring pointer moves to the next position}else{i=i-j+2;//backtracking, is to let the main string pointer back to the last comparison, the most starting point j=1;//let the substring to start from the beginning with the main string comparison}}if (J>t[0])//When the substring pointer moves to the end is still equal, then return to its position, indicating that find return I-t[0];elsereturn 0;} 5. String permutation: The "S" in the string "T" all the occurrences of the "V"//in S to locate T, and then according to the length of the T and V; T.curlen<v.curlen: After moving V.curlen-t.curlent.curlen=v.curlen : Replace T.curlen>v.curlen: Move forward t.curlen-v.curlen//repeat until T exists in s

Virtual implementation of string manipulation algorithm

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.