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.

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

Can you explain why my input is not efficient

I copied this input line from gekksforgeeks How to use getline() in C++ when there are blank lines in input? - GeeksforGeeks

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