CTEXT - Editorial

PROBLEM LINK:

Practice

Contest

Author: rahul_ojha_07

Tester: horsbug98

DIFFICULTY:

EASY

PREREQUISITES:

Modulo Operation, Basic Mathematics

PROBLEM:

Given a string containing Characters and numbers replace each character by the specified character in the Question.

EXPLANATION:

We can solve this question very easily by the use of the ASCII codes of the characters. without the need of specifying which characters should replace which character (using if-else or switch - case).
According to the question we have to replace characters:

  1. a'-'m' with 'n'-'z'
  2. 'n'-'z' with 'a'-'m'
  3. 'A'-'M' with 'N'-'Z'
  4. 'N'-'Z' with 'A'-'M'
  5. '0'-'4' with '5'-'9'
  6. '5'-'9' with '0'-'4'

The ASCII code of character β€˜a’ is 97 and β€˜n’ is 110 we can get to β€˜n’ from β€˜a’ with the addition of 13 to ASCII code of β€˜a’, similarly by adding 13 to the ASCII code of β€˜b’ we get the character β€˜o’ and similarly by adding 13 to the ASCII code of a char we can get the desired so on till the character β€˜m’(109). so for the first condition, we can say if the Ascii code of a character is greater than or equal to 97 and less than or equal to 109 we can get the desired character by adding 13 to it.
Similarly also for Uppercase characters from β€˜A’(65) to β€˜M’(77) we can add 13 to the ASCCI code and get Characters from β€˜N’(78) to β€˜Z’(90).

if (AsciiCode >= 97 and AsciiCode <= 109) or (AsciiCode >= 65 and AsciiCode <= 77):
    AsciiCode + = 13

Now, we can get β€˜a’ from β€˜n’ by subtracting 13 from the ASCII code of β€˜n’. We can use technique this to get the desired character from β€˜n’(110) - β€˜z’(122).
Similarly also for Uppercase characters from β€˜N’(78) to β€˜Z’(90) we can subtract 13 from the ASCCI code and get Characters from β€˜A’(65) to β€˜M’(77).

if (AsciiCode >= 110 and AsciiCode <= 122) or (AsciiCode >= 78 and AsciiCode <= 90):
    AsciiCode - = 13

By following this method we can also get the digit replacement, for example, the ASCII code for β€˜0’ is 48 and ASCII code for β€˜5’ is 53 so in this case, we can add 5 to the digit and get the desired digit i.e.

if (AsciiCode >= 48 and AsciiCode <= 52):
    AsciiCode + = 5

and for getting β€˜0’-β€˜4’ from β€˜5’-β€˜9’ we can subtract 5 from the ascci code i.e.

if (AsciiCode >= 53 and AsciiCode <= 57):
    AsciiCode - = 5

Using this simple method for every character we can get the desired string.

Author’s solution can be found here.

1 Like

why did this give TLE @rahul_ojha_07


[1]


  [1]: https://www.codechef.com/viewsolution/22680424

You input method is not efficient. First make you input method efficient then check your code because you code also not correct.

Can you explain why my input is not efficient

This is because of that extra while loop during input. Instead of using that while loop , use two getline(). Refer this


[1]

 


  [1]: https://www.codechef.com/viewsolution/22711863

I copied this input line from gekksforgeeks https://www.geeksforgeeks.org/how-to-use-getline-in-c-when-there-are-black-lines-in-input/

I know this but this but while loop makes the code TLE.