#include<iostream>
using namespace std;
#define MAXSIZE 255
typedef struct String{
char data[MAXSIZE];
int length;
}String;
void initString(String &s){
s.length = 0;
}
//赋值
void valueString(String &s,char *data){
int i = 1;
while(*data !='\0'){
s.data[i++] = *data++;
}
s.length = i-1;
}
//朴素匹配算法
//n主串 m 模式串
int compareString(String n,String m){
int nLen = n.length;
int mLen = m.length;
int i = 1;
int j = 1;
while(i <= nLen && j <= mLen){
if(n.data[i] == m.data[j]){
i++;
j++;
}else{
i = i-j+2;
j = 1;
}
}
if(j > mLen){
return i - mLen;
}else{
return 0;
}
}
int main(){
String n;
initString(n);
valueString(n,"aaaaaaaaaaaaaaaaaaab");
String m;
initString(m);
valueString(m,"aaaa");
int flag = compareString(n,m);
cout<<flag<<endl;
return 1;
}
C/C++ 串,串的朴素匹配算法
最新推荐文章于 2024-11-06 11:05:01 发布