-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathllg.jl
More file actions
executable file
·50 lines (42 loc) · 1.25 KB
/
llg.jl
File metadata and controls
executable file
·50 lines (42 loc) · 1.25 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
#!/usr/bin/env julia
function traverse_paths{T}(graph::Array{Array{T}}, position::T, visited::BitArray, stack::Array{T}, toppath::Array{T}, depth::T)
@inbounds stack[depth] = position
@inbounds visited[position] = true
if depth > length(toppath)
empty!(toppath)
append!(toppath, stack[1:depth])
end
@inbounds for p in graph[position]
@inbounds if !visited[p]
traverse_paths(graph, p, visited, stack, toppath, depth + 1)
end
end
@inbounds visited[position] = false
end
function longest_path{T}(graph::Array{Array{T}})
len = length(graph)
visited = falses(len)
stack = zeros(T, len)
toppath = T[]
for i = 1:len
traverse_paths(graph, i, visited, stack, toppath, 1)
end
return toppath
end
function main()
words = unique(filter((e->!isempty(e)), map(strip, eachline(STDIN))))
count = length(words)
graph = Array{Array{typeof(count)}}((count,))
for (i, left) in enumerate(words)
graph[i] = typeof(count)[]
for (j, right) in enumerate(words)
if left[end] == right[1]
push!(graph[i], j)
end
end
end
for w in map(i -> words[i], longest_path(graph))
println(w)
end
end
main()