14491 - Arrange Big orange cat's puzzle   

Description

Last time you helped Orange Cat complete his puzzles. This time, he wants to arrange these puzzles on his shelf. He has a particular fondness for a specific pattern, represented here by a string S of length m. The more times this pattern S appears as a substring within a puzzle, the more he likes it! We'll call the number of occurrences of S the preference level.

 

Please help rearrange these n puzzles (P1 to Pn) by sorting them in descending order based on their preference levels. If two puzzles have the same preference level, maintain their original relative order.

Input

The first line contains two positive integers n and m.

The following n lines each contain a string Pi.

The last line contains a string S of length m.

Constraints

  • 1 <= n <= 1000
  • 1 <= | Pi |, m <= 10000
  • All strings contain only lowercase English letters.

Subtasks

  • testcases 1 ~ 2: n <= 10, | Pi |, m <= 100
  • testcases 3 ~ 4: | Pi | <= 200, m <= 100
  • testcases 5 ~ 6:  No additional restrictions.

Output

Output n lines, where the i-th line contains a string representing the puzzle placed in the i-th position.

Please remember to print "\n" at the end, and dont print space (" ") at the end of every line

Hint1:
Please implement string swapping in bubble sort using strcpy to avoid TLE (Time Limit Exceeded).

Hint2: 
For testcases 5 ~ 6, please refer to the following polynomial. This technique is called rolling hash, which converts a string into a number within a fixed range, allowing string comparison in O(1) time:

(a* mn-1 + a* mn-2 + a* mn-3 + ... + an-1 * m1 + a* m0) mod P (m is a small prime number like 37, P is a big prime number like 10+ 7)

more reference:

Sample Input  Download

Sample Output  Download




Discuss