fbinnzhivko

04.01 Nakovs Matching

Apr 16th, 2016
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.71 KB | None | 0 0
  1. /* Problem 4 – Nakovs Matching
  2. We are given two words a and b. Each word can be split in several ways into left and right side:
  3. • a = (aLeft|aRight)
  4. • b = (bLeft|bRight)
  5. The weight w(s) of given character sequence s is calculated as sum of the ASCII codes of its characters.
  6.  * The Nakov's distance between two split words (aLeft|aRight) and (bLeft|bRight) is defined as:
  7. • nakovs = w(aLeft) * w(bRight) - w(aRight) * w(bLeft), as absolute value
  8. We assume that two word splits have good matching if their Nakov's difference is not greater than given limit number d.
  9.  * Your task is to write a program that prints all good matchings between given two words a and b and given limit number d.
  10. Example: a = "hello", b = "csharp", d = 20000. The word a can be split in 4 ways: "h|ello", "he|llo","hel|lo" and "hell|o".
  11.  * The word b can be split in 5 ways: "c|sharp", "cs|harp", "csh|arp", "csha|rp" and "cshar|p".
  12.  * There are 20 possible matchings between the words a and b, but only 3 of them are good matchings (nakovs ≤ d):
  13. (h|ello) matches (c|sharp) by 13996 nakovs  w(h) = 104, w(ello) = 428, w(c) = 99, w(sharp) = 542
  14. nakovs = abs(104 * 542 - 428 * 99) = 13996 ≤ 20000
  15. (he|llo) matches (cs|harp) by 17557 nakovs  w(he) = 205, w(llo) = 327, w(cs) = 214, w(harp) = 427
  16. nakovs = abs(205 * 427 - 327 * 214) = 17557 ≤ 20000
  17. (hell|o) matches (cshar|p) by 11567 nakovs  w(hell) = 421, w(o) = 111, w(cshar) = 529, w(p) = 112
  18. nakovs = abs(421 * 112 - 529 * 111) = 11567 ≤ 20000
  19. Your task is to write a program that enters a, b and d and prints on the console all good matchings between a and b.
  20. Input
  21. The input data should be read from the console. It consists of 3 lines:
  22. • The first line hold the first string.
  23. • The second line holds the second string.
  24. • The third line holds the limit number d.
  25. The input data will always be valid and in the format described. There is no need to check it explicitly.
  26. Output
  27. Print on the console all good matchings between these two words in format "(aLeft|aRight) matches (bLeft|bRight) by XXX nakovs",
  28.  * each at separate line. The order of the output lines is not important. Print "No" if no good matchings are found.
  29. Constraints
  30. • The input words a and b consist of small Latin letters only. The length of the words is in the range [2…20].
  31. • The number d is integer in the range [0…5 000 000].
  32.  */
  33.  
  34. using System;
  35. class NakovsMatching
  36. {
  37.     static void Main()
  38.     {
  39.         string first = Console.ReadLine().ToLower();
  40.         string second = Console.ReadLine().ToLower();
  41.  
  42.         int D = int.Parse(Console.ReadLine());
  43.         int count = 0;
  44.  
  45.         for (int i = 0; i < first.Length - 1; i++)
  46.         {
  47.             string a = first.Substring(0, i + 1);
  48.             string b = first.Substring(i + 1);
  49.             int wA = CalcWeight(a);
  50.             int wB = CalcWeight(b);
  51.  
  52.             for (int j = 0; j < second.Length - 1; j++)
  53.             {
  54.                 string c = second.Substring(0, j + 1);
  55.                 string d = second.Substring(j + 1);
  56.                 int wC = CalcWeight(c);
  57.                 int wD = CalcWeight(d);
  58.  
  59.                 int match = Math.Abs(wA * wD - wB * wC);
  60.                 if (match <= D)
  61.                 {
  62.                     Console.WriteLine("({0}|{1}) matches ({2}|{3}) by {4} nakovs", a, b, c, d, match);
  63.                     count++;
  64.                 }
  65.             }
  66.         }
  67.  
  68.         if (count == 0)
  69.         {
  70.             Console.WriteLine("No");
  71.         }
  72.     }
  73.  
  74.     private static int CalcWeight(string s)
  75.     {
  76.         char[] arr = s.ToCharArray();
  77.  
  78.         int weight = 0;
  79.         for (int i = 0; i < arr.Length; i++)
  80.         {
  81.             weight += arr[i];
  82.         }
  83.  
  84.         return weight;
  85.     }
  86. }
Add Comment
Please, Sign In to add comment