亦见:奶奶爱安娜
您将收到一串小写的ASCII字母。使用这个字典文件 (更新),您的任务是解决字谜。要解决一个字谜,您必须输出所有可以使用输入字符串中的每个字母形成的单词或组,用换行符隔开。按不同顺序排列的同一词组并不是唯一的,不应单独输出;然而,这些词的顺序并不重要。输出解决方案的顺序也不重要。如果输入不能形成单词,则不输出任何内容。
一些有用的测试用例:
Input: viinlg
Output: living
Input: fceodglo
Output: code golf
Input: flogflog
Output: golf golf
Input: ahwhygi
Output: highway
high way
Input: bbbbbb
Output: 规则/注意事项:
bluehouse)。我是密码-高尔夫。以字节为单位的最短代码获胜。祝好运!
发布于 2023-05-27 07:39:57
金色的版本。在网上试试!
(l:Seq[String],k:String)=>l.filter(w=>w.foldLeft((k,true)){case((r,b),c)=>val i=r.indexOf(c);if(i>=0)(r.substring(0,i)+r.substring(i+1),b)else(r,false)}._2)非高尔夫版。在网上试试!
object Main {
def main(args: Array[String]): Unit = {
val arr = List("living", "code", "golf", "highway", "high", "way")
def checkAvailability(str: String, keyword: String): Boolean = {
keyword.foldLeft((str, true)) { case ((remaining, result), ch) =>
val idx = remaining.indexOf(ch)
if (idx >= 0) (remaining.substring(0, idx) + remaining.substring(idx + 1), result && true)
else (remaining, false)
}._2
}
def filterList(arr: List[String], keyword: String): List[String] = {
arr.filter(word => checkAvailability(keyword, word))
}
println(filterList(arr, "viinlg"))
println(filterList(arr, "fceodglo"))
}
}发布于 2023-05-27 09:41:27
金色的版本。在网上试试!
let f s t=List.filter(fun w->List.fold_left(fun(x,y)c->let i=String.index_opt x c in match i with Some j->String.sub x 0 j^String.sub x(j+1) (String.length x-j-1),y|None->x,false)(t,true)(String.to_seq w|>List.of_seq)|>snd)s非高尔夫版。在网上试试!
let filter_strings lst str =
let rec filter_chars str char =
let i = String.index_opt str char in
match i with
| Some idx -> String.sub str 0 idx ^ String.sub str (idx + 1) (String.length str - idx - 1), true
| None -> str, false
in
List.filter (fun word ->
let _, ok = List.fold_left (fun (str, b) c -> let str', b' = filter_chars str c in str', b && b') (str, true) (String.to_seq word |> List.of_seq) in
ok) lst
let () =
let arr = ["living"; "code"; "golf"; "highway"; "high"; "way"] in
let result1 = filter_strings arr "viinlg" in
let result2 = filter_strings arr "fceodglo" in
print_endline (List.fold_left (fun a b -> if a = "" then b else a ^ "," ^ b) "" result1);
print_endline (List.fold_left (fun a b -> if a = "" then b else a ^ "," ^ b) "" result2)发布于 2023-05-27 09:47:15
金色的版本。在网上试试!
f=fn(l,k)->Enum.filter(l,fn w->elem(Enum.reduce(String.graphemes(w),{k,true},fn c,{r,b}->if String.contains?(r,c),do: {String.replace(r,c,"",global: false),b},else: {r,false}end),1)end)end非高尔夫版。在网上试试!
defmodule Main do
def run do
filter_strings = fn (lst, k) ->
Enum.filter(lst, fn w ->
str = Enum.reduce(String.graphemes(w), {k, true}, fn c, {r, b} ->
case String.contains?(r, c) do
true -> {String.replace(r, c, "", global: false), b}
_ -> {r, false}
end
end)
elem(str, 1)
end)
end
arr = ["living", "code", "golf", "highway", "high", "way"]
IO.inspect(filter_strings.(arr, "viinlg"))
IO.inspect(filter_strings.(arr, "fceodglo"))
end
end
Main.runhttps://codegolf.stackexchange.com/questions/127243
复制相似问题