Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace Cipher
- {
- class Program
- {
- static Dictionary<string, char> dic = new Dictionary<string, char>();
- static HashSet<string> variations = new HashSet<string>();
- static void Main(string[] args)
- {
- string n = Console.ReadLine();
- string cipher = Console.ReadLine();
- FindCipher(cipher, 1, cipher[0], "");
- Decipher(n, "", 0, "");
- Console.WriteLine(variations.Count);
- List<string> list = variations.ToList();
- list.Sort();
- Console.WriteLine(string.Join(Environment.NewLine, list));
- }
- static void Decipher(string n, string currentResult, int i, string currentKey)
- {
- if (i == n.Length)
- {
- return;
- }
- currentKey += n[i];
- if (dic.ContainsKey(currentKey))
- {
- currentResult += dic[currentKey];
- Decipher(n, currentResult, i + 1, "");
- if (i == n.Length - 1)
- {
- variations.Add(currentResult);
- }
- currentResult = currentResult.Remove(currentResult.Length - 1, 1);
- }
- Decipher(n, currentResult, i + 1, currentKey);
- }
- static void FindCipher(string cipher, int i, char c, string code)
- {
- if (i == cipher.Length)
- {
- dic.Add(code, c);
- return;
- }
- if (!Char.IsDigit(cipher[i]))
- {
- dic.Add(code, c);
- FindCipher(cipher, i + 1, cipher[i], "");
- }
- else
- {
- code += cipher[i];
- FindCipher(cipher, i + 1, c, code);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement