Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 04. Santa's Secret Helper
- After the successful second Christmas, Santa needs to gather information about the behavior of children to plan the presents for next Christmas. He has a secret helper, who is sending him encrypted information. Your task is to decrypt it and create a list of the children who have been good.
- You will receive an integer, which represents a key and afterwards some messages, which you must decode by subtracting the key from the value of each character. After the decryption, to be considered a valid match, a message should:
- - Have a name, which starts after '@' and contains only letters from the Latin alphabet
- - Have a behaviour type - "G"(good) or "N"(naughty) and must be surrounded by "!" (exclamation mark).
- The order in the message should be: child’s name -> child’s behavior. They can be separated from the others by any character except: '@', '-', '!', ':' and '>'.
- You will be receiving message until you are given the “end” command. Afterwards, print the names of the children, who will receive a present, each on a new line.
- Input / Constraints
- • The first line holds n – the number which you have to subtract from the characters – integer in range [1…100];
- • On the next lines, you will be receiving encrypted messages.
- Output
- Print the names of the children, each on a new line
- Examples
- Input Output Comments
- 3 Kate We receive three messages and to decrypt them we use the key:
- CNdwhamigyenumje$J$ Bobbie First message has decryption key 3. So we substract from each characters code 3 and we receive:
- CEreelh-nmguuejn$J$ @Kate^jfdvbkrjgb!G!
- CVwdq&gnmjkvng$Q$ @Bobbie*kjdrrbgk!G!
- end @Stan#dkjghskd!N!
- They are all valid and they contain a child’s name and behavior – G for good and N for naughty.
- Input Output Comments
- 3 Kim We receive four messages. They are with key 3:
- N}eideidmk$'(mnyenmCNlpamnin$J$ Connor Kzbfabfajh!$%jkvbkj@Kim^jkfk!G!
- ddddkkkkmvkvmCFrqqru-nvevek$J$nmgievnge Valentine aaaahhhhjshsj@Connor*ksbsbh!G!kjdfbskdb
- ppqmkkkmnolmnnCEhq/vkievk$Q$ mmnjhhhjklijkk@Ben,shfbsh!N!
- yyegiivoguCYdohqwlqh/kguimhk$J$ vvbdffsldr@Valentine,hdrfjeh!G!
- end
- input Output
- 4 Bobbie
- ~lwzjkl~jenlymfDFsffmiCwozwlzjln%K% Sammie
- 0zfjrl}xnrlDWeqqmi/wnznlwzjnn%K%onhfhnf Lana
- ;:<lyiljz{onzDPere=;=9<;8=rhknlf%K% Samantha
- Wonvfkmwzkmpwvzkm'lhjnlDWeqerxle0wlnzj{nz%K%nohwn
- DReh}e=<4lhzj1%K%
- end
- using System;
- using System.Text.RegularExpressions;
- using System.Collections.Generic;
- public class Program
- {
- public static void Main()
- {
- var key = int.Parse(Console.ReadLine());
- var listOfTheChildrenWhoWillReceiveAPresent = new List<string>();
- var encryptedInfo = "";
- while (!(encryptedInfo = Console.ReadLine()).Equals("end"))
- {
- var decryptedInfo = "";
- foreach (var item in encryptedInfo)
- {
- decryptedInfo += (char)(item - key);
- }
- var patternNameAndType = @"(?<=@)(?<Name>[A-Za-z]+)[^@\-!:>]*!(?<BehaviourType>G|N)(?=!)";
- var matchNameAndBehaviourType = Regex.Matches(decryptedInfo, patternNameAndType);
- foreach (Match item in matchNameAndBehaviourType)
- {
- var behaviourType = item.Groups["BehaviourType"].Value;
- if (behaviourType == "G")
- {
- var name = item.Groups["Name"].Value;
- listOfTheChildrenWhoWillReceiveAPresent.Add(name);
- }
- }
- }
- Console.WriteLine(string.Join("\n", listOfTheChildrenWhoWillReceiveAPresent));
- }
- }
Add Comment
Please, Sign In to add comment