//字符串首尾模式匹配#include <iostream.h>#include <string>void main(){ char* s="hhhsauhduwdh"; char* t="sau"; int i=0; int j=0; int Slen=strlen(s); int Tlen=strlen(t); while(i<=Slen-Tlen+1) {// if(s[i]!=t[0]){i++;} else if(s[i]==t[0]&&s[i+Tlen-1]!=t[Tlen-1]){i++;} else { int k=1; j=1; while(j<Tlen&&s[i+k]==t[j]) {k++;j++;} if(j==Tlen) { cout<<i<<endl; return; } else ++i; //重新开始下一次匹配检测 } } cout<<"0"<<endl;}

评论