首页 > 其他 > 详细

朴素模式串匹配

时间:2014-03-14 22:18:46      阅读:524      评论:0      收藏:0      [点我收藏+]
最大时间复杂度为|P|(|T|-|P|+1)次
#include<cstdio>
#include<cstring>
#include<iostream>
#include<stack>
#include<queue>
#include<string>
#include<algorithm>
using namespace std;
int match(string T,string P)//返回目标串与模式串完全匹配的第i个位置
{
    int p=0;
    int t=0;
    int plen=P.length();
    int tlen=T.length();
    if(tlen<plen)
        return -1;
    while(p<plen&&t<tlen)
    {
        if(T[t]==P[p])
            t++,p++;
        else
        {
            t=t-p+1;
            p=0;
        }
    }
    if(p>=plen)
        return t-plen+1;
    else
        return -1;
}
int main()
{
    string s1="abcde";
    string s2="de";
    printf("%d\n",match(s1,s2));
    return 0;
}



朴素模式串匹配,布布扣,bubuko.com

朴素模式串匹配

原文:http://blog.csdn.net/u013889359/article/details/21240399

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!