首页 > 代码库 > golang 轮训加密算法

golang 轮训加密算法

Roy‘s friends has been spying on his text messages, so Roy thought of an algorithm to encrypt text messages.

Encryption Algorithm is as follows:
We say message to be encrypted as Plain Text and encrypted form of message as Cipher.
Plain Text consists of lower case alphabets only.
Consider the Cipher Disk as shown in figure.

技术分享

Initially, we start with 0 (zero). For each character in Plain Text, we move either clockwise or anti-clockwise on the disk depending on which way is closest from where we are currently standing.
If both clockwise and anti-clockwise distances are equal, we give priority to clockwise movement.
Clockwise movements are represented using positive numbers while Anti-clockwise movements are represented as negative numbers.

Roy needs your help in implementing this algorithm. Given a Plain Text message, your task is to encrypt it using above algorithm and print the Cipher Text.

Input:
First line contains integer T - number of test cases.
Each of next T lines contains a string representing Plain Text message.

Output:
For each test case, print the encrypted form of given string in new line.
Each line should consist of space separated integers in the range [-12,13].
See the sample test case for more clarification.

Constraints:
1 <= T <= 100
1 <= Length of Plain Text string <= 100

Sample Test Case Explanation:
Explanation for 3rd sample test case "correct"

技术分享

SAMPLE INPUT
3aeiouhackerearthcorrect
SAMPLE OUTPUT
 
0 4 4 6 67 -7 2 8 -6 13 13 -4 -9 2 -122 12 3 0 13 -2 -9
 
Explanation

We begin from 0 (zero) 1. ‘a‘->‘c‘ - two steps clockwise, we reach ‘c‘ 2. ‘c‘->‘o‘ - twelve steps clockwise, we reach ‘o‘ 3. ‘o‘->‘r‘ - three steps clockwise, we reach ‘r‘ 4. ‘r‘->‘r‘ - we are already at ‘r‘, so zero steps 5. ‘r‘->‘e‘ - thirteen steps clockwise, we reach ‘e‘ 6. ‘e‘->‘c‘ - here moving anti-clockwise is optimal, so two steps anticlockwise, and for anticlockwise we add negative sign. 7. ‘c‘->‘t‘ - again anti-clockwise, nine steps.

就是用0到26来代码A到Z字母,然后根据输入的字符串转换成,两个字母间的最小距离,用到了判断绝对值的函数。其中 leftValue 为顺时针计算的距离,rightValue 是逆时针计算的距离,然后取绝对值小的数值。

package mainimport (	"fmt")var step int = 97func CalcAbs(a int) (ret int) {	ret = (a ^ a>>31) - a>>31	return}func main() {	var numCount int	fmt.Scanln(&numCount)	var question string	var Previous int = -1	for i := 0; i < numCount; i++ {		fmt.Scanln(&question)		for _, v := range question {			if Previous == -1 {				Previous = int(v) - 97				fmt.Print(int(v)-97, " ")			} else {				var current = int(v) - 97				var leftValue, rightValue int				if current >= Previous {					rightValue = http://www.mamicode.com/current - Previous"left:%d-right:%d", left, right)				//fmt.Println("")				if CalcAbs(leftValue) > CalcAbs(rightValue) {					fmt.Print(rightValue, " ")				} else {					fmt.Print(leftValue, " ")				}				Previous = current			}		}		fmt.Println("")	}}

  

 

golang 轮训加密算法