#K94057. Safe String Truncation with Tolerance

    ID: 38556 Type: Default 1000ms 256MiB

Safe String Truncation with Tolerance

Safe String Truncation with Tolerance

Given a string ( s ), an integer ( L ) (limit), and an integer ( T ) (tolerance), your task is to truncate the string without splitting any word. Words are separated by spaces. You must include as many words as possible such that the total length does not exceed ( L ). However, if adding the next word causes the length to exceed ( L ) but does not exceed ( L+T ), then include that word and stop. Otherwise, do not include that word. If the first word is longer than ( L ), output an empty string.

Examples:

  1. For input: "This is an example of a string", ( L = 12 ), ( T = 5 ), the output is "This is an".
  2. For input: "Hello world", ( L = 11 ), ( T = 0 ), the output is "Hello world".
  3. For input: "Supercalifragilisticexpialidocious", ( L = 5 ), ( T = 1 ), the output is "".

inputFormat

The input is read from standard input (stdin) and consists of three lines:

  1. A non-empty string ( s ).
  2. An integer ( L ) representing the limit.
  3. An integer ( T ) representing the tolerance.

outputFormat

Output the truncated string to standard output (stdout).## sample

This is an example of a string
12
5
This is an