找到所有的字谜和子字谜!


13

这个问题很大程度上是基于这个问题,但是应该带来许多其他困难。

你的任务

您必须编写一个程序或函数,该程序或函数在接收到字符串时会打印出所有可能的字谜。出于这个问题的目的,一个字谜是一个字符串,它包含与原始字符串相同的字符,但不是原始字符串。子字谜是输入字符串的子字符串的字谜。字谜和子字谜不一定是或包含实际单词。

输入值

您可以通过任何标准输入法接受长度大于0的字符串。它可以包含任何ASCII字符。

输出量

您可以以任何标准方式输出输入字符串的所有可能的字谜和子字谜。您不得输出相同的字符串两次,也不能输出与输入相等的字符串。

其他规定

不允许使用标准漏洞

计分

这是,最少字节获胜。


空字符串可能是一个字谜吗?
Digital Trauma

是否允许输出原始字符串?
CalculatorFeline

@CalculatorFeline“您不得输出相同的字符串两次,或输出等于输入的字符串。”
Jonathan Allan

@DigitalTrauma,“您可以通过任何标准输入法接受长度大于0的任何字符串”。(添加重点)
Gryphon

4
一些测试用例会有所帮助
Xcoder先生,2017年

Answers:


8

05AB1E,7 个字节

Œ€œ˜Ù¹K

一个函数,它从输入接受一个字符串,并在堆栈上保留一个字符串列表。作为完整程序,将显示列表的表示形式。

在线尝试!

怎么样?

        - push input
Œ       - all substrings
 €œ     - for €ach: all permutations
   ˜    - flatten
    Ù   - de-duplicate
     ¹  - push 1st input onto top of stack
      K - pop a,b; push a without any b's (remove the copy of the input from the list)
        - as a full program: implicit print of the top of the stack

而且...您处理得更短。
狮ry

这是相同的算法,只是减少了字节数。
乔纳森·艾伦

是的,语言的改变是全部,但仍然令人印象深刻。
狮ry

@ ais523好像我都走错路了!
乔纳森·艾伦,

@ ais523我认为它是固定的。
Jonathan Allan

9

Brachylog(2),7个字节

{sp}ᶠdb

在线尝试!

说明

{sp}ᶠdb
{  }ᶠ    Find all
  p        permutations of
 s         a substring of {the input}
     d   Remove duplicates (leaving the list otherwise in the same order)
      b  Remove the first (the input itself)

(2)是什么意思?
狮ry

@Gryphon(afaik)有2个版本的branchylog,这是使用V2的版本。
约翰·汉密尔顿

1
好的,不确定是版本号还是使用其他可能非法的方法计算的字节数。

1
这是我第二次被问到。我猜我将不得不开始将其编写为(v2)

7

果冻,9 字节

ẆŒ!€;/QḟW

一个单子链接,它接受一个列表并返回除输入本身之外的所有不同子字谜的列表。

在线尝试!(页脚通过加入换行符漂亮地打印了结果列表。)

怎么样?

ẆŒ!€;/QḟW - Link: list of characters, s
Ẇ         - all contiguous sublists of s
 Œ!€      - all permutations for €ach sublist now a list of lists of lists)
     /    - reduce by:
    ;     -   concatenation (flattens the list by one level)
      Q   - de-duplicate (gets the unique entries)
        W - wrap s in a list (otherwise filtering will attempt to remove characters)
       ḟ  - filter discard from left if in right (remove the one equal to the input)

4

珀斯,12岁

-{.n.pM.:Q)]

在线测试

         Q       # input
       .: )      # all substrings
    .pM          # all permutations of all substrings
  .n             # flatten
 {               # deduplicate
-          ]Q    # subtract (list of) (implicit) input

@ ais523重做-我认为现在是正确的。
Digital Trauma

3

Japt,10字节

à má c â Å

在线尝试!

我也必须依次使用àáâ一个答案。真是巧合

说明

 à má c â Å
Uà má c â s1  // Ungolfed
              // Implicit: U = input string
Uà            // Take all combinations of characters in the input string.
   má         // Map each combination to all of its permutations.
      c       // Flatten into a single array.
        â     // Uniquify; remove all duplicates.
          s1  // Remove the first item (the input) from the resulting array.
              // Implicit: output resulting array, separated by commas

1
您甚至也管理过Å。
狮ry

1

Mathematica,60个字节

DeleteCases[""<>#&/@Permutations[c=Characters@#,Tr[1^c]],#]&

Permutations接受一个可选的数字参数,该参数告诉它要用于排列的输入值有多少。如果我们给它输入的长度,它将为输入的所有子集生成排列,没有重复。我们需要做的就是删除输入。


1

爪哇8,313个 312 306字节

import java.util.*;s->{Set l=new HashSet();for(int z=s.length(),i=0,j;i<z;i++)for(j=i;j<z;p("",s.substring(i,j+++1),l));l.remove(s);l.forEach(System.out::println);}void p(String p,String s,Set l){int n=s.length(),i=0;if(n<1)l.add(p);else for(;i<n;p(p+s.charAt(i),s.substring(0,i)+s.substring(i+++1,n),l));}

修改版我的答案在这里,这里p("",s,l);已被替换for(int z=s.length(),i=0,j;i<z;i++)for(j=i;j<z;p("",s.substring(i,j+++1),l));

-6字节感谢我链接的答案中的@OlivierGrégoire

这部分的解释:

在这里尝试。

for(int l=s.length(),i=0,j;i<l;i++)
                               // Loop (1) from 0 to the length of the String (exclusive)
  for(j=i+1;j<=l;              //  Loop (2) from 1 to the length of the String (exclusive)
    p("",                      //   Call the permutation-method,
    s.substring(i,j+++1),l));  //   with a substring from `i` to `j` (inclusive)
                               //  End of loop (2) (implicit / single-line body)
                               // End of loop (1) (implicit / single-line body)

0

Perl 6,75个字节

{unique(flat $_,.comb.combinations.skip».permutations.map(*».join)).skip}

尝试一下

展开:

{                    # bare block lambda with implicit parameter 「$_」

  unique(            # return each (sub)anagram once

    flat             # unstructure the following (List of Lists into flat List)
      $_,            # the input (so we can skip it at the end)

      .comb          # split the input into graphemes
      .combinations  # get all the combinations
      .skip\         # skip the first empty combination
      ».permutations # get all the permutations of each combination
      .map(*».join)  # join the inner permutations

  ).skip             # skip the first value (the input)
}
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.