Mock CCC '23 Contest 1 J4 - String Decryption

View as PDF

Submit solution


Points: 5 (partial)
Time limit: 1.0s
Memory limit: 512M

Author:
Problem type

The alphabetical sum of an alphabetic string is defined as the sum of the indices of each character in the alphabet. For example, the alphabetical value of the string abce is 11, since the indices of a, b, c, and e are 1, 2, 3, and 5, respectively, and 1 + 2 + 3 + 5 = 11.

Steven has a string which consists strictly of lowercase letters and asterisks. The asterisks can each be replaced with any lowercase letter in the English alphabet.

Given an integer N, representing Tommy's desired alphabetical sum, Steven wonders whether it is possible to construct a string with an alphabetical sum of N by replacing the asterisks in his string. If it is, he wants to know the lexicographically smallest such string.

Definition: A string S is lexicographically smaller than a string T if S \neq T and at the first index i where S_i \neq T_i, S_i < T_i.

Input Specification

The input consists of two lines. The first line contains N, representing Tommy's desired alphabetical sum. The second line contains S, representing the string Steven has.

The following table shows how the available 15 marks are distributed.

Mark AwardedExpected Alphabetical ValueLength of Steven's String
3 marks0 \leq N \leq 10^51 \leq |S| \leq 10^3
12 marks0 \leq N \leq 10^91 \leq |S| \leq 10^5

Output Specification

If it is impossible to construct a string that satisfies Tommy's expectations, output Impossible.

Otherwise, output the lexicographically smallest string such that the alphabetical sum of the string is N.

Sample Input 1

2
a*

Output for Sample Input 1

aa

Explanation of Output for Sample Input 1

The alphabetical sum of aa is 1 + 1 = 2. It can be shown that aa is the only possible string in this case.

Sample Input 2

4
a**

Output for Sample Input 2

aab

Explanation of Output for Sample Input 2

The alphabetical value of aab is 1 + 1 + 2 = 4. Other possible strings like aba also have 4 as the alphabetical sum, but aab is the lexicographically smallest.

Sample Input 3

1
a*

Output for Sample Input 3

Impossible

Explanation of Output for Sample Input 3

It can be shown that we cannot construct a string for this case that has an alphabetical sum of 1.


Comments

There are no comments at the moment.