Pattern matching-BF algorithm

Source: Internet
Author: User

/*** string matching algorithm ***/#include <cstring> #include <iostream>using namespace std; #define OK 1#define ERROR 0#define overflow-2typedef int Status; #define Maxstrlen 255   //user can define the longest string of long typedef char sstring[maxstrlen+1];//within 255 Unit No. 0 Stores the length of the string status Strassign (sstring T, char *chars) {//generates a string whose value equals chars tint i;if (strlen (chars) > Maxstrlen) return Erro R;else {T[0] = strlen (chars); for (i = 1; I <= t[0]; i++) T[i] = * (chars + i-1); return OK;} Fill in the 4.1 bf algorithm int Index (sstring s, sstring t, int pos) {//return mode T in the main string S in the first POS character after the first occurrence of the position. If not present, the return value is 0//where T non-null, 1≤pos≤strlength (S) int i=pos;int j=1;while (i<=s[0]&&j<=t[0]) {if (S[i]==t[j]) {i++ ; j + +;} Else{i=i-j+2;j=1;}} if (j>t[0])  return i-t[0];else        return 0;} Indexint Main () {sstring S; Strassign (S, "Bbaaabbaba"); Sstring T; Strassign (T, "ABB");cout<< "main string and substring in section" <<index (s,t,1) << "Word prompt first match \ n"; return 0;}

Pattern matching-BF 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.