#include <iostream>
#include <stdlib.h>
#define len1 100
#define len2 50
int main(int argc, char *argv[])
{、
char str[len1],str1[len2];
char *p=str;
char *q=p;
char *r=str1;
int time=0;
cin>>str;
cin>>str1;
while(*p)
{ q=p;
r=str1;
while(*q==*r&&*q) {r++;q++;}
if(*r=='/0') {time++;cout<<"posite is "<<(q-strlen(str1)-str)<<endl;}
p++;
}
cout<<time<<endl;
system("PAUSE");
return 0;
}
感觉和KMP比起来还是浪费多了。。。
本文介绍了一个简单的字符串匹配算法实现,该算法通过输入两个字符串并遍历检查子串是否存在主串中,若存在则记录出现次数及位置。尽管该算法效率不及KMP等高级算法,但有助于理解基本的字符串匹配原理。

被折叠的 条评论
为什么被折叠?



