首页 > 编程语言 > 详细

简单字符串模式匹配算法的C++实现

时间:2015-07-13 18:16:52      阅读:300      评论:0      收藏:0      [点我收藏+]
/*
*   simpleIndex.cpp
*   Author: Qiang Xiao
*   Time: 2015-07-13
*/

#include<iostream>
#include<string>
using namespace std;

int simpleIndex(const string&, const string&, int);

int main(){
    string t1= "Hello, world!";
    string p1= "o";
    int pos= 0;
    int re= simpleIndex(t1, p1, pos);
    cout<<re<<endl;

    return 0;
}

int simpleIndex(const string& T, const string& P, int pos= 0){
    int startPos= pos, i= pos, j= 0;
    while(i< T.length() && j< P.length()){
      if(T[i]== P[j]){
          i++;
          j++;
      }
      else{
          i= ++startPos;
          j= 0;
      }
    }
    if(j== P.length())
      return startPos;
    else
      return -1;
}

 

串的模式匹配最基本的算法。

当作练习用。

欢迎交流!

简单字符串模式匹配算法的C++实现

原文:http://www.cnblogs.com/ruchicyan/p/4643180.html

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