Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Globalization;
- using System.Text.RegularExpressions;
- namespace lab4.PhoenixOscarRomeoNovember
- {
- class Program
- {
- static void Main(string[] args)
- {
- string input = Console.ReadLine();
- var creatureData = new Dictionary<string, HashSet<string>>(); // with hashset we have only unique squads
- var temp = new Dictionary<string, HashSet<string>>();
- while (input != "Blaze it!") //start to collect the creature and squads
- {
- string[] tokens = input.Split(new String[] { " -> " }, StringSplitOptions.RemoveEmptyEntries);
- string creature = tokens[0];
- string squad = tokens[1];
- if (creature == squad)
- {
- input = Console.ReadLine();
- continue;
- }
- if (!creatureData.ContainsKey(creature))
- {
- creatureData.Add(creature, new HashSet<string>());
- temp.Add(creature, new HashSet<string>());
- }
- creatureData[creature].Add(squad);
- input = Console.ReadLine();
- }
- foreach (var datas in creatureData) //start to collect in the temp Dict only the squads we need
- {
- string creatureName = datas.Key;
- HashSet<string> squads = datas.Value;
- var tempHash = new HashSet<string>();
- foreach (var squad in squads)
- {
- if (SquadExistInCreatureKeys(squad,temp))
- {
- if (CreatureNameExistInSquadKey(creatureData[squad], creatureName))
- {
- continue;
- }
- else
- {
- tempHash.Add(squad);
- }
- }
- else
- {
- tempHash.Add(squad);
- }
- }
- foreach (var item in tempHash)
- {
- temp[creatureName].Add(item);
- }
- }
- foreach (var creature in temp.OrderByDescending(x =>x.Value.Count())) // start to output the result from temp Dict..
- {
- string creatur = creature.Key;
- HashSet<string> squadCount = creature.Value;
- Console.WriteLine("{0} : {1}",creatur,squadCount.Count());
- }
- }
- private static bool CreatureNameExistInSquadKey(HashSet<string> hashSet, string creatureName)
- {
- foreach (var squad in hashSet)
- {
- if (squad == creatureName)
- {
- return true;
- }
- }
- return false;
- }
- private static bool SquadExistInCreatureKeys(string squad, Dictionary<string, HashSet<string>> temp)
- {
- foreach (var key in temp.Keys)
- {
- if (key == squad) return true;
- }
- return false;
- }
- }
- }
Add Comment
Please, Sign In to add comment