文字列探索のbrute-forceなアルゴリズム

今日は文字列探索が必要な問題を解いたので、文字列探索のアルゴリズムをついでに勉強した。といっても、まだセジウィック本の該当章の初めの方しか読んでないので、brute-forceな方法しか勉強していない。
brute-forceな方法は以下のような誰でも思いつく方法。この関数は返り値としてtext中のpatternが最初に現れる位置を返す。

int brutesearch(const string pattern, const string text)
{
    int M = pattern.size();
    int N = text.size();
    int i, j;
    for (i = 0, j = 0; i < M && i < N; i++, j++) {
        while (text[i] != pattern[j]) {
            i -= j - 1;
            j = 0;
        }
    }
    if (j == M) {
        return i - M;
    } else {
        return i;
    }
}

文字列探索のアルゴリズムを忘れてたので勉強しなおしたけど、C++を使うなら

searh(text.begin(),text.end(),pattern.begin(),pattern.end())

で同じような(これはiteratorを返すので厳密には少し違う)文字列探索はできる。