Enumeration

시간 제한1초메모리 제한512 MB

요약
S로 시작해 T로 끝나며 연속한 두 k-문자가 정확히 k-1개의 문자를 공유하도록 모든 k-단어를 나열하고, 해가 없으면 -1을 출력한다.
난이도

보통10점 중 7점

유형
그래프, 백트래킹, DFS, 조합론
정답자
아직 제출이 없습니다

문제

We are given a set Σ of n English lowercase characters. We select k characters from Σ without repetition and arrange these k characters in alphabetical order, then we get a word of k characters, which is called a k-word. For example, let n = 5 , k = 3, and Σ = {a, b, c, d, e}. Then there are ten 3-words which are abc, abd, abe, acd, ace, ade, bcd, bce, bde, and cde. Given two distinct k-words, S and T, we want to enumerate all k-words satisfying two conditions: (C1) the first k-word is S and the last k-word is T, and (C2) the number of common characters in any two consecutive k-words is exactly k − 1. In the above example, if we enumerate all 3-words for S = abd and T = bde , then we have a list of 3- words, (abd, abe, abc, ace, bce, bcd, cde, acd, ade, bde).

Given Σ, k, n, S, and T, enumerate all k-words so that the above two conditions (C1) and (C2) are satisfied.

입력

Your program is to read from standard input. The input consists of three lines. The first line contains two integers, n and k, where 2 ≤ n ≤ 20 and 1 ≤ k ≤ n − 1. The second line contains a string of n characters of Σ in alphabetical order. The third line contains two distinct k-words S and T separated by a single space.

출력

Your program is to write to standard output. The first line contains an integer representing the number of k-words in the enumeration list which satisfies two conditions (C1) and (C2). The second line contains all k-words in the order of enumeration. If there are many solutions, print any one of the solutions. If there is no solution, print -1 only.

예제3

  1. 예제 1

    입력
    5 3
    abcde
    abd bde
    
    예상 출력
    10
    abd abe abc ace bce bcd cde acd ade bde
    
  2. 예제 2

    입력
    5 1
    abcde
    d c
    
    예상 출력
    5
    d a b e c
    
  3. 예제 3

    입력
    4 3
    befy
    efy bef
    
    예상 출력
    4
    efy bey bfy bef