#C14326. URL Shortener Service

    ID: 43963 Type: Default 1000ms 256MiB

URL Shortener Service

URL Shortener Service

You are required to implement a URL shortening service. The service should support two operations:

  • shorten <long_url>: Generate a shortened URL for the given long URL.
  • retrieve <short_url>: Retrieve the original long URL for a given short URL. If the short URL does not exist, output an empty string.

For a given long URL, the shortened URL is generated by computing a simple hash as follows:

\( H = (\sum_{i=1}^{n} \text{ASCII}(s_i)) \bmod 16^6 \)

Then, represent \(H\) in hexadecimal with exactly 6 digits (padded with leading zeros if necessary) and append it to "http://short.url/". Ensure that if the same long URL is provided again, the service returns the same short URL.

You need to process a series of operations. Read input from standard input (stdin) and output the result of each operation to standard output (stdout).

inputFormat

The first line contains an integer \(T\) (number of operations). Each of the next \(T\) lines contains an operation in one of the following formats:

  • shorten <long_url>
  • retrieve <short_url>

Here, <long_url> and <short_url> are strings without spaces.

outputFormat

For each operation, output a line:

  • For a shorten operation, output the generated short URL.
  • For a retrieve operation, output the original long URL if it exists, or an empty line otherwise.
## sample
4
shorten http://example.com
retrieve http://short.url/0006b1
retrieve http://short.url/abcdef
shorten http://example.com
http://short.url/0006b1

http://example.com

http://short.url/0006b1

</p>