14505 - Arrange Big orange cat's puzzle II   

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 interger x. The number of substring [l, r] in each string that satisfy the following conditions are defined as the preference levels:

  1. r = l + m - 1
  2. (al * km-1 + al+1 * km-2 + ... + ar-1 * k + ar ) mod y = x ai represents the ASCII code of the i-th character in string a
    example: Assuming the ascii code of string a is like this: a = [1, 2, 3, 4, 5], k = 10, m = 4, l = 2, r = 5, y = 1000, the result of the polynomial above is: (2 * 10+ 3 * 102 + 4 * 101 + 5) mod 1000 = (2345) mod 1000 = 345. 

When a substring with length m satisfies the above conditions, the preference level increases by 1. In other words, you need to calculate how many substrings with length m in a string satisfy the polynomial conditions above. (For a string "abcdefg", when m = 4, all substrings of length m are: "abcd", "bcde", "cdef", and "defg".)

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 five positive integers nm, k, x, y.

The following n lines each contain a string Pi.

Constraints

  • 1 <= n <= 200
  • 1 <= | Pi |m <= 10000
  • 1 <= xy <= 10
  • 1 <= k <= 109
  • All strings contain only lowercase English letters.

Subtasks

  • testcases 1 ~ 2: n <= 10, | Pi |m <= 100
  • testcases 3 ~ 5: | Pi | <= 100, m <= 100
  • testcase 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:
Please be mindful of overflow.

Hint3:
You can use the following template:
for (int i = 0; i + m - 1 < len; i++) {
  int l = i, r = i + m - 1, sum = 0;
  for (int j = l; j <= r; j++) {
    // do something
  }
  if (sum == x) {
    // do something
  }
}

Sample Input  Download

Sample Output  Download

Tags




Discuss