当前位置: 首页 >> 程序设计 >> 数据结构和算法 >> 朴素(Naive)字符串匹配算法
 

朴素(Naive)字符串匹配算法

作者:hedongfu      来源:zz     发表时间:2006-09-15     浏览次数:      字号:    

 作为最原始的字符串匹配算法,它的时间复杂度是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;
}

 

责任编辑 webmaster

 
 
 
 
 
评论更多>>
 
 
 
发表
 
姓名: QQ:
性别: MSN:
E-mail: 主页:
评分: 1 2 3 4 5
评论内容:
验证码:
  
  • 请遵守《互联网电子公告服务管理规定》及中华人民共和国其他各项有关法律法规。
  • 严禁发表危害国家安全、损害国家利益、破坏民族团结、破坏国家宗教政策、破坏社会稳定、侮辱、诽谤、教唆、淫秽等内容的评论 。
  • 用户需对自己在使用本站服务过程中的行为承担法律责任(直接或间接导致的)。
  • 本站管理员有权保留或删除评论内容。
  • 评论内容只代表网友个人观点,与本网站立场无关。
  •