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