标签:blog io ar for div log c amp size
#include <stdio.h> #include <STRING.H> #include <MALLOC.H> void GetNext(int *next, char *str) { int len = strlen(str); int i, k, flag = 1; next[0] = -1; for(i = 2; i < len; i++) //next[1]一定为0 { k = next[i - 1]; flag = 1; while(k != -1 && flag == 1) //flag跳出while,k=-1为终止条件 { if(str[k] == str[i - 1]) //如果相等 { next[i] = k + 1; flag = 0; } else //如果不等 { k = next[k]; } } } } int Search(char *str1, char *str2) { int len1 = strlen(str1); int len2 = strlen(str2); int i, j; int *next = (int *)malloc(len2 * sizeof(int)); memset(next, 0, len2 * sizeof(int)); GetNext(next, str2); for(i = 0, j =0; i < len1 && j < len2;) { if(str1[i] == str2[j] || j == -1) { i++; j++; } else { j = next[j]; } } if (j == len2) { return 0; } return 1 ; } int main() { char *str1 = "abcabe"; char *str2 = "ababa"; printf("%d", Search(str1, str2)); }
标签:blog io ar for div log c amp size
原文地址:http://www.cnblogs.com/jsy306/p/3961472.html