Easy

You've intercepted an encrypted message, and you are really curious about its contents. You were able to find out that the message initially contained only lowercase English letters, and was encrypted with the following cipher:

  • Let all letters from 'a' to 'z' correspond to the numbers from 0 to 25, respectively.
  • The number corresponding to the ith letter of the encrypted message is then equal to the sum of numbers corresponding to the first i letters of the initial unencrypted message modulo 26.

Now that you know how the message was encrypted, implement the algorithm to decipher it.

Example

For message = "taiaiaertkixquxjnfxxdh", the output should be
cipher26(message) = "thisisencryptedmessage".

The initial message "thisisencryptedmessage" was encrypted as follows:

  • letter 0: t -> 19 -> t;
  • letter 1: th -> (19 + 7) % 26 -> 0 -> a;
  • letter 2: thi -> (19 + 7 + 8) % 26 -> 8 -> i;
  • etc.

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] string message

    An encrypted string containing only lowercase English letters.

    Guaranteed constraints:
    1 ≤ message.length ≤ 200.

  • [output] string

    • A decrypted message.

[C] Syntax Tips

// Prints help message to the console
// Returns a string
char * helloWorld(char * name) {
    char * answer = malloc(strlen(name) + 8);
    printf("This prints to the console when you Run Tests");
    strcpy(answer, "Hello, ");
    strcat(answer, name);
    return answer;
}

더보기

Solution

char *cipher26(char *message)
{
	int sum=0;

	for(int i=0;i<strlen(message);i++)
	{
		message[i]-='a';
		for(int j=0;j<26;j++)
			if((sum+j)%26==message[i])
			{
				sum+=j;
				message[i]=j+'a';
				break;
			}
	}

	return message;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> Swap Adjacent Bits  (0) 2020.06.16
<Codesignal> Stolen Lunch  (0) 2020.05.29
<Codesignal> New Numeral System  (0) 2020.05.24
<Codesignal> Reflect String  (0) 2020.05.24
<Codesignal> Character Parity  (0) 2020.05.24

+ Recent posts