fork download
  1. using System;
  2.  
  3. public class Kamus
  4. {
  5. private Dictionary<string, HashSet<string>> a = new Dictionary<string, HashSet<string>>();
  6.  
  7. public void tambah(string kata, string[] sinonim)
  8. {
  9. if (!a.ContainsKey(kata))
  10. {
  11. a[kata] = new HashSet<string>();
  12. }
  13.  
  14. foreach (var item in sinonim)
  15. {
  16. if (!a.ContainsKey(item))
  17. {
  18. a[item] = new HashSet<string>();
  19. }
  20.  
  21. a[kata].Add(item);
  22. a[item].Add(kata);
  23. }
  24. }
  25.  
  26. public string[] ambilSinonim(string kata)
  27. {
  28. if (!a.ContainsKey(kata))
  29. {
  30. return null;
  31. }
  32.  
  33. return a[kata].Where(w => w != kata).ToArray();
  34. }
  35. }
  36.  
  37. public class Test
  38. {
  39. public static void Main()
  40. {
  41. Kamus kamus = new Kamus();
  42. kamus.tambah("big", new string[] { "large", "great" });
  43. kamus.tambah("big", new string[] { "huge", "fat" });
  44. kamus.tambah("huge", new string[] { "enormous", "gigantic" });
  45.  
  46. void showResult(string kata)
  47. {
  48. var sinonim = kamus.ambilSinonim(kata);
  49. Console.WriteLine($"Sinonim '{kata}': {(sinonim == null ? "null" : string.Join(", ", sinonim))}");
  50. }
  51.  
  52. showResult("big");
  53. showResult("huge");
  54. showResult("gigantic");
  55. showResult("colossal");
  56. }
  57. }
Success #stdin #stdout 0.06s 27544KB
stdin
Standard input is empty
stdout
Sinonim 'big': large, great, huge, fat
Sinonim 'huge': big, enormous, gigantic
Sinonim 'gigantic': huge
Sinonim 'colossal': null