C++字符串匹配程序,该如何解决

C++字符串匹配程序
算法要求就是:先子串的第一个字符与主串第一个字符进行匹配,然后子串最后一个与主串的第一个后+子串长度后的那个字符进行匹配,再子串第二个字符与主串第二个匹配,依次如此。。。如果匹配不成功再子串第一个与主串第二个匹配,再按原来的方式进行匹配  
.比如:
主串:   abdfefds
子串:dfe
先子串d与主串a进行匹配,匹配不成功;
则子串d与主串b进行匹配还是不匹配;
再子串d与主串d进行匹配能匹配;
然后子串最后一个字符e与主串e进行匹配匹配成功;  
然后子串f与主串f进行匹配能匹配成功,
匹配成功!能匹配   返回主串中能匹配成功的首个字符在主串中的位置!

菜鸟跪求各位高手!请赐教!谢谢


------解决方案--------------------
朴素(Naive)字符串匹配算法, 作为最原始的字符串匹配算法,它的时间复杂度是O((n-m+1)m)

#include "stdio.h "

//计算字符串的长度
int Length(char *s)
{
int count=0;
while(*s++!= '\0 ')
count++;

return count;
}

//字符串匹配
void NaiveStringMatching(char *t,char *p)
{
int n=Length(t);
int m=Length(p);
if(n <m)
{
printf( "Error:The P is longer than T!\n ");
return;
}

bool find=true;

printf( "The string T is %s\n ",t);
printf( "The string P is %s\n ",p);
for(int s=0;s <=n-m;s++)
{
find=true;
for(int i=0;i <m;i++)
{
if(t[s+i]!=p[i])
{
find=false;
break;
}
}
if(find)
printf( "Pattern occurs with shift:%d\n ",s+1);
}
}

int main()
{
char t[]= "abcdebcg ";
char p[]= "bcdebcg ";

NaiveStringMatching(t,p);
return 0;
}


------解决方案--------------------
#include <iostream.h>
#include <string.h>

//#define MAXSTRLEN 255
//typedef char string[MAXSTRLEN+1];
//using namespace std;

int find(char *str1,char *str2)
//S为主串,T为子串
{
int index=1;
char *p,*m,*q,*n;
char *Sstart,*Send,*Tstart,*Tend;
Sstart=str1;
Tstart=str2;
Send=Sstart+strlen(str2)-1;
Tend=Tstart+strlen(str2)-1;
p=Sstart;
m=Tstart;
q=p+strlen(str2)-1;
n=q+strlen(str2)-1;
if(strlen(str1) <strlen(str2))
{

cout < < "子串比主串长不能进行匹配! " < <endl;
return -1;
}//if
else

{
while(q <=str1+strlen(str1))
{
if(*p==*m&&*q==*n)
{
p++;q--;
m++;n--;
if(p=q)
{ cout < < "匹配成功 " < <endl < < "从主串第 " < <index < < "个字符开始匹配 " < <endl;break;}


}
else
{ index++;
Sstart++;Send++;


if(Send> str1+strlen(str1)-1)
{ cout < < "匹配不成功! ";
break;
}
else
{
p=Sstart;q=Send;
m=Tstart;
n=Tend;
}
// cout < < "匹配成功! " < <endl < < "从主串第 " < <index < < "个字符开始匹配! " < <endl;

}


}//while


}//else
return 1;
}

void main()
{
char str1[255],str2[255];
cout < < "请输入主串: ";
cin> > str1;
cout < <endl;
cout < < "请输入子串: ";
cin> > str2;
cout < <endl;
find(str1,str2);
}

// cout < <find(str1,str2) < <endl;


还是只能靠自己啊