leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

0792.cpp (681B)


      1 class Solution {
      2   public:
      3     int numMatchingSubseq(const string &s, const vector<string> &words) const {
      4         static int pos[50001][26];
      5         const int n = size(s);
      6         int res = 0;
      7 
      8         memset(pos[n], 0x00, sizeof(pos[n]));
      9         for (int i = n - 1; i >= 0; i--) {
     10             memcpy(pos[i], pos[i + 1], sizeof(pos[i]));
     11             pos[i][s[i] - 'a'] = i + 1;
     12         }
     13 
     14         for (const auto &word : words) {
     15             int crnt = 0;
     16             for (const char c : word) {
     17                 if (!pos[crnt][c - 'a']) goto next;
     18                 crnt = pos[crnt][c - 'a'];
     19             }
     20             res++;
     21         next:;
     22         }
     23 
     24         return res;
     25     }
     26 };