Program time limit: 1 second
Program memory limit: 512 MB
You are designing a banner for CPM (Competitive Programming Masters) and want to make it as short as possible while still being recognizable. The banner should contain the letters "CPM" as a subsequence exactly $N$ times.
You are given an initial string that must be a prefix of your final banner. Given $N$ and the initial string, output the shortest string where "CPM" appears as a subsequence exactly $N$ times and starts with the given initial string. If there are multiple strings of equal length, output the lexicographically smallest one. If it's impossible to satisfy the constraints, output "impossible".
A subsequence is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements. For example, "CPM" appears as a subsequence in "CCPMM" four times (after removing elements at positions $(1, 4), (1, 5), (2, 4) or (2, 5)$).
For all test cases:
Output a single string, which is the shortest string containing "CPM" as a subsequence exactly N times and starting with the given initial string. If there are multiple strings of equal length, output the lexicographically smallest one. If it's impossible to satisfy the constraints, output "impossible".
You should read from standard input and write to standard output.
In Python, you could use the following code.
# Taking inputs, already done! :D
N = int(input())
L = int(input())
S = input().strip()
result = ""
# Write your code here
# Printing output
print(result)
In C or C++, you could use the following code.
// Taking inputs, already done! :D
int N; scanf("%d", &N);
int L; scanf("%d", &L);
char S[505]; scanf("%s", S);
string result;
// Write your code here
// Printing output
printf("%s\n", result.c_str());
1
3
CPM
CPM
We have an initial string "CPM" and need exactly 1 occurrence of "CPM" as a subsequence. The shortest string is "CPM" itself, which has length 3 and starts with the given initial string.
2
2
CC
CCPM
We have an initial string "CC" and need exactly 2 occurrences of "CPM" as a subsequence. The shortest string satisfying this is "CCPM", where "CPM" appears at positions (1, 3, 4) and (2, 3, 4).
1
3
CPP
impossible
We have an initial string "CPP" and need exactly one occurance of "CPM". This is impossible as we must add an "M" at some point, but adding one would create at least two "CPM"s.
Your program will be run on both sample cases and 20 secret cases one after another, and if it produces the correct output for all test cases, it solves this task. Recall that your final score on the task is the score of your highest scoring submission.
Log in to submit!