Concatenating Teams
Time limit2sMemory limit512 MB
Given two lists of team names, count the teams in each university whose removal makes some A+B concatenation impossible to form.
- Level
Medium7 of 10
- Topics
- String matching, Trie, String
- Solved
- No attempts yet
Problem
Pepito is a marathon coach who often likes to "concatenate" the names of two teams, such as "AJI" and "Oxidados", to make names for new teams, such as "AJIOxidados".
Pepito coaches teams from two universities where he teaches. He decided to consider every possible concatenation of one team name from university A with one team name from university B. The university A name always comes first, followed by the university B name. For example, if university A has the teams "Buen" and "Kilo" and university B has the teams "Pan" and "Flauta", the concatenations he considers are "BuenPan", "BuenFlauta", "KiloPan", and "KiloFlauta".
A team is peculiar if removing it makes the set of concatenations lose every concatenation that uses that team's name.
In the example above, every team is peculiar. Now suppose university A has the teams "xx" and "xxy", and university B has the teams "z", "yz", and "xx". Then university A's "xx" is not peculiar, because "xx" + "yz" = "xxyz" can also be produced without that team, as "xxy" + "z" = "xxyz". For the same reason, "yz", "xxy", and "z" are not peculiar. The only peculiar team in this example is university B's "xx". It is used to create "xxxx" and "xxyxx", and neither name can be created without using that team.
Given the team names of both universities, count the peculiar teams in each university.
Input
The first line contains two integers M and N, separated by a space. University A has M teams, with 1 ≤ M ≤ , and university B has N teams, with 1 ≤ N ≤ .
The second line contains the M team names of university A, separated by spaces. The third line contains the N team names of university B, separated by spaces. Every name consists only of lowercase Latin letters. Distinct teams within the same university have distinct names.
The total length of all team names is at most .
Output
Print one line with two integers: the number of peculiar teams in university A and the number of peculiar teams in university B, separated by a space.