hackercup / 2018 /quals /ethanstring.html
wjomlex's picture
2018 Problems
ab396f0 verified
raw
history blame
No virus
2.92 kB
<p>
Ethan's doing his very first programming assignment: implementing a <code>contains()</code> function. This function takes two strings, <strong>A</strong> and <strong>B</strong>,
and returns <code>true</code> if <strong>A</strong> is a substring of <strong>B</strong>, and <code>false</code> otherwise.
</p>
<p>
Here's the algorithm that Ethan has come up with. Note that |<strong>A</strong>| denotes the length of <strong>A</strong>, and the individual characters of the strings are 1-indexed.
</p>
<ol>
<li>Set <em>i</em> and <em>j</em> to each be equal to 1.</li>
<li>If <em>i</em> &gt; |<strong>A</strong>|, return <code>true</code>.</li>
<li>If <em>j</em> &gt; |<strong>B</strong>|, return <code>false</code>.</li>
<li>If <strong>A<sub>i</sub></strong> = <strong>B<sub>j</sub></strong>, increment <em>i</em> and <em>j</em> by 1 each, and return to Step 2.</li>
<li>If <em>i</em> = 1, increment <em>j</em> by 1, and return to Step 2.</li>
<li>Set <em>i</em> to be equal to 1, and return to Step 2.</li>
</ol>
<p>
As the TA in charge of grading Ethan's assignment, this doesn't look quite right to you. To make sure Ethan doesn't get any more credit than he deserves, you'd like to find some inputs
for which his algorithm returns <code>false</code> even though it should return <code>true</code>.
</p>
<p>
The professor teaching this class has provided you with a half-written list of test cases. In particular, it's a list of inputs for the <strong>A</strong> parameter, and you're free to come up with
your own inputs for the <strong>B</strong> parameter. For each given string <strong>A</strong>, you want to find a string <strong>B</strong> that will cause Ethan's algorithm to return the
wrong output (<code>false</code> instead of <code>true</code>), if possible. <strong>A</strong> will only contain uppercase alphabetic characters, and <strong>B</strong> must follow the same constraint. The test cases shouldn't be too
large, so <strong>B</strong> must also contain at most 10,000 characters.
</p>
<h3>Input</h3>
<p>
Input begins with an integer <strong>T</strong>, the number of given strings.
Then, <strong>T</strong> lines follow. Each line contains a single string, <strong>A</strong>.
</p>
<h3>Output</h3>
<p>
For the <em>i</em>th given string, print a line containing "Case #<em>i</em>: "
followed by any valid string <strong>B</strong> that will cause Ethan's algorithm to return the wrong value, or "Impossible" if no such string exists.
</p>
<h3>Constraints</h3>
<p>
1 &le; <strong>T</strong> &le; 100 <br />
1 &le; |<strong>A</strong>| &le; 2,000 <br />
</p>
<h3>Explanation of Sample</h3>
<p>
In the first case, <em>i</em> and <em>j</em> will have these values in order the first 10 times the algorithm is at Step 2:
</p>
<pre>
i j
---
1 1
2 2
1 2
1 3
1 4
1 5
2 6
3 7
4 8
1 8
</pre>
<p>
Please note that other outputs for example cases 1 and 3 would also be accepted.
</p>