forked from Guarav/SPOJ
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSNGMSG.cpp
66 lines (59 loc) · 1.71 KB
/
SNGMSG.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include<iostream>
#include<stdio.h>
#include<string.h>
#define maxLength 1000
using namespace std;
int convertNumToDigitsAndReturnLength(int key, int keyDigits[10])
{
int index = 0;
while(key > 0)
{
keyDigits[index++] = key % 10;
key = key / 10;
}
return index;
}
int main()
{
int t, key;
char inputMessage[maxLength], keyString[10];
scanf("%d", &t);
while(t-->0)
{
scanf("%s", keyString);
int keyLength = strlen(keyString);
int keyDigits[10];
//conver the key to digits
//int keyLength = convertNumToDigitsAndReturnLength(key, keyDigits);
scanf("%s", inputMessage);
//encode message now
for(int i = 0; inputMessage[i] != '\0'; ++i)
{
int keyDigitRemainder = i % keyLength;
int keyDigitQuotient = i / keyLength;
if( keyDigitQuotient % 2 == 0 )
{
// at even positions we use direct key digits
int posn = inputMessage[i] - ( keyString[keyDigitRemainder] - '0' );
//cout<<posn<<" "<<inputMessage[i]<<" "<<keyDigits[keyDigitRemainder]<<endl;
if(posn < 97)
{
posn = 26 + 97 + posn - 97;
}
printf("%c", posn);
}
else
{
// at even positions we use reversed key digits
int posn = inputMessage[i] - ( keyString[ keyLength - 1 -keyDigitRemainder] - '0' );
if(posn < 97)
{
posn = 26 + 97 + posn - 97;
}
printf("%c", posn);
}
}
printf("\n");
}
return 0;
}