给定一个只由字母组成的输入字符串,返回步骤大小,返回从任意字母开始访问所有字母所需的最小步数。
例如,以单词dog为例。如果我们使用1的步长,我们最终会得到:
defghijklmnopqrstuvwxyzabcdefg Alphabet
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
defghijklmnopqrstuvwxyzabcdefg Visited letters
d o g Needed letters总共30步。
然而,如果我们使用一个步骤大小为11,我们得到:
defghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefg
^ ^ ^ ^ ^ ^
d o z k v g Visited letters
d o g Needed letters总共有6个步骤。这是步骤的最小数量,因此dog的返回结果是步骤大小;11。
测试用例:
"dog" -> 11
"age" -> 6
"apple" -> 19
"alphabet" -> 9
"aaaaaaa" -> 0 for 0 indexed, 26 for 1 indexed
"abcdefga" -> 1 or 9
"aba" -> Any odd number except for 13
"ppcg" -> 15
"codegolf" -> 15
"testcase" -> 9
"z" -> Any number
"joking" -> 19a到z组成(您可以选择大写还是小写)。0-25)或1索引(1-26)发布于 2018-12-12 09:38:37
≔EEβEθ∧μ⌕⭆β§β⁺⌕β§θ⊖μ×κξλ⎇⊕⌊ιΣι⌊ιθI⌕θ⌊Φθ⊕ι在网上试试!链接是详细的代码版本。0-索引。解释:
Eβ循环26步的大小。(实际上,我在这里循环小写字母并使用索引变量。)
Eθ∧μ在第一个输入之后,循环遍历输入的每个字符。
⭆β§β⁺⌕β§θ⊖μ×κξ循环26次,并生成在给定步长下从输入的前一个字符开始(0-索引)的26步所产生的字符串。
⌕...λ查找输入的当前字符在该字符串中的位置,如果找不到-1。
E...⎇⊕⌊ιΣι⌊ι取所有位置之和,除非找不到一个,在这种情况下使用-1。
≔...θ省点钱吧。
⌊Φθ⊕ι找出最小的非负和。
I⌕θ...用这个和找到第一步的大小并输出它。
发布于 2018-12-12 06:25:57
w=>(a=[...Array(26).keys(m=1/0)]).map(s=>~[...w].map(c=>(t+=a.find(v=>!p|(u(c,36)+~v*s-u(p,36))%26==0),p=c),p=t=0,u=parseInt)+t<m&&(m=t,n=s))|n多亏了沙基,使用[...Array(26).keys()]可以节省9个字节。
发布于 2018-12-12 18:06:55
ƓI%
26×þ%iþÇo!SỤḢ输入是STDIN上的字节串,输出是1索引.
ƓI% Helper link. Argument: m (26 when called)
Ɠ Read a line from STDIN and eval it as Python code.
I Increments; take all forward differences.
% Take the differences modulo m.
26×þ%iþÇoSSỤḢ Main link. No arguments.
26 Set the argument and the return value to 26.
×þ Create the multiplication table of [1, ..., 26] by [1, ..., 26].
% Take all products modulo 26.
Ç Call the helper link with argument 26.
iþ Find the index of each integer to the right in each list to the left,
grouping by the lists.
o! Replace zero indices (element not found) with 26!.
This works for strings up to 25! = 15511210043330985984000000 chars,
which exceeds Python's 9223372036854775807 character limit on x64.
S Take the sum of each column.
Ụ Sort the indices by their corresponding values.
Ḣ Head; extract the first index, which corresponds to the minimal value.https://codegolf.stackexchange.com/questions/177404
复制相似问题