Created
July 26, 2014 18:45
-
-
Save boyanov83/fda2e8168532016c25b3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
class NakovMatching | |
{ | |
static void Main() | |
{ | |
string word1 = Console.ReadLine(); | |
string word2 = Console.ReadLine(); | |
long diff = long.Parse(Console.ReadLine()); | |
List<char> char1 = word1.ToList(); | |
List<char> char2 = word2.ToList(); | |
bool check = false; | |
long nakovs = 0; | |
long firstSum1 = 0; | |
long firstSum2 = 0; | |
long firstWeight = 0; | |
long secondSum1 = 0; | |
long secondSum2 = 0; | |
long secondWeight = 0; | |
string print1 = ""; | |
string print2 = ""; | |
for (int i = 0; i < word1.Length - 1; i++) | |
{ | |
char1.Insert(i + 1, '|'); | |
firstSum1 = FindSum1(char1, firstSum1); | |
firstSum2 = FindSum2(char1, firstSum2); | |
for (int k = 0; k < word2.Length - 1; k++) | |
{ | |
char2.Insert(k + 1, '|'); | |
secondSum1 = FindSum1(char2, secondSum1); | |
secondSum2 = FindSum2(char2, secondSum2); | |
firstWeight = firstSum1 * secondSum2; | |
secondWeight = firstSum2 * secondSum1; | |
if (Math.Abs(firstWeight - secondWeight) <= diff) | |
{ | |
for (int l = 0; l < word1.Length + 1; l++) | |
{ | |
print1 += char1[l]; | |
} | |
for (int p = 0; p < word2.Length + 1; p++) | |
{ | |
print2 += char2[p]; | |
} | |
nakovs = (Math.Abs(firstWeight - secondWeight)); | |
Console.WriteLine("({0}) matches ({1}) by {2} nakovs", print1, print2, nakovs); | |
check = true; | |
} | |
firstWeight = 0; | |
nakovs = 0; | |
secondSum1 = 0; | |
secondSum2 = 0; | |
secondWeight = 0; | |
print2 = ""; | |
char2 = word2.ToList(); | |
print1 = ""; | |
} | |
firstSum1 = 0; | |
firstSum2 = 0; | |
char1 = word1.ToList(); | |
} | |
if (check == false) | |
{ | |
Console.WriteLine("No"); | |
} | |
} | |
private static long FindSum2(List<char> char1, long sum2) | |
{ | |
char1.Reverse(); | |
for (int o = 0; o < 900; o++) | |
{ | |
if (char1[o] == '|') | |
{ | |
break; | |
} | |
sum2 += char1[o]; | |
} | |
char1.Reverse(); | |
return sum2; | |
} | |
private static long FindSum1(List<char> char1, long sum1) | |
{ | |
for (int o = 0; o < 900; o++) | |
{ | |
if (char1[o] == '|') | |
{ | |
break; | |
} | |
sum1 += char1[o]; | |
} | |
return sum1; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment