Note: this problem is solvable without the use of the Aho-Corasick algorithm.
The Aho-Corasick algorithm is a well known hated algorithm used to solve many fun annoying string problems. Wesley was recently asked to create a problem involving the Aho-Corasick algorithm. After taking one look at an implementation, he immediately declined. In fact, he was so disgusted by it that he was inspired to make a problem that did not involve the use of the Aho-Corasick algorithm.
However, even problems that do not use the Aho-Corasick algorithm can seem like they do. A very recent ECOO problem involving words in the English language seemed to require the Aho-Corasick algorithm, and not wanting to mislead people into thinking they needed this intriguing disgusting algorithm, Wesley wanted to make it very clear that the Aho-Corasick algorithm will not be required to solve this problem.
You recently found a dictionary containing
Formally, let the strings in the dictionary be
For example, if the dictionary contains the words aho
and corasick
, words in the new language include (but are not limited to): aho
, hoa
, oah
, corasick
, ahocorasick
, hoaaho
, sickcorahoa
, and oahickcorasckcorasihoa
. Words that are not in the new language include (but are not limited to): oha
, hao
, and acorahsicko
. To be clear, even if the word ahocorasick
is in the new language, the Aho-Corasick algorithm does not need to be used to solve the problem.
Given a string
Constraints
The sum of all
Each string
In addition, the problem is guaranteed to be solvable without the Aho-Corasick algorithm.
Input Specification
The first line contains 2 integers
The next
The last line contains the string
Output Specification
This problem is graded with an identical
checker. This includes whitespace characters. Ensure that every line of output is terminated with a \n
character.
Output 0 if you used the Aho-Corasick algorithm to solve this problem, and 1 otherwise. Only solutions that did not use the Aho-Corasick algorithm are considered correct.
Output a single integer, the number of different ways to form this new word from the dictionary. Two ways are considered different if
Formally, let one way of forming
Note: users who submit a solution using the Aho-Corasick algorithm may be banned from making further submissions to this problem … okay maybe not.
Sample Input 1
2 2
sickcora
hoa
ahocorasick
Sample Output 1
1
Sample Explanation 1
Since the Aho-Corasick algorithm was not used to solve this problem, the answer is 1.
The only way to make the word ahocorasick
is by concatenating the second word in the dictionary, rotated once to the right, and the first word, rotated 4 times to the right.
Sample Input 2
5 3
cab
abc
cba
aa
bc
bcaabc
Sample Output 2
2
Sample Explanation 2
Using the formal definition of a unique way to form
Sample Input 3
2 5
wx
yz
wyxz
Sample Output 3
0
Sample Explanation 3
There are no ways to make the string wyzx
by concatenating rotations of words in the dictionary.
Comments
Edit: My algorithm was fine, it turns out that when accessing the map, you should use .count to check if the key exists first otherwise it makes time and memory a lot worse.
Maybe try using hashmap instead
That's the same as unordered right? This still TLEs, do you think my algorithm is wrong?