Train
Time limit1sMemory limit128 MB
Sum over all n! orders of the wagon strings the number of occurrences of their concatenation in t.
- Level
Hard8 of 10
- Topics
- Dynamic programming, String matching, Combinatorics
- Solved
- No attempts yet
Problem
Little Jacus got a toy train made of wagons from his mom. On the back of each wagon there is a serial number made of lowercase English letters.
He can line the wagons up in any order to build a train (always using all wagons). Reading the wagons' serial numbers from left to right yields a single string .
He asked his mom to write a string on a sheet of paper. He treats each string built from one ordering as a pattern and looks for it in . Over all orderings of the wagons, compute the total number of times the pattern formed by that ordering occurs in .
Occurrences at different starting positions are counted separately even when they overlap. The wagons are distinct physical objects, so even if two wagons share the same serial number, each ordering (permutation) is counted on its own.
Input
The first line contains an integer (). Each of the next lines contains the serial number of the -th wagon; each serial number is a non-empty string of lowercase English letters of length at most . The next line contains the string , a string of lowercase English letters of length at most .
Output
Print a single integer: the total number of occurrences in of the patterns formed by all orderings of the wagons.