LeetCruel
It's actually not too bad.

3850. Glasgow Multi-Word Phonetic Palindrome IV: Last Orders

Medium Linked List Segment Tree Rolling Hash Sweep Line

You are given the head of a circular doubly linked list. Each node contains a lowercase English letter or a space. The circle holds w words, and every word is followed by a gap of one or more spaces. head is the first letter of a word.

A speaker in Glasgow may begin at any word. Starting at a word, they say all w words exactly once, in the forward direction, wrapping around the circle.

You are also given integers k (the speaker's initial intoxication level), j (the pause threshold) and m (the number of distinguishable states of intoxication), and a pronunciation dictionary. pronunciation[word] is a list of exactly m nonempty lists of phoneme tokens, one for each level from 0 to m - 1. All pronunciations are supplied; you still do not need to know the dialect.

Pronunciation rules

  1. For each word word, append pronunciation[word][level] to the phoneme sequence.
  2. After every word except the last, let g be the number of spaces in the gap that follows it, and i the zero-based index of that gap counted from the starting word. Update the level using both rules below, simultaneously:
    • Increase the level by 1 if the current word ends in two consecutive consonants, or its last letter and the next word's first letter are both consonants. If both conditions hold, increase it only once.
    • Decrease the level by 1 if g > j + i.
  3. The level is always taken modulo m, so it stays between 0 and m - 1.
  4. The gap after the last word is not processed. Spaces produce no phonemes.

A consonant is any letter other than a, e, i, o, u. Phoneme tokens are atomic and case-sensitive.

Extra pints

Before starting, the speaker may drink x extra pints, where 0 ≤ x ≤ m - 1. They then begin at level (k + x) mod m.

The cost of a starting word is the smallest x for which the phoneme sequence of the utterance is a palindrome. If no such x exists, the speaker is asked to leave, and the cost is m.

Return the sum of the costs of all w starting words.

Example 1:

Input: head = "no   own ", k = 0, j = 1, m = 2
pronunciation = {
  "no": [["N", "OW"], ["N", "AW"]],
  "own": [["OH", "N"], ["OW", "N"]]
}
Output: 1

Explanation: The string wraps around: the single space after "own" leads back to "no".

  • Starting at "no" with no extra pints: the gap has 3 spaces, exceeding the threshold 1, so the level drops from 0 to 1 (modulo 2). The sequence is [N, OW, OW, N], a palindrome. Cost 0.
  • Starting at "own": the gap after it has 1 space, so there is no pause, but "own" ends in two consonants, so the level rises by 1. With no extra pints the sequence is [OH, N, N, AW]. With one, it is [OW, N, N, OW], a palindrome. Cost 1.

Example 2:

Input: head = "paul    potts ", k = 0, j = 2, m = 3
pronunciation = {
  "paul": [["P", "AO", "L"], ["P", "AW", "L"], ["P", "OH"]],
  "potts": [["P", "O", "T", "S"], ["P", "OH", "S"], ["P", "O"]]
}
Output: 6

Explanation: No starting word and no number of pints produces a palindrome. The speaker is asked to leave twice, at a cost of 3 each.

Example 3:

Input: head = "hush  no   own ", k = 0, j = 1, m = 3
pronunciation = {
  "hush": [["SH"], ["HH", "AH", "SH"], ["AE", "N"]],
  "no": [["N", "OW"], ["N", "AW"], ["N", "AE"]],
  "own": [["OH", "N"], ["OW"], ["N", "SH"]]
}
Output: 5

Explanation:

  • Starting at "hush" with no extra pints: the gap after "hush" has index 0 and 2 spaces, so the pause rule applies and cancels the consonant rule. The gap after "no" has index 1 and 3 spaces, so the level drops to 2. The sequence is [SH, N, OW, N, SH]. Cost 0.
  • Starting at "no" with two extra pints: the levels are 2, 1 and 2, and the sequence is [N, AE, OW, AE, N]. Fewer pints do not work. Cost 2.
  • Starting at "own": the gap after "hush" now has index 1, and 2 spaces no longer exceed the threshold, so the same gap that paused the first utterance does not pause this one. No number of pints produces a palindrome. Cost 3.

Constraints:

  • 2 ≤ number of nodes ≤ 105.
  • 1 ≤ m ≤ 8; 0 ≤ k < m; 0 ≤ j ≤ 105.
  • The list is a single valid circular doubly linked list. head is the first letter of a word, and its predecessor is a space.
  • Every word in the circle has a dictionary entry with exactly m nonempty lists.
  • The dictionary contains at most 2 · 105 phoneme tokens in total.
  • A single utterance may contain more than 109 phonemes.
  • You may not modify the linked list or the dictionary.
Hint 1The change in level at a gap never depends on the level itself. So a run of consecutive words can be summarised by what it sounds like for each of the m levels it could be entered at, plus the total change in level across it.
Hint 2Write the circle out twice in a row. Every utterance is then a window of w consecutive words. For a fixed position in this row, how does the pause rule behave as the starting word moves forward?
Hint 3You cannot store an utterance, but you can store its hash, read in both directions.
Contest problem · Weekly Contest 487   |   Practice version
Code
Python3
Editable practice editor · Local execution unavailable
Testcase
Sample inputs and expected outputs are provided for manual verification.