-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDeBruijnSequence.java
More file actions
68 lines (57 loc) · 1.77 KB
/
DeBruijnSequence.java
File metadata and controls
68 lines (57 loc) · 1.77 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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashSet;
import java.util.Set;
import java.util.Vector;
public class DeBruijnSequence {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s[] = br.readLine().split(" ");
int n = Integer.parseInt(s[0]);
String A = "01";
int k = 2;
System.out.println(deBruijn(n, k, A));
}
static Set<String> seen = new HashSet<String>();
static Vector<Integer> edges = new Vector<Integer>();
// Modified DFS in which no edge
// is traversed twice
static void dfs(String node, int k, String A)
{
for (int i = 0; i < k; ++i)
{
String str = node + A.charAt(i);
if (!seen.contains(str))
{
seen.add(str);
dfs(str.substring(1), k, A);
edges.add(i);
}
}
}
// Function to find a de Bruijn sequence
// of order n on k characters
static String deBruijn(int n, int k, String A)
{
// Clearing global variables
seen.clear();
edges.clear();
String startingNode = string(n - 1, A.charAt(0));
dfs(startingNode, k, A);
String S = "";
// Number of edges
int l = (int) Math.pow(k, n);
for (int i = 0; i < l; ++i)
S += A.charAt(edges.get(i));
S += startingNode;
return S;
}
private static String string(int n, char charAt)
{
String str = "";
for (int i = 0; i < n; i++)
str += charAt;
return str;
}
}