Connection Status:
Competition Arena > Hexspeak
SRM 662 · 2015-06-30 · by cgy4ever · Encryption/Compression
Class Name: Hexspeak
Return Type: String
Method Name: decode
Arg Types: (long long)
Problem Statement

Problem Statement

If you compile a Java program and open the .class file in a text editor, you will find that the first four bytes spell "CAFEBABE" in hexadecimal. Using hexadecimal numbers to spell words is called Hexspeak.

Hexadecimal numbers are integers written in base 16. The letters 'A' through 'F' represent the digits with values 10 through 15. For example, the integer 202 written in hexadecimal is "CA". This is because 202 = 12*16 + 10, and the digits 12 and 10 are written as 'C' and 'A', respectively.

In this problem we will use eight different letters: in addition to the letters 'A' through 'F' we will also interpret the digit 0 as the letter 'O' and the digit 1 as the letter 'I'. Hence, any word that only consists of the letters ABCDEFIO can be interpreted as a hexadecimal number. Such words are called valid hexspeak words.

Fox Ciel has a long ciphertext containing a positive integer. Convert this number to hexadecimal. If you get the representation of a valid hexspeak word, return that word. Otherwise, return the string "Error!" (quotes for clarity). In other words, you should return "Error!" if the hexadecimal representation of ciphertext contains some occurrence of a digit between 2 and 9, inclusive.

Notes

  • The correct hexadecimal representation of ciphertext does not contain any leading zeros.
  • The return value is case-sensitive.

Constraints

  • ciphertext will be between 1 and 1,000,000,000,000,000,000, inclusive.
Examples
0)
257
Returns: "IOI"

The number 257 in decimal is written as 101 in hexadecimal. The digits 1 and 0 represent the characters 'I' and 'O', thus we should return "IOI".

1)
258
Returns: "Error!"

The number 258 in decimal is written as 102 in hexadecimal. The digit 2 does not represent a letter, so we return "Error!".

2)
3405691582
Returns: "CAFEBABE"
3)
2882400001
Returns: "ABCDEFOI"
4)
999994830345994239
Returns: "DEOBIFFFFFFFFFF"

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

Coding Area

Language: C++17 · define a public class Hexspeak with a public method string decode(long long ciphertext) · 20 test cases · 2 s / 256 MB per case

Submitting as anonymous