Shift-Or,Shift-And,KMP 限制条件木有写. template<typename ElemType> int MatchSO(ElemType* t, int n, ElemType* p, int m, vector<int>& result) { typedef unsigned long long NumberType; const int CharSet = 128; NumberType B[CharSet]; NumberType D = ~0; NumberType Check = 1 << (m-1); NumberType BIT = 1; for (int i = 0; i < CharSet; ++i) B[i] = ~0; for (int i = 0; i < m; ++i, BIT <<= 1) B[p[i]] &= ~BIT; result.clear(); for (int i = 0; i < n; ++i) { D = (D << 1) | B[t[i]]; if ((D & Check) == 0) result.push_back(i-m+1); } return result.size(); } template<typename ElemType> int MatchSA(ElemType* t, int n, ElemType* p, int m, vector<int>& result) { typedef unsigned long long NumberType; const int CharSet = 128; NumberType B[CharSet]; NumberType D = 0; NumberType Check = 1 << (m-1); NumberType BIT = 1; for (int i = 0; i < CharSet; ++i) B[i] = 0; for (int i = 0; i < m; ++i, BIT <<= 1) B[p[i]] |= BIT; result.clear(); for (int i = 0; i < n; ++i) { D = ((D << 1) | 1) & B[t[i]]; if ((D & Check) != 0) result.push_back(i-m+1); } return result.size(); } template<typename ElemType> int MatchKMP(ElemType* t, int n, ElemType* p, int m, vector<int>& result) { int * Prefix = new int[m]; { Prefix[0] = -1; for (int i = 1, F = -1; i < m; ++i) { while (F >= 0 && p[F+1] != p[i]) F = Prefix[F]; if (p[F+1] == p[i]) ++F; Prefix[i] = F; } } { result.clear(); for (int i = 0, F = -1; i < n; ++i) { while (F >= 0 && p[F+1] != t[i]) F = Prefix[F]; if (p[F+1] == t[i]) ++F; if (F+1 == m) { result.push_back(i-m+1); F = Prefix[F]; } } } delete[] Prefix; return result.size(); }