字符串匹配算法(一)

来源:互联网 发布:建筑施工安全网络平台 编辑:程序博客网 时间:2024/04/30 08:55

 

 

/* 功能Function Description:  返回子串 T 在 主串 S 中第 pos个字符之后的位置。
   开发环境Environment:          DEV C++ 4.9.9.1
   技术特点Technique:            一般字符串匹配算法
   版本Version:                  1

   作者Author:               qing du
   日期Date:                 20120807
   备注Notes:
*/
#include <iostream>
#include <cstring>
#include <stdlib.h>
using namespace std;
typedef char *SString;
int Index(SString S,SString T,int pose)
{
    int i,j;
    i=pose; j=1;
    while(i<=S[0]&&j<=T[0])
    {
         if(S[i]==T[j]) {++i;++j;}
         else {i=i-j+2; j=1;}
    }
    if(j>T[0]) return i-T[0];   //匹配成功后返回 pose 后匹配的 i - T[0];
    else return 0;
}
int main()
{
    char a[100],b[10];
    int pos;
    cin>>a+1;
   while(cin>>b+1)
    { 
       cin>>pos;
       a[0]=strlen(a+1);
       b[0]=strlen(b+1);                   
       cout<<Index(a,b,pos)<<endl;    
    }
    system("pause");
    return 0;
   
}