Connection Status:
Competition Arena > BearPasswordLexic
SRM 695 · 2016-06-25 · by Errichto · Brute Force
Class Name: BearPasswordLexic
Return Type: String
Method Name: findPassword
Arg Types: (vector<int>)
Problem Statement

Problem Statement

A substring of a string is any non-empty contiguous subsequence of its characters. For example, both "abc" and "bcd" are substrings of "abcde", but "ace" is not a substring of "abcde".


A string is called constant if all of its characters are the same. For example, "a" and "bbbbb" are constant strings, but "aba" is not a constant string.


Two substrings of the same string are considered distinct if they start or end at a different position. For example, the string "ababab" contains three distinct copies of the substring "ab", and the string "aaaa" contains two distinct copies of the substring "aaa".


Bear Limak is creating a new account and he needs to choose a password. His password should satisfy the following security requirements:

  • The password must be a string of length N.
  • Each character of the password must be either 'a' or 'b'.
  • For each i between 1 and N, inclusive, the password must contain exactly x[i-1] constant substrings of length i.

You are given the int[] x with N elements. Help Limak: find and return a valid password! If there are many valid passwords, return the lexicographically smallest of them. If there are no valid passwords, return "" (i.e., an empty string).

Constraints

  • N will be between 1 and 50, inclusive.
  • x will contain exactly N elements.
  • Each element in x will be between 0 and N, inclusive.
Examples
0)
{5,0,0,0,0}
Returns: "ababa"

Since the given int[] x has five elements, the password must contain exactly five characters. A password must contain x[0] = 5 constant substrings of length 1, and 0 constant substrings of bigger lengths. The only two valid passwords are "ababa" and "babab". The first one is smaller lexicographically.

1)
{4,2,1,0}
Returns: "aaab"
2)
{3,1,1}
Returns: ""
3)
{4,3,2,1}
Returns: "aaaa"
4)
{0}
Returns: ""

Submissions are judged against all 84 archived test cases, of which 5 are shown here. Case numbers match the judge’s.

Coding Area

Language: C++17 · define a public class BearPasswordLexic with a public method string findPassword(vector<int> x) · 84 test cases · 2 s / 256 MB per case

Submitting as anonymous