1 #include<stdio.h> 2 #include<string.h> 3 4 int BF(char *A, char *B) { 5 int i = 0,j=0; 6 while(i < strlen(A) && j < strlen(B)) { 7 if(A[i]==B[j]) { 8 i++; 9 j++; 10 } else { 11 i = i - j + 1; //j代表j(i)移动的距离,i返回初始位置再从下一个开始 12 j = 0; 13 } 14 } 15 if(j == strlen(B)) 16 return i - strlen(B) + 1; //加一是第几个,不加一是下标 17 18 return -1; 19 } 20 21 int main() { 22 char *A = "ababcabcacbab"; 23 char *B = "abcac"; 24 int num = BF(A, B); 25 printf("A = %s/nB = %s/n", A, B); 26 printf("number = %d/n",num); 27 28 return 0; 29 }
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/273444.html