Complete the Word

Source: Codeforces 716B

  1. Read the problem and summarise the task requirements.

For now, let’s just determine whether the string is nice or not, and worry about filling in any question marks later.

Start with special cases of the problem. Suppose there are no question marks, i.e. string s is purely alphabetic.

  1. Design a subroutine to test whether a given character ch appears in string t of length 26.
  2. Extend this subroutine to test each letter of the alphabet in turn to determine whether each appears exactly once from index i to index i+25 of s.
  3. Hence design an algorithm which determines whether string s is nice by testing each substring independently.

Now, let’s add back more of the original problem. Suppose string s consists of uppercase letters and question marks, with the task of determining whether it is nice or not.

  1. Recall that a length 26 substring containing each letter exactly once makes a purely alphabetic string nice. What change(s) must we make to this criterion in order to apply it to string s?
  2. Hence design an algorithm which determines whether string s is nice.

It remains to complete the word.

  1. Suppose that the length 26 substring starting at index i satisfies the criterion from Q4. Design a subroutine which fills in any question marks within the substring so that each letter appears once within the completed word.
  2. There could still be question marks elsewhere in s. How should we fill those in?
  3. Put it all together.
  4. Submit your program for judging!

The solution we’ve developed is sufficient for the given bounds, but it’s far from optimal.

  1. Recall that we tested each substring by going through each letter in turn. Can you instead test all letters in parallel?
  2. Modify your earlier solution to make use of this optimisation.

There are even further optimisations available.

  1. Until this point, we’ve considered each substring independently. However, consecutive substrings have a lot in common! What information can you carry from one substring to the next, and what updates do you need to make?
  2. Modify your earlier solution to make use of this optimisation.

For an extension, let’s add updates! Note that you need some material from future lectures to solve the following problem.

  1. Suppose ZS gives you the length n of the string, initially all question marks, and also m updates (up to 50,000) of the form “change the character at index i of the string to ch”, where ch can be an uppercase letter or a question mark. After each update you must report whether or not it is possible to fill in any missing letters so that the resulting word is nice.

    Sample Input

    26 3
    1 A
    2 A
    1 B

    Sample Output

    YES
    NO
    YES

    Explanation

    The first line indicates that the string initially has 26 question marks, and three updates will follow.