-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBruteForceAlgorithm.cs
More file actions
70 lines (55 loc) · 1.99 KB
/
BruteForceAlgorithm.cs
File metadata and controls
70 lines (55 loc) · 1.99 KB
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BruteForceAlgo
{
class BruteForceAlgo
{
public delegate bool BruteForceTest(ref char[] testChars);
public static bool BruteForce(string testChars, int startLength, int endLength, BruteForceTest testCallback)
{
for (int len = startLength; len <= endLength; ++len)
{
char[] chars = new char[len];
for (int i = 0; i < len; ++i)
chars[i] = testChars[0];
if (testCallback(ref chars))
return true;
for (int i1 = len - 1; i1 > -1; --i1)
{
int i2 = 0;
for (i2 = testChars.IndexOf(chars[i1]) + 1; i2 < testChars.Length; ++i2)
{
chars[i1] = testChars[i2];
if (testCallback(ref chars))
return true;
for (int i3 = i1 + 1; i3 < len; ++i3)
{
if (chars[i3] != testChars[testChars.Length - 1])
{
i1 = len;
goto outerBreak;
}
}
}
outerBreak:
if (i2 == testChars.Length)
chars[i1] = testChars[0];
}
}
return false;
}
static void Main(string[] args)
{
BruteForceTest testCallback = delegate (ref char[] testChars)
{
var str = new string(testChars);
return (str == "bbc");
};
bool result = BruteForce("abcde", 1, 5, testCallback);
Console.WriteLine(result);
}
}
}