Palindrome Suits
Limits: 4 sec., 256 MiB
This statement was translated automatically from Ukrainian.
This is an interactive problem (where your program interacts with the jury’s program via standard input and output).
Zenyk and Marichka have stallions of various suits — bay, black, buckskin, and many others. They (Zenyk, Marichka, and the horses) want to play a game with you.
Zenyk and Marichka lined up \(n\)
horses in a row. The row is given by a string \(s\) of length \(n\) consisting of lowercase Latin letters.
The characters of the string represent the suits of the horses in the
order of the row. For example, the letter a can denote a
bay stallion, b — a black one, c — a buckskin
one, and so on.
Once the horses are lined up, Zenyk and Marichka will no longer change their order in the row. Thus, the string \(s\) is fixed in advance and will not change throughout the game.
You are given the number \(n\), but you cannot see the row and do not know the string \(s\). The goal of the game is to guess the length of the longest substring of \(s\) that is a palindrome.
A string \(t\) is called a
substring of a string \(s\) if
it is possible to delete some (possibly zero) characters from the
beginning and some (possibly zero) characters from the end of string
\(s\) to form the string \(t\). For example, the strings
algo, tester, got, e
are substrings of algotester, while lost,
alcotester are not.
A string \(t\) is called a
palindrome if it reads the same forwards and backwards. For
example, the strings abacaba, pylyp,
aa, b are palindromes, while
algotester, oblasna are not.
You can ask questions. Each question is a pair of numbers \(l, r\) such that \(1 \le l \le r \le n\). In response, Zenyk and Marichka will tell you whether the substring \(s_l s_{l+1} \dots s_r\) is a palindrome.
To make the game more interesting, Zenyk and Marichka set a limit — you can ask no more than \(25000\) questions.
Input
You are given an integer \(n\) — the number of horses in the row.
After reading the number \(n\), the interaction between your program and Zenyk and Marichka begins, during which your program asks questions and Zenyk and Marichka answer them.
To ask a question, print "?\(\
l \ r\)", where \(l, r\) are
integers satisfying \(1 \le l \le r \le
n\). This means you want to know whether the substring \(s_l s_{l+1} \dots s_r\) is a
palindrome.
In response to the question, Zenyk and Marichka will give an integer \(x\) (\(x \in \{0, 1\}\)), which you must read in your program. If \(x = 1\), then the substring is a palindrome; if \(x = 0\), then it is not.
If the question is invalid (i.e., the maximum number of questions has
been exceeded or the question parameters are invalid), Zenyk and
Marichka will reply with -1 and terminate the game. In this
case, terminate your program to receive the Wrong Answer
verdict. If you fail to do this, you may receive a
Runtime Error verdict.
Make sure to flush the output after printing each line. You can use:
fflush(stdout),cout << endlorcout.flush()inC++;System.out.flush()inJava;flush(output)inPascal;sys.stdout.flush()inPython;Console.Out.Flush()inC#.
Output
To give the answer, print a string in the format "!\(\ p\)", where \(p\) is the length of the longest substring
of \(s\) that is a palindrome. After
that, your program must terminate.
Constraints
\(1 \le n \le 7447\).
Scoring consists of the following blocks:
1 point for the example from the statement,
17 points: \(n \le 100\),
36 points: \(n \le 1000\), the answer to the problem is an even number,
21 points: \(n \le 1000\),
25 points: without additional constraints.
Points for a block are awarded only if your program passes all tests in that block.
Notes
Below is an example of interaction for \(n=7\), \(s
=\)cabaaba.
| Input | Output | Description |
|---|---|---|
7 |
Zenyk and Marichka state the number \(n\). | |
? 1 7 |
The participant asks if the entire string is a palindrome. | |
0 |
Zenyk and Marichka reply that the entire
string (cabaaba) is not a palindrome. |
|
? 1 1 |
The participant asks if the substring \(s_1\) is a palindrome. | |
1 |
Zenyk and Marichka reply that the
substring \(s_1\) (c) is a
palindrome. |
|
? 2 3 |
The participant asks if the substring \(s_2s_3\) is a palindrome. | |
0 |
Zenyk and Marichka reply that the
substring \(s_2s_3\) (ab)
is not a palindrome. |
|
? 2 4 |
The participant asks if the substring \(s_2s_3s_4\) is a palindrome. | |
1 |
Zenyk and Marichka reply that the
substring \(s_2s_3s_4\)
(aba) is a palindrome. |
|
? 4 5 |
The participant asks if the substring \(s_4s_5\) is a palindrome. | |
1 |
Zenyk and Marichka reply that the
substring \(s_4s_5\) (aa)
is a palindrome. |
|
? 2 7 |
The participant asks if the substring \(s_2s_3s_4s_5s_6s_7\) is a palindrome. | |
1 |
Zenyk and Marichka reply that the
substring \(s_2s_3s_4s_5s_6s_7\)
(abaaba) is a palindrome. |
|
! 6 |
The participant realizes that the answer is \(6\) and outputs it. |
Submit a solution
| Element Type | Created | Who | Problem | Compiler | Result | Time (sec.) | Memory (MiB) | # | Actions |
|---|
Palindrome Suits - Editorial
We solve the problem separately for palindromes of even and odd length.
If we have a palindrome \(p\) of
length \(x > 1\) and drop its first
and last characters, we get a palindrome of length \(x - 2\). For example, if \(p\) = abbcbba, dropping the
first and last characters gives bbcbb, which is also a
palindrome. We can keep dropping characters while there are at least 3
of them. What remains after this process we call the center of the
palindrome. For odd palindromes the center is one character, for even
ones it is empty.
Let’s solve the problem for odd palindromes. We go over the characters from left to right and try to improve the answer with them. Initially the answer for the first character is 1. Suppose we have considered the first \(i - 1\) characters as centers and the best answer is \(len\). For the center \(i\) we want to know whether we can improve the answer. We ask about the odd palindrome centered at the \(i\)-th character of length \(len + 2\). If the answer is that this substring is a palindrome, we update the answer and ask whether the string centered at \(i\) of length \(len + 4\) is a palindrome. We keep increasing the length while the query does not go beyond the string and the previous answer was positive.
Let’s count the queries used. Every positive answer increases the size of the largest palindrome by 2, so this happens at most \(\frac{n}{2}\) times. For each center, once we get the answer 0 we stop querying it, so there are at most \(n\) such queries.
Even palindromes are handled the same way, so the total number of queries is at most \(3 \cdot n\).
| Element Type | Created | Who | Problem | Compiler | Result | Time (sec.) | Memory (MiB) | # | Actions |
|---|