-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLloydsi_keys.cs
89 lines (73 loc) · 2.46 KB
/
Lloydsi_keys.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
using System;
using System.Threading;
using System.Threading.Tasks;
using System.Linq;
using System.IO;
namespace ConsoleApp1
{
class Program
{
public static int Calculate(string source1, string source2) //O(n*m)
{
var source1Length = source1.Length;
var source2Length = source2.Length;
var matrix = new int[source1Length + 1, source2Length + 1];
// First calculation, if one entry is empty return full length
if (source1Length == 0)
return source2Length;
if (source2Length == 0)
return source1Length;
// Initialization of matrix with row size source1Length and columns size source2Length
for (var i = 0; i <= source1Length; matrix[i, 0] = i++) { }
for (var j = 0; j <= source2Length; matrix[0, j] = j++) { }
// Calculate rows and collumns distances
for (var i = 1; i <= source1Length; i++)
{
for (var j = 1; j <= source2Length; j++)
{
var cost = (source2[j - 1] == source1[i - 1]) ? 0 : 1;
matrix[i, j] = Math.Min(
Math.Min(matrix[i - 1, j] + 1, matrix[i, j - 1] + 1),
matrix[i - 1, j - 1] + cost);
}
}
// return result
return matrix[source1Length, source2Length];
}
static void Main(string[] args)
{
string text = System.IO.File.ReadAllText(@"C:\Users\Developer\Downloads\keys\keys\test_cases\keys_9.in");
string[] temp = text.Split();
string[] test = temp.Skip(2).ToArray();
int[] counter = new int[test.Length];
Parallel.For(0, test.Length, i =>
{
counter[i] = Calculate(temp[1], test[i]);
});
for (int i=0; i < int.Parse(temp[0]); ++i){
}
int max = counter[0], index=0;
for (int i=0; i < counter.Length; ++i)
{
if (max > counter[i]) {
max = counter[i];
index = i;
}
}
//Console.WriteLine(index);
Console.WriteLine(index);
}
}
}
/*
0: 5
1: 3
2: 0
3: 17
4: 7
5: 30
6: 9
7: 108
8: 415
9: 118
*/