Alice and Bob play Contact

All submissions for this problem are available.
Chef Alice and Chef Bob are going to play their version of the social word game called Contact. First things first, let's say that the vocabulary of Alice and Bob are defined by English words A_{1}, A_{2}, ..., A_{N} and B_{1}, B_{2}, ..., B_{M}, respectively. Also, we define Prefix(S, i) as prefix of string S consisting of first i characters. We define S as length of string S.
Before the game starts Alice tells Bob a word A_{i} from her vocabulary. The game proceeds as follows:
 In the first move, Bob tells a word P from his vocabulary such that Prefix(P, 1) == Prefix(A_{i}, 1).
 In second move, Bob tells a word P (which shouldn't have been used before by him) from his vocabulary such that Prefix(P, 2) == Prefix(A_{i}, 2).
 In the j^{th} move, Bob tells a word P (which wasn't used earlier by him) from his vocabulary such that Prefix(P, j) == Prefix(A_{i}, j).
 The game ends when Bob has made A_{i} moves or Bob fails to make a move.
Bob is secretly aware of Alice's whole vocabulary and also, he wants to spend as much time possible with her. Now, they'll play this game for all the N words in Alice's vocabulary. Note that all games are independent. You have to tell Bob what is the sum of maximum number of moves he can make for each of the N games.
Input
The first line of the input contains integers N and M denoting the size of vocabularies of Alice and Bob, respectively.
Next N + M lines contain strings A_{1}, A_{2}, ..., A_{N} and B_{1}, B_{2}, ..., B_{M}, each in a separate line.
Output
Output the sum of maximum number of moves for each of the N games (i.e. if Alice and Bob play the game for each word in Alice's vocabulary).
Constraints
 1 ≤ N, M ≤ 10^{5}
 1 ≤ A_{i}, B_{i} ≤ 10^{5}
 1 ≤ Sum of lengths of A_{i} and B_{i} for all i ≤ 10^{6}
 All strings consist of English lowercase alphabets.
 All strings in array A are distinct.
 All strings in array B are distinct.
Example
Input: 3 2 ac abd b ae bd Output: 3
Explanation
Game 1:
Alice choses word "ac". Bob can make a maximum of 1 move. In first move, Bob tells "ae". Now Bob can't make any more moves.
Game 2:
Alice choses word "abd". Again, Bob can make a maximum of one move by telling the word "ae".
Game 3:
Alice choses word "b". Again, Bob can make a single move in which he tells the word "bd". Game ends here because Bob cannot make more than A_{i} moves.
So, you can see that total number of moves Bob can make is 1 + 1 + 1 = 3.
Author:  admin3 
Tags  admin3 
Date Added:  15122016 
Time Limit:  1 sec 
Source Limit:  50000 Bytes 
Languages:  C, CPP14, JAVA, PYTH, PYTH 3.6, PYP3 
Comments
 Please login at the top to post a comment.
SUCCESSFUL SUBMISSIONS
Fetching successful submissions
HELP
If you are still having problems, see a sample solution here. 