- // By hacker
- # Include <iostream>
- # Include <vector>
- # Include <functional>
- # Include <algorithm>
- Using namespace STD;
- Template <typename FTYPE, typename stype>
- Struct equal_cmp
- {
- Bool operator () (const FTYPE & _ F, const stype & _ s) const
- {Return _ f = _ s ;}
- };
- Template <typename randaccessconcept,
- Typename protocol CMP>
- Inline void
- Calfailfunction (
- Const randaccessconcept & in,
- Int Len,
- Vector <int> & ff,
- Equalcmp CMP
- )
- {
- Ff. Clear ();
- Ff. Reserve (LEN );
- Ff. push_back (-1 );
- For (INT curr = 1, K =-1; curr <Len; ++ curr)
- {
- For (; k> = 0 &&! CMP (in [k + 1], in [curr]);
- K = FF [k]);
- If (CMP (in [k + 1], in [curr]) ++ K;
- Ff. push_back (k );
- }
- }
- Template <typename frandaccessconcept,
- Typename srandaccessconcept,
- Typename protocol CMP>
- Inline int
- Kmpsearchfirst (
- Const frandaccessconcept & D,
- Int dlength,
- Const srandaccessconcept & P,
- Int plength,
- Equalcmp CMP,
- Int start = 0
- )
- {
- Vector <int> ff;
- Calfailfunction (p, plength, FF, CMP );
- For (INT I = start, j =-1; I <dlength; ++ I)
- {
- While (j> = 0 &&! CMP (d [I], p [J + 1]) J = FF [J];
- If (CMP (d [I], p [J + 1]) ++ J;
- If (j = plength-1) return I-plength + 1;
- }
- Return-1;
- }
- Template <typename frandaccessconcept,
- Typename srandaccessconcept,
- Typename protocol CMP>
- Inline int
- Kmpsearchall (
- Const frandaccessconcept & D,
- Int dlength,
- Const srandaccessconcept & P,
- Int plength,
- Vector <int> & result,
- Equalcmp CMP
- )
- {
- Vector <int> ff;
- Calfailfunction (p, plength, FF, CMP );
- Result. Clear ();
- For (INT I = 0, j =-1; I <dlength; ++ I)
- {
- While (j> = 0 &&! CMP (d [I], p [J + 1]) J = FF [J];
- If (CMP (d [I], p [J + 1]) ++ J;
- If (j = plength-1)
- {
- Result. push_back (I-plength + 1 );
- J = FF [J];
- }
- }
- Return result. Size ();
- }
- Int main ()
- {
- Int d [] = {1, 2, 3, 1, 2, 2 };
- Char P [] = "/1/2/3/1/2 ";
- Vector <int> result;
- Int found = kmpsearchall (D, 8, P, 5, result, 1__cmp <int, char> ());
- For (INT I = 0; I <found; ++ I)
- Cout <result [I] <"";
- Cout <Endl;
- Return 0;
- }
: Updated some codes that can be used directly and used for short mode strings. I recommend a book called flexible string matching.
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;NumberTypeB[CharSet];NumberTypeD = ~0;NumberTypeCheck = 1 << (m-1);NumberTypeBIT = 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;NumberTypeB[CharSet];NumberTypeD = 0;NumberTypeCheck = 1 << (m-1);NumberTypeBIT = 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();}