https://www.spoj.pl/problems/WSCIPHER/
Weird Wally's Wireless Widgets, Inc. manufactures an eclectic assortment of small, wireless, network capable devices, ranging from dog collars, to pencils, to fishing bobbers. All these devices have very small memories. Encryption algorithms like Rijndael, the candidate for the Advanced Encryption Standard (AES) are demonstrably secure but they don't fit in such a tiny memory. In order to provide some security for transmissions to and from the devices, WWWW uses the following algorithm, which you are to implement.
Encrypting a message requires three integer keys, k1, k2, and k3. The letters [a-i] form one group, [j-r] a second group, and everything else ([s-z] and underscore) the third group. Within each group the letters are rotated left by ki positions in the message. Each group is rotated independently of the other two. Decrypting the message means doing a right rotation by ki positions within each group.
Consider the message the_quick_brown_fox encrypted with ki values of 2, 3 and 1. The encrypted string is _icuo_bfnwhoq_kxert. The figure below shows the decrypting right rotations for one character in each of the three character groups.
Looking at all the letters in the group [a-i] we see {i,c,b,f,h,e} appear at positions {2,3,7,8,11,17} within the encrypted message. After a right rotation of k1=2, these positions contain the letters {h,e,i,c,b,f}. The table below shows the intermediate strings that come from doing all the rotations in the first group, then all rotations in the second group, then all the rotations in the third group. Rotating letters in one group will not change any letters in any of the other groups.
| | [a-i], k1= 2 | [j-r], k2= 3 | [s-z] and _, k3= 1 |
| Encrypted: | _icuo_bfnwhoq_kxert | _heuo_icnwboq_kxfrt | _heuq_ickwbro_nxfot |
| Decrypted: | _heuo_icnwboq_kxfrt | _heuq_ickwbro_nxfot | the_quick_brown_fox |
| Changes: | ^^ ^^ ^ ^ | ^ ^ ^^ ^ ^ | ^ ^ ^ ^ ^ ^ ^ |
All input strings contain only lowercase letters and underscores(_). Each string will be at most 80 characters long. The ki are all positive integers in the range 1-100.
Input consists of information for one or more encrypted messages. Each problem begins with one line containing k1, k2, and k3 followed by a line containing the encrypted message. The end of the input is signalled by a line with all key values of 0.
For each encrypted message, the output is a single line containing the decrypted string.
Input:
2 3 1
_icuo_bfnwhoq_kxert
1 1 1
bcalmkyzx
3 7 4
wcb_mxfep_dorul_eov_qtkrhe_ozany_dgtoh_u_eji
2 4 3
cjvdksaltbmu
0 0 0
Output:
the_quick_brown_fox
abcklmxyz
the_quick_brown_fox_jumped_over_the_lazy_dog
ajsbktcludmv
| Added by: | Wanderley Guimarães |
| Date: | 2006-06-09 |
| Time limit: | 1s |
| Source limit: | 50000B |
| Languages: | All except: ERL TECS JS |
| Resource: | ACM Mid Central Regionals 2001 |
My Solution
using System;
using System.Collections.Generic;
namespace www.spoj.pl._899_WSCIPHER
{
class Program
{
static void Main(string[] args)
{
while (true)
{
string line = Console.In.ReadLine();
string[] nums = line.Split(new char[] { ' ' });
if (nums.Length == 3)
{
int k1 = 0;
int.TryParse(nums[0], out k1);
int k2 = 0;
int.TryParse(nums[1], out k2);
int k3 = 0;
int.TryParse(nums[2], out k3);
if (k1 == 0 && k2 == 0 && k3 == 0)
break;
string ciphertext = Console.In.ReadLine();
CipherGroup Group1 = new CipherGroup(k1);
CipherGroup Group2 = new CipherGroup(k2);
CipherGroup Group3 = new CipherGroup(k3);
int len = ciphertext.Length;
for (int i = 0; i < len; i++)
{
char c = ciphertext[i];
if (c >= 'a' && c <= 'i')
{
Group1.Add(c, i);
}
else if (c >= 'j' && c <= 'r')
{
Group2.Add(c, i);
}
else
{
Group3.Add(c, i);
}
}
Group1.Rotate();
Group2.Rotate();
Group3.Rotate();
string[] letters = new string[len];
Group1.AddToArray(letters);
Group2.AddToArray(letters);
Group3.AddToArray(letters);
Console.WriteLine(string.Join("", letters));
}
}
}
}
public class CipherGroup
{
public int k { get; set; }
public List<char> Letters { get; set; }
public List<int> Positions { get; set; }
public CipherGroup(int k1)
{
k = k1;
Letters = new List<char>();
Positions = new List<int>();
}
public void Add(char c, int p)
{
Letters.Add(c);
Positions.Add(p);
}
public void Rotate()
{
int len = Letters.Count;
if (len == 0) return;
for (int i = 0; i < k; i++)
{
Letters.Insert(0, Letters[len - 1]);
Letters.RemoveAt(len);
}
}
public void AddToArray(string[] letters)
{
int len = Letters.Count;
for (int i = 0; i < len; i++)
{
letters[Positions[i]] = Letters[i].ToString();
}
}
}
}