Interesting Words
Time limit3sMemory limit2048 MB
Count the ordered sequences of given words whose concatenation is a palindrome of exact length L, modulo 1e9+7.
- Level
Hard8 of 10
- Topics
- Dynamic programming, String matching, Hash map
- Solved
- No attempts yet
Problem
Ene loves palindromes.
Ene has a list of interesting words. She wants to select some interesting words and concatenate them to form a palindrome of length exactly . Each interesting word can be chosen multiple times or not chosen at all.
Ene wants to know the number of ways to do this. Two ways are considered different when the sequences of the concatenated interesting words in them are different. Multiple different ways may result in the same palindrome. Print the result modulo , since the answer may be very large.
Input
The first line contains two positive integers and , the number of interesting words and the length of the palindrome to be formed (, ).
Each of the following lines contains an interesting word (, ). The words contain only lowercase English letters and are pairwise distinct.
Output
Print a single integer: the number of ways to form a palindrome, modulo .
Hint
In the first sample, the five different ways are: stack cats; evil olive; eel eve lee; lee eve eel; eve eve eve.