Implement strStr().
Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack
实现strStr()函数:返回模式P在文本T中的索引,如果没有发现则返回-1。
Brute Force:(暴力求解法)
int strStr(char* haystack, char* needle) {
int h_index=0;
int n_index=0;
if(*haystack=='\0'&&*needle=='\0')
h_index=0;
else if(*haystack=='\0'&&*needle!='\0')
h_index=-1;
while(*haystack!='\0') //判断haystack字符数组是否结束
{
while(*needle!='\0') //判断needle字符数组是否结束
{
if(*haystack!=*needle)
{
haystack-=n_index; //h指针回位
needle-=n_index; //n指针回位
n_index=0;
break; //有一个字符不相等
}
else
{
haystack++;
needle++;
n_index++;
}
}
if(*needle=='\0') //确定是子字符串
break;
else //比较下一个
{
haystack++;
h_index++;
if(*haystack=='\0')
{
h_index=-1;
break;
}
}
}
return h_index;
}
思路很简单,匹配失败则移动一个字符继续匹配,最坏复杂度为O(n^2),在LeetCode OJ上提交时由于Time limit exceeded失败。
Sunday 算法
int strStr(char* haystack, char* needle) {
int h_index,n_index,m,n=0;
n=strlen(haystack);
m=strlen(needle);
if(*haystack=='\0'&&*needle=='\0')
h_index=0;
else if(*haystack=='\0'&&*needle!='\0'||n<m)
h_index=-1;
else
{
for(h_index=0;h_index<(n-m+1);)
{
for(n_index=0;n_index<m;n_index++) //P与T逐一对比
{
if(*(haystack+h_index+n_index)!=*(needle+n_index))
break;
}
if(n_index<m&&h_index==(n-m)) //最后一次匹配失败
{
h_index=-1;
break;
}
if(n_index<m) //非最后一次匹配失败
{
h_index+=m; //T的下标移动至haystack+m处
for(n_index=m-1;n_index>-1;n_index--) //从右往左将P与haystack+m处字符比较
{
if(*(haystack+h_index)==*(needle+n_index))
break;
}
if(n_index==-1&&(n-h_index+1)>m) //匹配不上且容得下P,从haystack+m+1处开始比较
h_index++;
else if(n_index>-1&&(n-h_index)>=(m-n_index)) //匹配上且T能容下移动过的P,将P移至匹配字符处
h_index-=n_index;
else //否则结束
{
h_index=-1;
break;
}
}
else //直接匹配成功
break;
}
}
return h_index;
}
其核心思想是:在匹配过程中,模式串发现不匹配时,算法能跳过尽可能多的字符以进行下一步的匹配,从而提高了匹配效率。
提交结果如下
还有其他诸如KMP算法,但实现比较复杂我没有搞懂。