Problem 528
Number of Matching Subsequences
Medium · 20 pts · String, Hash Table, Binary Search
Problem
You are given a string S and a list of N words. Count how many of the words are subsequences of S. A word is a subsequence if it can be obtained from S by deleting some characters without changing the order of the rest. Repeated words count each time they appear. For speed, store for each letter the sorted positions where it appears in S, and match each word by binary searching for the next position after the previous match.
Input
The first line contains S. The second line contains N. The third line contains N space-separated words.
Output
Print how many of the words are subsequences of S.
Constraints
1 <= |S| <= 50000; 1 <= N <= 5000; 1 <= |word| <= 50; all lowercase letters
Example input
abcde 4 a bb acd ace
Example output
3
Explanation
With S = abcde, the words a, acd and ace are subsequences, but bb is not because S has only one b. So the answer is 3.