标签:return oid == string include name ios char s cab

//简单的模式匹配算法
public class BF {
    public static int indexBF(char s[],char p[]){
        int i=0,j=0;
        while(i<s.length&&j<p.length){
            if(s[i]==p[j]){
                i++;
                j++;
            }else{
                i=i-j+1;
                j=0;
            }
        }
        if(j>=p.length){
            return i-p.length;
        }else{
            return -1;
        }
    }
    public static void main(String[] args) {
        char s[] = {'a','b','a','b','c','a','b','c','a','c','b','a','b'};
        char p[] = {'b','c','a'};
        int index = indexBF(s,p);
        System.out.println(index);
    }
}#include <iostream>
#include <string> 
using namespace std;
int indexBF(string S, string T){  
    if (S.size() < 1 || T.size() < 1) 
        return -1;
    int i = 0, j = 0;
    while (i < S.size() && j < T.size()){  
        if (S[i] == T[j]){  
            i++;
            j++;
        }else{  
            i = i- j+ 1;  
            j = 0;  
        }
    }  
    if(j==T.size()) return i - j;  
    return -1;  
}         
int main(){
    string S= "ababcabcacbab";
    string T = "cacb";
    int index = indexBF(S,T);
    cout<<index<<endl;
    return 0;
}标签:return oid == string include name ios char s cab
原文地址:https://www.cnblogs.com/hglibin/p/8976703.html