String Puzzle
Time limit2sMemory limit512 MB
Given equality hints between substrings of a huge implicit string plus some fixed letters, decide the letters at the queried positions. The hint structure is a partition of the string into sections, and a hint joins one section to an earlier same-length section, so the constraints are interval equalities on an unknown string of length n; the task is to propagate equality and fixed letters across positions, answering ? where a position's letter is not forced. The input size is small (at most 1000 hints and 1000 queries) but n can be 10^9, so positions cannot be enumerated directly and the hint/
- Level
Hard8 of 10
- Topics
- String, Union-find, Implementation
- Solved
- No attempts yet
Problem
The magazine Amazing Coding runs a puzzle contest in every issue and gives away the latest digital gadgets as prizes. Its readers are programmers, so the magazine asks them to solve the puzzles by writing programs.
The puzzle in the latest issue is about deciding some of the letters of a string, called the secret string below, from a collection of hints. The figure shows one example of the hints.

The first hint is the length of the secret string. In the figure it is nine, and the nine boxes correspond to nine letters. Letter positions, that is box numbers, are counted from 1, from the left to the right.
Hints of the second kind give the letter of the secret string at a specific position. In the figure, the letters in boxes 3, 4, 7, and 9 are C, I, C, and P.
Hints of the third kind are about repeated substrings of the secret string. The bar immediately below the boxes is cut into sections, each covering a substring of the secret string. A section may be joined by a line running to the left with another section of the same length. The substrings covered by two joined sections are equal. One hint in the figure says that the letters in boxes 8 and 9 equal the letters in boxes 4 and 5, and from that you get the substring IP.
Not every pair of equal substrings of the secret string appears among the hints. Some pairs are left out.
Two joined sections may also overlap. In the figure, the two letter substring in boxes 2 and 3 is said to equal the one in boxes 1 and 2, and the two sections share box 2.
In this example every position of the secret string can be decided, and the string is CCCIPCCIP. In general the hints may not be enough to decide every letter.
The answer to the puzzle is the letters at the asked positions. Write a question mark ? when the letter at an asked position cannot be decided from the given hints.
Input
The input consists of a single test case in the following format.
n a b q
x1 c1
.
.
.
xa ca
y1 h1
.
.
.
yb hb
z1
.
.
.
zq
The first line contains four integers , , , and . Here is the length of the secret string, is the number of hints on letters at specified positions, is the number of hints on repeated substrings, and is the number of positions asked.
The -th of the following lines contains an integer and an uppercase letter , meaning that the letter at position of the secret string is . These hints are ordered by position, that is .
The -th of the following lines contains two integers and . It is guaranteed that and . When is not 0, the substring of the secret string starting at position with length (with length when ) equals the substring of the same length starting at position . A line with tells nothing except that the substring specified in the line immediately above ends just before position .
The -th of the following lines contains an integer , the position of the letter to output.
At least one secret string matching all the given information exists. In other words, the hints have no contradiction.
Output
Print a single line of characters. The -th character of the output is the letter at position of the secret string when the hints decide it uniquely, and a question mark ? otherwise.