Advertisement
Guest User

Untitled

a guest
Nov 15th, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.93 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4.  
  5. namespace Vizhener
  6. {
  7.     class MainClass
  8.     {
  9.         public static void Main(string[] args)
  10.         {//wxmgomrk
  11.             var alphabet = " abcdefghijklmnopqrstuvwxyz0123456789'";
  12.             var text = "first longest word=sticking|wxmgomrkmrxo5kyoeoky1f5qyf5iyxrl4yen5yfw3rkywqob3o5pyxowyxrfkyqxen8pxyqxrymef5bxrwy5fxyovcyfllyeopxq";
  13.             var keyWord = text.Split('|', '=')[1];
  14.             var parsedText = text.Split('|')[1];
  15.             var charArr = new char[alphabet.Length];
  16.  
  17.             for (int i = 0; i < alphabet.Length; i++)
  18.                 charArr[i] = alphabet[i];
  19.  
  20.             var charDict = new Dictionary<char, int>();
  21.             for (int i = 0; i < charArr.Length; i++)
  22.                 charDict.Add(charArr[i], i);
  23.  
  24.             var stepKeyArr = new int[keyWord.Length - 1];
  25.             var stepTextArr = new int[parsedText.Length -1];
  26.  
  27.             for (int i = 1; i <= stepKeyArr.Length; i++)
  28.             {
  29.                 var firstLetter = charDict[keyWord[i - 1]];
  30.                 var secondLetter = charDict[keyWord[i]];
  31.  
  32.                 if (firstLetter < secondLetter) firstLetter += charArr.Length;
  33.                 stepKeyArr[i - 1] = firstLetter - secondLetter;
  34.             }
  35.  
  36.             for (int i = 1; i <= stepTextArr.Length; i++)
  37.             {
  38.                 var firstLetter = charDict[parsedText[i - 1]];
  39.                 var secondLetter = charDict[parsedText[i]];
  40.  
  41.                 if (firstLetter < secondLetter) firstLetter += charArr.Length;
  42.                 stepTextArr[i - 1] = firstLetter - secondLetter;
  43.                
  44.             }
  45.  
  46.             var startIndex = FindSubarrayStartIndex(stepTextArr, stepKeyArr);
  47.  
  48.             var cypherWord = new StringBuilder();
  49.  
  50.             for (int i = 0; i < keyWord.Length; i++)
  51.             {
  52.                 cypherWord.Append(parsedText[startIndex]);
  53.                 startIndex++;
  54.             }
  55.  
  56.             Console.WriteLine(cypherWord.ToString());
  57.  
  58.             var step = charDict[keyWord[0]] - charDict[cypherWord[0]];
  59.             var result = new StringBuilder();
  60.  
  61.             for (int i = 0; i < parsedText.Length; i++)
  62.                 result.Append(charArr[(charDict[parsedText[i]] + alphabet.Length + step) % alphabet.Length]);
  63.  
  64.             Console.WriteLine(result.ToString());
  65.         }
  66.  
  67.         public static int FindSubarrayStartIndex(int[] array, int[] subArray)
  68.         {
  69.             for (var i = 0; i < array.Length - subArray.Length + 1; i++)
  70.                 if (ContainsAtIndex(array, subArray, i))
  71.                     return i;
  72.             return -1;
  73.         }
  74.  
  75.         public static bool ContainsAtIndex(int[] array, int[] subArray, int index)
  76.         {
  77.             foreach (int element in subArray)
  78.                 if (array[index] != element) return false;
  79.                 else index++;
  80.             return true;
  81.         }
  82.     }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement