ค้นหา Anagrams ทั้งหมดและ Subanagrams ด้วย!


13

คำถามนี้เป็นคำถามหนักขึ้นปิดคำถามนี้แต่ควรก่อให้เกิดจำนวนของปัญหาเพิ่มเติม

งานของคุณ

คุณต้องเขียนโปรแกรมหรือฟังก์ชั่นที่เมื่อได้รับสตริงให้พิมพ์แอนนาแกรมที่เป็นไปได้ทั้งหมด สำหรับจุดประสงค์ของคำถามนี้แอนนาแกรมคือสตริงที่มีอักขระเหมือนกับสตริงดั้งเดิม แต่ไม่ใช่สตริงดั้งเดิม Subanagram เป็นแอนนาแกรมของสตริงย่อยของสตริงที่ป้อนเข้า แอนนาแกรมและกราฟย่อยไม่จำเป็นต้องเป็นหรือมีคำที่แท้จริง

อินพุต

คุณอาจยอมรับสตริงซึ่งอาจมีความยาวใด ๆ > 0 โดยวิธีการอินพุตมาตรฐาน มันอาจมีอักขระ ASCII ใด ๆ

เอาท์พุต

คุณสามารถส่งออกแอนนาแกรมและกราฟย่อยที่เป็นไปได้ทั้งหมดของสตริงที่ป้อนเข้าด้วยวิธีมาตรฐาน คุณต้องไม่ส่งออกสตริงเดียวกันสองครั้งหรือส่งออกสตริงเท่ากับอินพุต

กฎอื่น ๆ

ช่องโหว่มาตรฐานไม่ได้รับอนุญาต

เกณฑ์การให้คะแนน

นี่คืออย่างน้อยไบต์ชนะ


สตริงว่างเปล่าเป็นแอนนาแกรมที่เป็นไปได้หรือไม่?
บาดเจ็บทางดิจิทัล

การส่งออกสตริง / sustrings เดิมอนุญาตหรือไม่
CalculatorFeline

@CalculatorFeline "คุณต้องไม่ส่งออกสตริงเดียวกันสองครั้งหรือส่งออกสตริงเท่ากับอินพุต"
Jonathan Allan

@DigitalTrauma "คุณอาจยอมรับสตริงซึ่งอาจมีความยาว> 0โดยวิธีการอินพุตมาตรฐานใด ๆ " (เน้นการเพิ่ม)
Gryphon

4
กรณีทดสอบบางอย่างอาจมีประโยชน์
Mr. Xcoder

คำตอบ:


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

และ ... คุณจัดการบางสิ่งให้สั้นลง
กริฟฟอน

มันเป็นอัลกอริทึมเดียวกันเพียงไบต์น้อย
Jonathan Allan

ใช่การเปลี่ยนแปลงภาษาเป็นสิ่งที่ดี แต่ก็ยังน่าประทับใจ
กริฟฟอน

@ ais523 ดูเหมือนว่าฉันผิดทั้งสองทาง!
Jonathan Allan

@ 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) หมายถึงอะไร
กริฟฟอน

@Gryphon (afaik) มี branchylog 2 เวอร์ชันซึ่งกำลังใช้ V2
John Hamilton

1
ตกลงไม่แน่ใจว่าเป็นหมายเลขรุ่นหรือจำนวนไบต์ที่เป็นไปได้โดยใช้วิธีอื่นและอาจผิดกฎหมาย
กริฟฟอน

1
นั่นเป็นครั้งที่สองที่ฉันถูกถามตอนนี้ (v2)ผมคิดว่าผมจะต้องเริ่มต้นการเขียนเป็น

7

เยลลี่ขนาด 9 ไบต์

ẆŒ!€;/QḟW

ลิงก์ monadic ยอมรับรายการและส่งคืนรายการของ anagrams ย่อยที่แตกต่างทั้งหมดยกเว้นอินพุตเอง

ลองออนไลน์! (ส่วนท้ายสวยพิมพ์รายการผลลัพธ์โดยเข้าร่วมกับขึ้นบรรทัดใหม่)

อย่างไร?

ẆŒ!€;/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)


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
คุณยังจัดการÅด้วย
กริฟฟอน

1

Mathematica, 60 ไบต์

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

Permutationsใช้อาร์กิวเมนต์ตัวเลขที่เป็นทางเลือกซึ่งบอกจำนวนของค่าอินพุตที่จะใช้สำหรับการเรียงสับเปลี่ยน หากเราให้ความยาวของอินพุตมันจะสร้างการเรียงสับเปลี่ยนสำหรับชุดย่อยทั้งหมดของอินพุตโดยไม่ซ้ำกัน สิ่งที่เราต้องทำคือลบอินพุต


1

Java 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)
}
โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.