Skip to content

Instantly share code, notes, and snippets.

@johnboker
Created June 9, 2015 17:44
Show Gist options
  • Save johnboker/cf9eba67305b6e71f4e7 to your computer and use it in GitHub Desktop.
Save johnboker/cf9eba67305b6e71f4e7 to your computer and use it in GitHub Desktop.
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();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment