#include <stdio.h> #include <stdlib.h> #include <string.h> int maxPrefix(const char* str1, const char* str2); int state_transport(const char* p, const int n, const int m, char a) { char pk[n+2]; strncpy(pk, p, n); pk[n]=a; pk[n+1]='\0'; return maxPrefix(pk, p); } int maxPrefix(const char* str1, const char* str2) { int len1=strlen(str1); int len2=strlen(str2); int i , j, k; int count = 0; int maxCount=0; int l = len1 > len2 ? len2:len1; for(i=1; i<=l; ++i){ for(k=len1-i, j=0;k<len1 && j<len2; ++k, ++j){ if(str1[k]==str2[j]){ count++; } else{ count = 0; break; } } if(count > maxCount) maxCount=count; count = 0; } return maxCount; } int matchCount(const char* t, const char* sub_str, int m) { int len = strlen(t); int q = 0; for(int i = 0; i < len; ++i){ q = state_transport(sub_str, q, m, t[i]); if(q == m){ printf("occur in %d\n", i+1-m); } } } int main() { const char* str1="abcababababab"; const char* str2="abab"; matchCount(str1, str2, 4); }