码迷,mamicode.com
首页 > 其他好文 > 详细

串行KMP代码,谁能弄成并行的吗?

时间:2015-01-09 19:37:07      阅读:210      评论:0      收藏:0      [点我收藏+]

标签:

//#include "stdafx.h"  
#include<iostream>  
#include<string>  
#include <vector>  
using namespace std;  
  
int kmp_find(const string& target,const string& pattern)  
{  
    const int target_length=target.size();  
    const int pattern_length=pattern.size();  
    int* overlay_value=new int[pattern_length];  
    overlay_value[0]=-1;        //remember:next array‘s first number was -1.  
    int index=0;  
  
    //next array  
    for (int i=1;i<pattern_length;++i)  
        //×¢£?′?′|μ?iê?′ó1?aê?μ?  
    {  
        index=overlay_value[i-1];  
        while (index>=0 && pattern[index+1]!=pattern[i])    
        {  
            index=overlay_value[index];  
        }  
        if(pattern[index+1] == pattern[i])  
        {  
            overlay_value[i]=index+1;  
        }  
        else  
        {  
            overlay_value[i]=-1;  
        }  
    }  
  
    //mach algorithm start  
    int pattern_index=0;  
    int target_index=0;  
    while (pattern_index<pattern_length && target_index<target_length)  
    {  
        if (target[target_index] == pattern[pattern_index])  
        {  
            ++target_index;  
            ++pattern_index;  
        }   
        else if(pattern_index==0)  
        {  
            ++target_index;  
        }  
        else  
        {  
            pattern_index=overlay_value[pattern_index-1]+1;  
        }  
    }  
    if (pattern_index==pattern_length)  
    {  
        return target_index-pattern_index;  
    }   
    else  
    {  
        return -1;  
    }  
    delete [] overlay_value;  
}  
  
int main()  
{  
    string sourc="abcabaaabaabcac";  
    string pattern="abaabcac";  
    cout<<kmp_find(sourc,pattern)<<endl;  
    system("pause");  
    return 0;  
}


串行KMP代码,谁能弄成并行的吗?

标签:

原文地址:http://my.oschina.net/u/1454298/blog/365634

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!