45 lines
1.2 KiB
Plaintext
45 lines
1.2 KiB
Plaintext
-- Here is my recursive solution which also solves the extra problems on the discussion page:
|
|
|
|
sequence blocks = {"BO","XK","DQ","CP","NA","GT","RE","TG","QD","FS",
|
|
"JW","HU","VI","AN","OB","ER","FS","LY","PC","ZM"}
|
|
|
|
sequence words = {"","A","BarK","BOOK","TrEaT","COMMON","SQUAD","CONFUSE"}
|
|
|
|
--sequence blocks = {"US","TZ","AO","QA"}
|
|
--sequence words = {"AuTO"}
|
|
|
|
--sequence blocks = {"AB","AB","AC","AC"}
|
|
--sequence words = {"abba"}
|
|
|
|
sequence used = repeat(0,length(blocks))
|
|
|
|
function ABC_Solve(sequence word, integer idx)
|
|
integer ch
|
|
integer res = 0
|
|
if idx>length(word) then
|
|
res = 1
|
|
else
|
|
ch = word[idx]
|
|
for k=1 to length(blocks) do
|
|
if used[k]=0
|
|
and find(ch,blocks[k]) then
|
|
used[k] = 1
|
|
res = ABC_Solve(word,idx+1)
|
|
used[k] = 0
|
|
if res then exit end if
|
|
end if
|
|
end for
|
|
end if
|
|
return res
|
|
end function
|
|
|
|
constant TF = {"False","True"}
|
|
procedure ABC_Problem()
|
|
for i=1 to length(words) do
|
|
printf(1,"%s: %s\n",{words[i],TF[ABC_Solve(upper(words[i]),1)+1]})
|
|
end for
|
|
if getc(0) then end if
|
|
end procedure
|
|
|
|
ABC_Problem()
|