Advertisement
svetlai

MatchMaker

Nov 21st, 2015
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.41 KB | None | 0 0
  1. namespace MatchMaker
  2. {
  3.     using System;
  4.     using System.Collections.Generic;
  5.  
  6.     public class Program
  7.     {
  8.         private static Dictionary<string, HashSet<string>> vertices = new Dictionary<string, HashSet<string>>();
  9.         private static SortedSet<string> guys = new SortedSet<string>();
  10.         private static HashSet<string> girls = new HashSet<string>();
  11.  
  12.         public static void Main()
  13.         {
  14.             int n = int.Parse(Console.ReadLine());
  15.  
  16.             for (int i = 0; i < n; i++)
  17.             {
  18.                 string name = Console.ReadLine();
  19.                 string gender = Console.ReadLine();
  20.                 int numberOfInterests = int.Parse(Console.ReadLine());
  21.                 string[] interests = Console.ReadLine().Split(' ');
  22.  
  23.                 for (int j = 0; j < numberOfInterests; j++)
  24.                 {
  25.                     string from;
  26.                     string to;
  27.                     if (gender == "m")
  28.                     {
  29.                         from = name;
  30.                         to = interests[j];
  31.                         guys.Add(from);
  32.                     }
  33.                     else
  34.                     {
  35.                         from = interests[j];
  36.                         to = name;
  37.                         girls.Add(name);
  38.                     }
  39.  
  40.                     if (!vertices.ContainsKey(from))
  41.                     {
  42.                         vertices[from] = new HashSet<string>();
  43.                     }
  44.  
  45.                     vertices[from].Add(to);
  46.                 }
  47.             }
  48.  
  49.             var used = new HashSet<string>();
  50.             var matches = new Dictionary<string, int>();
  51.             int bestMatch = 0;
  52.             string bestGuy = string.Empty;
  53.             string bestGirl = string.Empty;
  54.  
  55.             foreach (var guy in guys)
  56.             {
  57.                 used.Clear();
  58.                 matches.Clear();
  59.                 var nextVertices = new Queue<string>();
  60.                 nextVertices.Enqueue(guy);
  61.  
  62.                 while (nextVertices.Count > 0)
  63.                 {
  64.                     string current = nextVertices.Dequeue();
  65.                     used.Add(current);
  66.  
  67.                     if (girls.Contains(current))
  68.                     {
  69.                         if (!matches.ContainsKey(current))
  70.                         {
  71.                             matches[current] = 0;
  72.                         }
  73.  
  74.                         matches[current]++;
  75.                     }
  76.                     else
  77.                     {
  78.                         if (vertices.ContainsKey(current))
  79.                         {
  80.                             foreach (var item in vertices[current])
  81.                             {
  82.                                 if (!used.Contains(item))
  83.                                 {
  84.                                     nextVertices.Enqueue(item);
  85.                                 }
  86.                             }
  87.                         }
  88.                     }
  89.  
  90.                     if (matches.ContainsKey(current) && bestMatch < matches[current])
  91.                     {
  92.                         bestGuy = guy;
  93.                         bestGirl = current;
  94.                         bestMatch = matches[current];
  95.                     }
  96.                 }
  97.             }
  98.  
  99.             Console.WriteLine("{0} and {1} have {2} common {3}!", bestGuy, bestGirl, bestMatch, bestMatch == 1 ? "interest" : "interests");
  100.         }
  101.     }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement