Password

Time limit: 1s Memory limit: 64MB Input: Output:

I forgot my password again! I am sitting at my computer punching in wrong passwords. All I remember is that my password contains only lowercase letters. Luckily, the login system responds with more than just "wrong password". It also tells me the length of the longest prefix of my input that occurs as a (not necessarily contiguous) subsequence in the password. Formally, for a password P=p1p2pNP = p_1p_2 \dots p_N and input Q=q1q2qNQ = q_1q_2 \dots q_N, the login system’s answer is the largest LL for which there exist indices 1k1<k2<<kLN1 \leq k_1 < k_2 < … < k_L \leq N such that qi=pkiq_i = p_{k_i} for all 1iL1 \leq i \leq L. The system also tells me NN, the length of my password, and SS, meaning my password only uses the first SS letters of the alphabet. For example, S=4S = 4 means my password only contains a, b, c and d (but not necessarily all of them).

Please help me recover my password!

Implementation details

This problem is interactive. You should implement the function:

string guess(int n, int s);
  • Arguments: NN and SS as described above.
  • Return value: the correct password.

Your program may call the function:

int query(string str);
  • Argument: a string of 11 to NN letters from among the first SS letters of the alphabet.
  • Return value: an integer between 00 and the length of strstr, representing the login system’s answer to your query.
  • To avoid compiler errors, you should declare this function using the exact text above, anywhere in the global scope before calling it.
  • You may call this function at most 50 00050\ 000 times for each test case.

Your program may define additional functions.

Sample grader

A sample_grader.cpp is provided for you to test your code locally. The sample grader reads the input from the file password.in in the format:

  • line 1: N S
  • line 2: password

You can compile the grader together with your solution. Then you can run the resulting binary to test your guessing strategy against the given input.

Subtasks

Subtask Percentage of points Input constraints
1 10% NS26N \leq S \leq 26; all the letters in the password are distinct
2 20% 2N1002 \leq N \leq 100 and 2S42 \leq S \leq 4
3 20% 2N2,0002 \leq N \leq 2{,}000 and 2S202 \leq S \leq 20
4 30% 2N3,5002 \leq N \leq 3{,}500
5 20% 2N5,0002 \leq N \leq 5{,}000

Example

Let the password be aab. The grader calls guess(3, 2). The call log may be:

Call Return value
guess("ab") 2
guess("abb") 2
guess("bab") 1
guess("aab") 3

At this point, guess(3, 2) should return "aab".

Log in or sign up to be able to send submissions!